---
title: "快速排序(c++)"
author: "wanran"
date: "2011-05-21T17:09:11Z"
tags: []
categories: ["编程语言"]
slug: "c"
draft: false
lastmod: "2011-05-21T17:09:11Z"
id: 1053485
url: "https://wanran.iteye.com/blog/1053485"
---

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

```