mirror of
https://github.com/xusun0623/exam_code_for_408.git
synced 2026-02-03 18:34:11 +08:00
41 lines
833 B
C++
41 lines
833 B
C++
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <cstring>
|
|
|
|
void printArr(int a[], int n) {
|
|
for (int i = 0; i < n; i++) {
|
|
printf("%d ", a[i]);
|
|
}
|
|
}
|
|
|
|
int Partition(int A[], int low, int high) {
|
|
int pivot = A[low]; //ѡȡµÚÒ»¸ö×÷ΪÊàÖá
|
|
while (low < high) {
|
|
while (low < high && A[high] >= pivot)
|
|
high--;
|
|
A[low] = A[high];
|
|
while (low < high && A[low] <= pivot)
|
|
low++;
|
|
A[high] = A[low];
|
|
}
|
|
A[low] = pivot;
|
|
return low;
|
|
}
|
|
void QuickDivideSort(int A[], int low, int high, int n) {
|
|
if (low < high) {
|
|
int pivot = Partition(A, low, high);
|
|
if (pivot < n / 2)
|
|
QuickDivideSort(A, pivot + 1, high, n);
|
|
else
|
|
QuickDivideSort(A, low, pivot - 1, n);
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
int sort[] = { 2, 3, 3, 5, 6, 7, 1, 1, 1, 1, 1 };
|
|
int n = sizeof(sort) / 4;
|
|
QuickDivideSort(sort, 0, n - 1, n);
|
|
printArr(sort, 11);
|
|
return 0;
|
|
}
|