Advertisement
Eather

Quick sort (Ascending order)

Apr 15th, 2011
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3.  
  4.  
  5. void quicksort(int arr[], int low, int high);
  6.  
  7. int main() {
  8.  int array[10] ;
  9.  int i,max;
  10. scanf("%d",&max);
  11.  
  12.  
  13.  for(i = 0; i < max; i++)
  14.    scanf("%d",&array[i]);
  15.  
  16.  printf("Before quicksort: ");
  17.  for(i = 0; i < max; i++) {
  18.   printf(" %d ", array[i]);
  19.  }
  20.  printf("\n");
  21.  
  22.  quicksort(array, 0, (max - 1));
  23.  
  24.  printf("After  quicksort: ");
  25.  for(i = 0; i < max; i++) {
  26.   printf(" %d ", array[i]);
  27.  }
  28.  printf("\n");
  29.  
  30.  return 0;
  31. }
  32.  
  33. void quicksort(int arr[], int low, int high) {
  34.  int i = low;
  35.  int j = high;
  36.  int temp = 0;
  37.  
  38.  int z = arr[(low + high) / 2];
  39.  
  40.  do {
  41.   while(arr[i] < z) i++;
  42.  
  43.   while(arr[j] > z) j--;
  44.  
  45.   if(i <= j)
  46.   {
  47.    temp = arr[i];
  48.    arr[i] = arr[j];
  49.    arr[j] = temp;
  50.    i++;
  51.    j--;
  52.   }
  53.  } while(i <= j);
  54.  
  55.  if(j> low )
  56.   quicksort(arr, low, j);
  57.  
  58.  if(i < high)
  59.   quicksort(arr, i, high);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement