ITeye博客

快速排序(c++)

wanran · 2011-05-21 17:09 · Algorithm · 编程语言 · Markdown



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);
    }
}


评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。