Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- usingnamespace std;
- #define BrEl 20
- void Merge(int Temp[], int Element[], constint list1, constint size1,
- constint list2, constint size2)
- {
- int i = list1, j = list2, k = list1;
- while(i < list1 + size1 && j < list2 + size2)
- {
- if (Temp[i] < Temp[j])
- Element[k++] = Temp[i++];
- else
- Element[k++] = Temp[j++];
- }
- while (i < list1 + size1)
- Element[k++] = Temp[i++];
- while (j < list2 + size2)
- Element[k++] = Temp[j++];
- for (k = list1; k < list1 + size1 + size2; k++)
- Temp[k] = Element[k];
- }
- void MergeSort(int Temp[], int Element[], constint list1, constint size)
- {
- int list2, size1, size2;
- if (size > 1)
- {
- list2 = list1 + size / 2;
- size1 = list2 - list1;
- size2 = size - size1;
- MergeSort(Temp, Element, list1, size1);
- MergeSort(Temp, Element, list2, size2);
- Merge(Temp, Element, list1, size1, list2, size2);
- }
- }
- int main()
- {
- int
- A[BrEl]={25,14,7,76,10,64,20,30,9,2,6,77,83,12,56,46,61,51,33,26};
- int
- B[BrEl]={25,14,7,76,10,64,20,30,9,2,6,77,83,12,56,46,61,51,33,26};
- cout<<"Unsorted array:"<<endl;
- for(int i=0;i<BrEl;i++)
- {
- cout<<A[i]<<endl;
- }
- MergeSort(B,A,0,BrEl);
- cout<<endl<<" Sorted array "<<endl;
- for(int i=0;i<BrEl;i++)
- {
- cout<<B[i]<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement