Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <iostream>
using namespace std;
void merge(int arr[], int p, int q, int r){
	
	int a = q - p + 1;
	int b = r - q;
	
	int left[a];
	int right[b];
	
	for(int i=0;i<a;i++){
		left[i] = arr[p + i];
	}
	for(int j=0;j<b;j++){
		right[j] = arr[q + j + 1];
	}
	
	int i=0;
	int j=0;
	int k=1;
	
	while(i < a && j < b){
		if(left[i] <= right[j]){
			arr[k] = left[i];
			i++;
			k++;
		}else{
			arr[k] = right[j];
			j++;
			k++;
		}
	}
	
	while(i < a){
		arr[k] = left[i];
		i++;
		k++;
	}
	
	while(j < b){
		arr[k] = right[j];
		j++;
		k++;
	}
}
void mergeSort(int arr[], int p, int r){
	if(p < r){
		int q = p + (r-p)/2;
		
		mergeSort(arr, p, q);
		mergeSort(arr, q+1, r);
		merge(arr, p, q, r);
		
	}
}

int main() {

    int arr[] = {12, 11, 13, 5, 6, 7}; 
    int arr_size = sizeof(arr)/sizeof(arr[0]); 
  
    printf("Given array is \n"); 
    for(int i=0;i<arr_size;i++){
    	cout<<arr[i]<<" ";
    }
  
    mergeSort(arr, 0, arr_size - 1); 
  
    printf("\nSorted array is \n"); 
     for(int i=0;i<arr_size;i++){
    	cout<<arr[i]<<" ";
    }
    return 0; 
}


What I have tried:

I can't seem to figure out what's wrong with it.
Posted
Updated 9-May-19 21:42pm
v2
Comments
ZurdoDev 9-May-19 7:59am    
First, tell us what it is supposed to do. Then tell us what it is doing. But you should debug it and figure it out.

1 solution

Quote:
Can anyone tell me, what's wrong with this merge sort code! I'm new to programming and can't figure it out.

With the debugger, you will watch your code performing, give it a try, it is a great learning tool.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
sparshjuneja 9-May-19 8:17am    
Thanks, I'll surely try this!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900