| | |
Can any one solve the problem with this merge sort??????????
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 3
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; void merge(int a[],int low,int mid,int high); void mergesort(int a[],int low, int high) { if(high>low){ int mid=(low+high)/2; mergesort(a,low,mid); mergesort(a,(mid+1),high); merge(a,low,mid,high); } } void merge(int a[],int low,int mid,int high){ int t[(high-low)+1]; int il=0; int ir=0; int nl = mid - low + 1; int nr = high - mid; while((il<nl)&&(ir<nr)){ if(a[low+il]<=a[mid+1+ir]){ t[il+ir]=a[low+il]; il++; } else{ t[il+ir]=a[mid+1+ir]; ir++; } } while(il<nl){ t[il+ir]=a[low+il]; il++; } while(ir<nr){ t[il+ir]=a[mid+1+ir]; ir++; } for(int i=low;i<high;i++){ a[i]=t[i-low]; } } int main() { int a[10] = {0,4,9,11,16,2,3,5,7,8}; mergesort(a,0,10); for(int i=0;i<10;i++) cout<<a[i]<<" "; cout<<endl; return 0; }
![]() |
Similar Threads
- Merge 2 sorted list using ADT (C++)
- strstream problem (C++)
Other Threads in the C++ Forum
- Previous Thread: array
- Next Thread: C++ GUI framework help??
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph 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 tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






