| | |
merge sort not working
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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
Views: 728 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





