快排优化_1

————参考博客(推荐看这篇博客,非常全)

1.三数取中(也可以5个数甚至更多)

#include<stdio.h>
#define Max 100  //储存的最大值

void Swap(int *A,int *B) {
	int temp = *A;
	*A = *B;
	*B = temp;
}

int NumberOfThree(int arr[], int head, int end)
{
	int mid = head + ((end - head) / 2);

	if (arr[mid] > arr[end])
	{
		Swap(&arr[mid], &arr[end]);
	}
	if (arr[head] > arr[end])
	{
		Swap(&arr[head], &arr[end]);
	}
	if (arr[mid] > arr[head])
	{
		Swap(&arr[mid], &arr[head]);
	}
	//此时,arr[mid] <= arr[head] <= arr[end]
	return arr[head];
}

int Sort(int A[], int head, int end) {
	int Standard = NumberOfThree(A, head, end);       //标准值为最左边的数
	while (head < end) {
		while (A[end] >= Standard && head < end)
			end--;
		A[head] = A[end];
		while (A[head] <= Standard && head < end)
			head++;
		A[end] = A[head];
	}
	A[head] = Standard;
	return end;
}

void Circle(int A[], int head, int end) {
	if (head < end) {
		int temp;
		temp = Sort(A, head, end);
		Circle(A, head, temp - 1);
		Circle(A, temp + 1, end);
	}
}

void Print(int A[], int n) {
	for (int i = 0; i < n; i++)
		printf("%d\t", A[i]);
}

int main() {
	int n, A[Max];
	printf("请输入n:\n");
	scanf_s("%d", &n);
	for (int i = 0; i < n; i++)
		scanf_s("%d", A + i);
	Circle(A, 0, n - 1);
	Print(A, n);
}

2.随机数产生(代码与上类似,只不过标准的取值使用随机数函数)

3.序列长度达到一定大小时,使用插入排序

i.由《数据结构与算法分析》(Mark Allen Weiness所著)可知,当待排序列长度为5~20之间,此时使用插入排序能避免一些有害的退化情形。

ii.插入排序

void insertion_sort(int arr[], int len){
        int i,j,key;
        for (i=1;i<len;i++){
                key = arr[i];
                j=i-1;
                while((j>=0) && (arr[j]>key)) {
                        arr[j+1] = arr[j];
                        j--;
                }
                arr[j+1] = key;
        }
}

iii.完整代码

#include<stdio.h>
#define Max 100  //储存的最大值

void InsertSort(int A[], int n) {
	for (int i = 1; i < n; i++) {
		int key = A[i];
		int j = i - 1;
		while (j >= 0 && A[j] > key)
		{
			A[j + 1] = A[j];
			j--;
		}
		A[j + 1] = key;
	}
}

void Swap(int *A,int *B) {
	int temp = *A;
	*A = *B;
	*B = temp;
}

int NumberOfThree(int arr[], int head, int end)
{
	int mid = head + ((end - head) / 2);

	if (arr[mid] > arr[end])
	{
		Swap(&arr[mid], &arr[end]);
	}
	if (arr[head] > arr[end])
	{
		Swap(&arr[head], &arr[end]);
	}
	if (arr[mid] > arr[head])
	{
		Swap(&arr[mid], &arr[head]);
	}
	//此时,arr[mid] <= arr[head] <= arr[end]
	return arr[head];
}

int Sort(int A[], int head, int end) {
	int Standard = NumberOfThree(A, head, end);       //标准值为最左边的数
	while (head < end) {
		while (A[end] >= Standard && head < end)
			end--;
		A[head] = A[end];
		while (A[head] <= Standard && head < end)
			head++;
		A[end] = A[head];
	}
	A[head] = Standard;
	return end;
}

void Circle(int A[], int head, int end) {
	if (end - head + 1 < 10)
		InsertSort(A, end - head + 1);
	else {
		if (head < end) {
			int temp;
			temp = Sort(A, head, end);
			Circle(A, head, temp - 1);
			Circle(A, temp + 1, end);
		}
	}
}

void Print(int A[], int n) {
	for (int i = 0; i < n; i++)
		printf("%d\t", A[i]);
}

int main() {
	int n, A[Max];
	printf("请输入n:\n");
	scanf_s("%d", &n);
	for (int i = 0; i < n; i++)
		scanf_s("%d", A + i);
	Circle(A, 0, n - 1);
	Print(A, n);
}

posted @ 2022-05-01 14:57  彭乐祥  阅读(225)  评论(0编辑  收藏  举报