快速排序

C++实现快速排序

int Partition(int a[], int first, int end) {
    int i = first;
    int j = end;
    int temp;
    while (i < j) {
        while (i < j && a[i] <= a[j]) {
            j--;
        }
        
        if (i < j) {
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
            i++;
        }
        
        while (i < j && a[i] <= a[j]) {
            i++;
        }
        
        if (i < j) {
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
            j--;
        }
    }
    
    return i;
    
}

void QuickSort(int a[], int first, int end) {
    int position;
    if (first < end) {
        position = Partition(a, first, end);
        QuickSort(a, first, position -1);
        QuickSort(a, position + 1, end);
    }
}
点赞