C Language Fast Sorting Algorithm

C Language Fast Sorting Algorithm
// Swap i and j positions in the array
void swap(int v[], int i, int j)
{
    int temp;
    temp = v[i];
    v[i] = v[j];
    v[j] = temp;
}

// Quick sort algorithm
void qsort(int v[], int left, int right)
{
    int i, last;

	/* If the array has less than 2 elements, */
    if (left >= right) 
        return; 

    swap(v, left, (left + right)/2); 
    last = left;                     

    for (i = left + 1; i <= right; i++){
        if (v[i] < v[left])
            swap(v, ++last, i);
	}
	
    swap(v, left, last); 
    qsort(v, left, last-1);
    qsort(v, last+1, right);
}
Source:ww‮uttual.w‬ri.com
Created Time:2017-08-29 10:01:06  Author:lautturi