| | |
merge sort not working
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 7
Reputation:
Solved Threads: 1
i am trying out merge sort.
i have made two functions sort and merge;
i am calling merge recursively.
i think my sort function is working fine. the problem lies with merge function.
in merge function i am dividing the array using recursive calls. it ends when lb = = ub(ie lower bound equals upper bound). lower bound and upper bound are index of array.
then i am passing it to sort function function.
suggest me a solution of correction to merge sort.
i have made two functions sort and merge;
i am calling merge recursively.
i think my sort function is working fine. the problem lies with merge function.
in merge function i am dividing the array using recursive calls. it ends when lb = = ub(ie lower bound equals upper bound). lower bound and upper bound are index of array.
then i am passing it to sort function function.
suggest me a solution of correction to merge sort.
C++ Syntax (Toggle Plain Text)
#include<iostream> //#include<stream> using namespace std; int A[7] = {3,4,77,10,2,5,7}; void sort(int l1,int u1,int l2,int u2) { int B[7]; int i=l1; int j=l2; int k=0; while(i<=u1&&j<=u2) { if(A[i]<A[j]) { B[k] = A[i]; i++; k++; } else { B[k] = A[j]; j++; k++; } } while(j<=u2) { B[k]=A[j]; j++; k++; } while(i<=u1) { B[k] = A[i]; i++; k++; } for(i=l1;i<=u2;i++){ A[i]=B[i]; } } void merge(int lb,int ub) { int lo; lo=(ub+lb)/2; if(lb<ub){ merge(lb,lo); merge(lo+1,ub); sort(lb,lo,lo+1,ub); } } int main() { for(int i=0;i<7;i++) cout<<A[i]<<" "; cout<<endl; merge(0,6); //sort(0,3,4,6); for(int i=0; i<7;i++) cout<<A[i]<<" "; return 0; }
![]() |
Similar Threads
- merge sort (C++)
- Merge Sort on a Array-Based List (C++)
- Merge sort not working (C++)
- Merge Sort Code Not Working Properly In Vc++.code Is Given (C++)
- another merge sort question (C++)
Other Threads in the C++ Forum
- Previous Thread: How to pause a timer
- Next Thread: ADT Calendar
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





