C++快速排序
#pragma warning (disable:4996)
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include <iomanip>
#include <stdio.h>
#include <vector>
#include "list_node.h"
using namespace std;
void printArr(const int(&a)[10], int size) {
for (int i = 0; i < size; i++) {
cout << a[i] << '\t';
}
cout << endl;
}
void swapNum(int(&a)[10], int b, int c) {
int temp = a[b];
a[b] = a[c];
a[c] = temp;
}
int partition(int(&a)[10], int start, int end) {
int pivot = a[start];
int left = start, right = end;
while (left < right) {
while (a[left] <= pivot && left < right) {
left++;
}
while (a[right] >= pivot && left < right) {
right--;
}
swapNum(a, left, right);
}
if (a[left] < pivot) {
swapNum(a, start, left);
return left;
}
if (a[left] > pivot) {
swapNum(a, start, left - 1);
return left - 1;
}
}
void quickSort(int(&a)[10], int start, int end) {
if (start >= end) {
return;
}
int pivotNum = partition(a, start, end);
quickSort(a, start, pivotNum - 1);
quickSort(a, pivotNum + 1, end);
}
int main(void) {
int arr[10] = { 23, 45, 36, 11, 12, 16, 18, 56, 11, 20 };
int size = sizeof(arr) / sizeof(arr[0]);
printArr(arr,size);
quickSort(arr, 0, size - 1);
printArr(arr, size);
system("pause");
return 0;
}