[ create a new paste ] login | about

Link: http://codepad.org/yDISKhmX    [ raw code | output | fork ]

C++, pasted on Oct 18:
#include<stdio.h>
void swap (int &a, int &b)
{
    int t =a;
    a=b;
    b=t;
}
int  partition(int a[], int low , int hight){
    int cot =a[hight];
    int left = low;
    int right = hight-1 ;
    while(true){
        while(left<= right && a[left] <cot ) left ++;
        while( right >= left && a[right] > cot) right --;
        if(left  >= right){
            break;
        }
        swap(a[left], a[right]);
        left ++;
        right--;
    }
    swap( a[left], a[hight]);
    return left;
}
void quicksort(int a[], int low , int hight){
    if(low < hight){
        int pi = partition(a,low, hight);
        quicksort(a,low, pi - 1);
        quicksort(a,pi + 1 , hight);
    }
}
void printarr(int a[], int n){
    for (int  i = 0; i < n; i++)
    {
        printf("%d  ", a[i]);
    } 
}
int main(){ 
    int a[]= {9,2,6,4,3,7};
    int n = sizeof(a)/sizeof(a[0]);
    quicksort(a,0,n-1);
    printarr(a,n);
    return 0;
}


Output:
1
2  3  4  6  7  9  


Create a new paste based on this one


Comments: