| | |
Merge sort not working
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 5
Reputation:
Solved Threads: 0
Below is the code for my merge sort - It doesn't like my temporary array c and I can't get anything to sort - I get all 0 in my output.
C++ Syntax (Toggle Plain Text)
void merge(int low,int mid,int high) { int size, p, q, i, r; size = high - low + 1; //size of array c int c[size]; //array c - the temporary array for sorting p=low; //index for a[i]...a[mid] q=mid+1; //index for a[mid + 1]...a[j] while((p<=mid)&&(q<=high)) { if(a[p]<=a[q]) //copy smaller value to local array c { c[i]=a[p]; p++; } else { c[i]=a[q]; q++; } i++; } while (a[p]<= mid)//copy the rest of the longer array { c[i] = a[p]; } while (a[q]<=high)//copy the rest of the larger array { c[i]=a[q]; } for (i=low; i<=high; i++)//copy back from temp array to main array { a[i]=c[i]; }} void merge(int low,int mid,int high) { int size, p, q, i, r; size = high - low + 1; //size of array c int c[size]; //array c - the temporary array for sorting p=low; //index for a[i]...a[mid] q=mid+1; //index for a[mid + 1]...a[j] while((p<=mid)&&(q<=high)) { if(a[p]<=a[q]) //copy smaller value to local array c { c[i]=a[p]; p++; } else { c[i]=a[q]; q++; } i++; } while (a[p]<= mid)//copy the rest of the longer array { c[i] = a[p]; } while (a[q]<=high)//copy the rest of the larger array { c[i]=a[q]; } for (i=low; i<=high; i++)//copy back from temp array to main array { a[i]=c[i]; }} int main() { long timeElapsed; timeElapsed = clock(); int num,i,elem, percent; //a[num]; cout<<"*********************************************************************"<<endl; cout<<" MERGE SORT PROGRAM"<<endl; cout<<"**********************************************************************"<<endl<<endl<<endl; cout<<"Please Enter THE NUMBER OF ELEMENTS you want to sort[THEN PRESS ENTER]:"<<endl; cin>>num; cout<<"Please Enter THE PERCENTAGE you want to sort[THEN PRESS ENTER]:"<<endl; cin>>percent; //for (i=1; i<=num; i++) for (i=0; i<num; i++) { elem =(rand()+1); cout<<endl<<"element "<<i<<"is "<<elem<<endl; a[i]= elem; cout<<"When i is "<<i<<" ai is "<<a[i]; } merge_sort(0,num); cout<<endl<<"So, the sorted list (using MERGE SORT) will be : "<<endl; for(i=0;i<num;i++) { cout<<a[i]<<" "; } cout<<endl<<endl<<"Time Elapsed is: "<<timeElapsed<<endl<<endl<<endl<<endl; system("PAUSE");
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> ... It doesn't like my temporary array c and I can't get anything to sort ...
the compiler should not like it at all (it is not c++); and should give you an error.
size is not a constant known at compile time.
the compiler should not like it at all (it is not c++); and should give you an error.
C++ Syntax (Toggle Plain Text)
int c[size]; //array c - the temporary array for sorting
The size variable is a variable. The compiler does not know what value it might hold at any given time.
Since you are using C++, you should be using a vector or deque for this instead of an array. But if you must use an array, just make one as big as the largest size you think you'll have, and make sure that it complains if size ever gets too large.
I haven't time to look very deeply at your code ATM, so if you are still having problems later I'll help then.
Since you are using C++, you should be using a vector or deque for this instead of an array. But if you must use an array, just make one as big as the largest size you think you'll have, and make sure that it complains if size ever gets too large.
I haven't time to look very deeply at your code ATM, so if you are still having problems later I'll help then.
I think you need to go sit down with your professor and work on this a little with him. There are a lot of syntactic and logical errors in here, which I think you can best overcome by working with someone face to face. He shouldn't give you a hard time. Most professors are genuinely pleased when students ask for help understanding a problem. (If for no other reason, it gives them a chance to talk about it more.)
Before you go, google "merge sort" to find some good animations of what is going on. Ignore the stuff in the WikiPedia. (There's nothing wrong with it, it'll just confuse you and your professor will know if you scavenge code from it --honestly, it would be really obvious.)
I was going to give you a bunch of stuff about a binary tree (which is a form of linked list) but I think you ought to go see your professor first. Most merge sorts are done using recursion instead of using an actual binary tree in memory --which is how you are trying to do it.
Hope this helps.
Before you go, google "merge sort" to find some good animations of what is going on. Ignore the stuff in the WikiPedia. (There's nothing wrong with it, it'll just confuse you and your professor will know if you scavenge code from it --honestly, it would be really obvious.)
I was going to give you a bunch of stuff about a binary tree (which is a form of linked list) but I think you ought to go see your professor first. Most merge sorts are done using recursion instead of using an actual binary tree in memory --which is how you are trying to do it.
Hope this helps.
![]() |
Similar Threads
- Merge Sort Code Not Working Properly In Vc++.code Is Given (C++)
- Merge Sort (C)
- hw assignment help on selection & merge sort (C)
- another merge sort question (C++)
- Merge sort (C++)
Other Threads in the C++ Forum
- Previous Thread: Fibonacci number series
- Next Thread: same problem
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux 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 return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






