- void exch(int *p,int *q) //Data Exchange
- {
- int temp;
- temp = *p;
- *p = *q;
- *q = temp;
- }
- int partition(int *a,int left,int right) //Data Partition
- {
- int i = left-1;
- int j = right;
- int tag = a[right];
- while(1)
- {
- while(tag >= a[++i])
- ;
- while(tag <= a[--j])
- if(j == left)
- break;
- if(i >= j)
- break;
- exch(&a[i],&a[j]);
- }
- exch(&a[i],&a[right]);
- return i;
- }
- void quick_sort(int *a,int left,int right)
- {
- if(right <= left) return;
- int pos = partition(a,left,right);
- quick_sort(a,left,pos-1);
- quick_sort(a,pos+1,right);
- }