//
// main.cpp
// Sort
//
// Created by scuhmz on 4/29/15.
// Copyright (c) 2015 com.nwpu.hmz. All rights reserved.
//
#include <iostream>
void swap(int *a,int *b){
int temp = *b;
*b = *a;
*a = temp;
}
int partion(int a[],int p,int r){
int pivot = a[r];
int i = p -1;
for (int j=p; j<=r-1; j++) {
if (a[j] <= pivot) {
i++;
swap(&a[i],&a[j]);
}
}
swap(&a[i+1],&a[r]);
return i+1;
}
void quickSort (int a[],int p,int r){
if (p<r) {
int q = partion(a,p,r);
quickSort(a, p, q-1);
quickSort(a, q+1, r);
}
}
int main(int argc, const char * argv[]) {
int a [] = {100,98,2,0,16,8};
quickSort(a, 0, 5);
for (int i=0; i<6; i++) {
printf("%d ",a[i]);
}
return 0;
}
【算法导论】快速排序
最新推荐文章于 2024-08-04 23:11:18 发布