#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
void MyPrint(int* a, int size) {
for (int i = 0; i < size; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
//三数取中
int takeMiddle(int *a,int left,int right) {
int mid = left + (right - left) / 2; // 防止溢出
if (a[left] > a[mid]) {
if (a[mid] > a[right]) {
return right;
}
else if (a[left] < a[right]) {
return left;
}
else {
return mid;
}
}
else {
if (a[mid] < a[right]) {
return mid;
}
else if (a[left] > a[right]) {
return right;
}
else {
return left;
}
}
}
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
//坑位法(快速排序)//思路
void QuickSort_test(int* a, int size) {
//设计单趟排序
int begin = 0;
int end = size - 1;
int pivot = begin;
int key = a[begin];
while (begin < end) {
while (begin < end && a[end] > key) {
end--;
}
a[pivot] = a[end];
pivot = end;
while (begin < end && a[begin] < key) {
begin++;
}
a[pivot] = a[begin];
pivot = begin;
}
a[begin] = key;
}
//坑位法(快速排序)//最终(递归法)
void QuickSort(int* a, int left, int right) {
//递归的结束条件
if (left >= right) {
return;
}
int keysub = takeMiddle(a, left, right);
swap(&a[left], &a[keysub]);
int begin = left;
int end = right;
int pivot = begin;
int key = a[begin];
while (begin < end) { //单趟
while (begin < end && a[end] > key) {
end--;
}
a[pivot] = a[end];
pivot = end;
while (begin < end && a[begin] < key) {
begin++;
}
a[pivot] = a[begin];
pivot = begin;
}
pivot = begin;
a[begin] = key;
//递归,分治
QuickSort(a, left, pivot - 1);
QuickSort(a, pivot + 1, right);
}
//双指针法
void QuickSort2(int *a,int left,int right) {
//递归的结束条件
if (left >= right) {
return;
}
int begin = left;
int end = right;
int key = a[begin];
int keyi = begin;
while (begin < end) {
while (begin < end && a[end] >= key) {
end--;
}
while (begin<end && a[begin]<=key) {
begin++;
}
swap(&a[begin], &a[end]);
}
swap(&a[begin], &a[keyi]);
QuickSort2(a, left,begin - 1);
QuickSort2(a, begin + 1, right);
}
//前后指针法
void QuickSort3(int* a, int left, int right) {
//递归的结束条件
if (left >= right) {
return;
}
int prev = left;
int cur = left + 1;
int key = left;
while (cur <= right) {
if (a[cur] < a[key]) {
++prev;
swap(&a[cur], &a[prev]);
}
cur++;
}
swap(&a[key],& a[prev]);
QuickSort3(a, left, prev - 1);
QuickSort3(a, prev + 1, right);
}
//前后指针法优化
void QuickSort3_1(int* a, int left, int right) {
//递归的结束条件
if (left >= right) {
return;
}
int prev = left;
int cur = left + 1;
int key = left;
while (cur <= right) {
if (a[cur] < a[key]&&++prev!=cur) { //自己和自己就没必要交换了
swap(&a[cur], &a[prev]);
}
cur++;
}
swap(&a[key], &a[prev]);
QuickSort3(a, left, prev - 1);
QuickSort3(a, prev + 1, right);
}
void test() {
int arr[] = { 6,1,2,7,9,3,4,5,10,8 };
int size = sizeof(arr) / sizeof(arr[0]);
//QuickSort(arr, 0, size - 1);
//QuickSort2(arr, 0, size - 1);
QuickSort3(arr, 0, size - 1);
MyPrint(arr, size);
}
int main() {
test();
return 0;
}
m0_70960708
- 粉丝: 640
- 资源: 2809
最新资源
- 人和箱子检测2-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 清华大学2022年秋季学期 高等数值分析课程报告
- GEE错误集-Cannot add an object of type <Element> to the map. Might be fixable with an explicit .pdf
- 清华大学2022年秋季学期 高等数值分析课程报告
- 矩阵与线程的对应关系图
- 人体人员检测46-YOLO(v5至v9)、COCO、Darknet、TFRecord数据集合集.rar
- GEMM优化代码实现1
- java实现的堆排序 含代码说明和示例.docx
- 资料阅读器(先下载解压) 5.0.zip
- 人、垃圾、非垃圾检测18-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈