Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int *array;
- int compare(int i, int j)
- {
- if (array[i] == array[j]) return 0;
- return array[i] < array[j] ? -1 : 1;
- }
- void swap(int i, int j)
- {
- int t = array[i];
- array[i] = array[j];
- array[j] = t;
- }
- void selectSort(int l, int r)
- {
- int i,j,k;
- for (j = r; j > l; j--) {
- k = j;
- i = j - 1;
- while (i >= l) {
- if (compare(k,i) < 0)
- k = i;
- i = i - 1;
- }
- swap(j,k);
- }
- }
- int partition(int left, int right)
- {
- int i,j;
- i = left;
- j = left;
- while (j < right) {
- if (compare(j,right) < 0) {
- swap(i,j);
- i++;
- }
- j++;
- }
- swap(i,right);
- return i;
- }
- void quicksort(int m, int left, int right)
- {
- int b;
- while ((left < right) && ((right - left + 1) >= m)){
- b = partition(left,right);
- quicksort(m,left,b - 1);
- left = b + 1;
- }
- selectSort(left,right);
- }
- int main()
- {
- int i, n, m;
- scanf("%d %d", &n,&m);
- array = (int*)calloc(n,sizeof(int));
- for (i = 0; i < n; i++) scanf("%d", array + i);
- quicksort(m, 0, n - 1);
- for (i = 0; i < n; i++) printf("%d ", array[i]);
- free(array);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement