ProAnswers.org

algorithm Quick Sort

algorithm Quick Sort

#define SWAP(x, y, t) ((t) = (x), (x) = (y), (y) = (t))

	void quicksort(int list[], int first, int last){

	    int q;

	    if(first >= last)

	                return;

	    q = partition(list, first, last);

	    quicksort(list, first, q - 1);

	    quicksort(list, q + 1, last);

	}

	 

	int partition(int list[], int first, int last){

	    int i, j, x, t;

	    x = list[last];

	    i = first - 1;

	    for(j = first; j < last; j++){

	        if(list[j] <= x){

	            i++;

	            SWAP(list[i], list[j], t);

	        }

	    }

	    SWAP(list[i + 1], list[last], t);

	    return i + 1;

	}