//    Quick Sort 
//    Source: Algorithms and Data Structure in C,BohYoh Shibata, Soft Bank Publishing, p.178 

#include   <stdio.h>

#define  swap(type,x,y)  do {type t=x; x=y; y=t;} while (0)

void quick(int a[], int left, int right)
{

  int i=left;
  int j=right;
  int pivot= a[(i+j)/2];
  
  do{
    while (a[i]<pivot) i++;
    while (a[j]>pivot) j--;
    if(i<=j) {
      swap(int, a[i], a[j]);
      i++;
      j--;
    }
  } while (i <= j);

  if(left<j)  quick(a,left,j);
  if(i<right) quick(a,i,right);
}

int main(void)
{
  int i;
  int x[4];
  int nx=sizeof(x)/sizeof(x[0]);

  printf("Masukkan %d buah bilangan bulat\n",nx);
  for(i=0;i<nx;i++) {
    printf("x[%d] : ",i);
    scanf("%d", &x[i]);
    
  }
  quick(x,0,nx-1);
  puts("Proses sorting selesai.");
  for(i=0;i<nx;i++) 
    printf("x[%d]=%d\n",i,x[i]);
  
  return(0);

}

