Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #define SIZE 7
- void mergesortarray(int *a, int begin, int mid, int mid2, int end)
- {
- int px = begin; // porque es donde inicia el primer arrglo
- int py = mid2; // porque es donde inicia el 2do arreglo
- int pt = begin; // porque el nuevo arreglo se llenara desde el inicio osea desde la pos 0;
- int temp[SIZE];
- while (px <= mid && py <= end)
- {
- if (a[px] < a[py])
- temp[pt++] = a[px++];
- else
- temp[pt++] = a[py++];
- }
- if (px > mid) // el primer arreglo llego a su limite
- {
- while (py <= end)
- {
- temp[pt++] = a[py++];
- }
- }
- else // el segundo arreglo llego a su limite
- {
- while (px <= mid)
- {
- temp[pt++] = a[px++];
- }
- }
- for (int i = begin; i <= end; i++)
- {
- a[i] = temp[i];
- }
- }
- void mergesort(int *a, int begin, int end)
- {
- if (begin < end)
- {
- int mid = (begin + end) / 2;
- mergesort(a, begin, mid);
- mergesort(a, mid + 1, end);
- mergesortarray(a, begin, mid, mid + 1, end);
- }
- }
- int main()
- {
- int a[SIZE] = { 5,4,3,1,2,6,7};
- for (int i = 0; i < SIZE; i++)
- {
- cout << a[i] << " ";
- }
- cout << endl;
- cout << "----------------------" << endl << endl;
- mergesort(a, 0, SIZE-1);
- for (int i = 0; i < SIZE; i++)
- {
- cout << a[i] << " ";
- }
- cout << endl;
- cout << "----------------------" << endl << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement