0

Merge_Sort - I really don't understand what is happening but my sorting didn't working, I need help, Help Me!

#include<iostream> using namespace std; void merge(int arr[],int start,int mid,int end) { int i=start,j=mid+1,k=0; int temp[10]; while(i<=mid && j<end) { if(arr[i]<=arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; } while(i<=mid) { temp[k++] = arr[i++]; } while(j<=end) { temp[k++] = arr[j++]; } for(i=0;i<10;i++){ arr[i] = temp[i]; } } } void mergeSort(int arr[],int start,int end) { int mid = (start+end)/2; if(start<end) { mergeSort(arr,start,mid); mergeSort(arr,mid+1,end); merge(arr,start,mid,end); } } int main() { int arr[10] = {10,35,40,25,21,52,6,20,3,54}; mergeSort(arr,0,9); for(int i=0;i<10;i++) cout<<arr[i]<<" "; return 0; }

23rd Feb 2022, 3:59 PM
Praween Gupta
Praween Gupta - avatar
3 Réponses
0
Praween Gupta This should be your calling function void mergesort(int arr[],int low,int high) { if(low<high){ int mid=(low+high)/2; mergesort(arr,low,mid); mergesort(arr,mid+1,high); merge(arr,low,mid,high); } } and you have to rewrite the merge function code Follow this Instagram page For more coding information Id:anonymous_me_0000
28th Feb 2022, 4:59 PM
B.VIDYADHAR VIJJU
B.VIDYADHAR VIJJU - avatar
0
What i need to do here??
1st Mar 2022, 5:25 AM
Praween Gupta
Praween Gupta - avatar
0
Praween Gupta You are wrongly copying the values ... Have separate arrays for left part and right part Then merge them into singlenarray
1st Mar 2022, 5:28 AM
B.VIDYADHAR VIJJU
B.VIDYADHAR VIJJU - avatar