`

快速排序(c++)

阅读更多

void swap(int list[], int first, int second) {
    int temp = list[first];
    list[first] = list[second];
    list[second] = temp;
}

int partition(int list[], int first, int last) {
    int pivot;
    int index, smallIndex;

    swap(list, first, (first+last)/2);

    pivot = list[first];
    smallIndex  = first;
    for(index=first+1; index<=last; index++)
        if(list[index]<pivot) {
            smallIndex++;
            swap(list, smallIndex, index);
        }

    swap(list, first, smallIndex);

    return smallIndex;
}

void recQuickSort(int list[] , int first, int last) {
    int pivotLocation;

    if(first<last) {
        pivotLocation = partition(list, first, last);
        recQuickSort(list, first, pivotLocation-1);
        recQuickSort(list, pivotLocation+1, last);
    }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics