c语言快速排序完整代码
时间: 2023-12-25 07:29:35 浏览: 77
快速排序c语言代码文件
5星 · 资源好评率100%
以下是C语言实现的快速排序的完整代码:
代码实现1:
```c
void QuickSort(int* a, int left, int right) {
if (left >= right) {
return;
}
int begin = left, end = right;
int keyi = begin;
int prev = begin;
int cur = prev + 1;
while (cur <= end) {
if (a[cur] < a[keyi] && ++prev != cur) {
int temp = a[cur];
a[cur] = a[prev];
a[prev] = temp;
}
cur++;
}
int temp = a[keyi];
a[keyi] = a[prev];
a[prev] = temp;
QuickSort(a, left, prev - 1);
QuickSort(a, prev + 1, right);
}
```
代码实现2:
```c
void quicksort(int a[10], int first, int end) {
if (first > end) {
return;
}
int i = first, j = end, flag = a[i], exchange = 0;
while (i != j) {
while (i < j && a[j] > flag) {
j--;
}
while (i < j && a[i] <= flag) {
i++;
}
if (j > i) {
exchange = a[i];
a[i] = a[j];
a[j] = exchange;
}
}
a[first] = a[i];
a[i] = flag;
quicksort(a, first, i - 1);
quicksort(a, i + 1, end);
}
```
阅读全文