Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- //using namespace std;
- void swap_f(int* a, int* b)
- {
- *a = *a + *b;
- *b = *a - *b;
- *a = *a - *b;
- }
- int partition(int* a, int left, int right)
- {
- int pivot = left + rand() % (right - left);
- pivot = a[pivot];
- while (left <= right)
- {
- while (a[left] < pivot)
- {
- left++;
- }
- while (a[right] > pivot)
- {
- right--;
- }
- if (left >= right)
- {
- break;
- }
- swap_f(&a[left++], &a[right--]);
- }
- return right;
- }
- void quickSort(int* a, int left, int right)
- {
- if (left < right)
- {
- int pivot = partition(a, left, right);
- quickSort(a, left, pivot);
- quickSort(a, pivot + 1, right);
- }
- }
- int* Partition(int* left, int* right)
- {
- int* pivot = left + rand() % (right - left);
- while (left <= right)
- {
- while (*left < *pivot)
- {
- left++;
- }
- while (*right > *pivot)
- {
- right--;
- }
- if (left >= right)
- {
- break;
- }
- swap_f(left++, right--);
- }
- return right;
- }
- void QuickSort(int* left, int* right)
- {
- if (left < right)
- {
- int* pivot = Partition(left, right);
- QuickSort(left, pivot);
- QuickSort(pivot + 1, right);
- }
- }
- int main(void)
- {
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- srand(time(0));
- int current_index = 0;
- int* a = (int *)malloc(sizeof(int) * 100100);
- int temp;
- while (scanf("%d", &temp))
- {
- a[current_index++] = temp;
- }
- //quickSort(a, 0, current_index - 1);
- QuickSort(a, a + current_index - 1);
- for (int i = 0; i < current_index; i++)
- {
- printf("%d ", a[i]);
- }
- printf("\n");
- return 0;
- }
- /* 2 3 4 32 34 3456 54 567 654 32 34 54 32 3 4567 8 765 432 345 678 7 6543 2 567 ^D */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement