快速排序算法C++实现
时间: 2023-11-08 20:04:59 浏览: 98
sort_algorithm_快速排序C++_
快速排序是一种常用的排序算法,它的C实现如下:
```C
#include <iostream>
using namespace std;
void QuickSort(int *array, int low, int high) {
if (low >= high) {
return;
}
int i = low;
int j = high;
int key = array[low];
while (i < j) {
while (array[j] >= key && i < j) {
j--;
}
array[i] = array[j];
while (array[i] <= key && i < j) {
i++;
}
array[j] = array[i];
}
array[i] = key;
QuickSort(array, low, i - 1);
QuickSort(array, i + 1, high);
}
int main() {
int array[] = {49, 38, 65, 97, 76, 13, 27, 49};
int length = sizeof(array) / sizeof(*array);
cout << "原始序列:";
for (int i = 0; i < length; i++) {
cout << array[i] << " ";
}
cout << endl;
QuickSort(array, 0, length - 1);
cout << "快排序列:";
for (int i = 0; i < length; i++) {
cout << array[i] << " ";
}
return 0;
}
```
运行示例:
原始序列:49 38 65 97 76 13 27 49
快排序列:13 27 38 49 49 65 76 97
阅读全文