quick sort 简单C++实现

来源:互联网 发布:2016网购大数据 编辑:程序博客网 时间:2024/06/05 04:03
// algorithms.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "iostream"using std::cout;using std::endl;void quickSort(int *a, int start, int end);int partition(int *a, int start, int end);void swap(int &i, int &j);int main(){int a[10] = { 121, 22, 13, 34, 53, 62, 71, 48, 59, 104};quickSort(a, 0, 9);for (size_t i = 0; i < 10; i++){cout << a[i] << " ";}return 0;}void swap(int &i, int &j){int temp = i;i = j;j = temp;}void quickSort(int *a, int start, int end){if (start < end){int q = partition(a, start, end);quickSort(a, start, q - 1);quickSort(a, q + 1, end);}}int partition(int *a, int start, int end){int friendPar = a[end];int i = start - 1;for (int j = start; j <= end - 1;j++){if (a[j]<friendPar){++i;swap(a[j], a[i]);}}swap(a[++i], a[end]);return i;}

0 0
原创粉丝点击