#include <stdio.h>
#include <stdlib.h>
void Print(int * arr,int length)
{
for (int i = 1; i <= length; i++)
printf("%d ",arr[i]);
}
void ShellSort(int* arr, int length)
{
int i, j, d;
for (d = length / 2; d >= 1; d = d / 2)
{
for (i = d + 1; i <= length; i++)
{
if (arr[i] < arr[i - d])
{
arr[0] = arr[i];
for (j = i - d; j > 0 && arr[0] < arr[j]; j = j - d)
arr[j + d] = arr[j];
arr[j + d] = arr[0];
}
}
}
}
int Partition(int* arr, int low, int high)
{
int pivot = arr[low];
while (low < high)
{
while (low < high && arr[high] >= pivot) high--;
arr[low] = arr[high];
while (low < high && arr[low] <= pivot) low++;
arr[high] = arr[low];
}
arr[low] = pivot;
return low;
}
void QuickSort(int *arr,int low,int high)
{
if (low < high)
{
int pivotpos = Partition(arr,low,high);
QuickSort(arr, low, pivotpos-1);
QuickSort(arr, pivotpos+1, high);
}
}
void main()
{
int* arr;
int length;
printf("请输入数组长度");
scanf_s("%d", &length);
arr = (int *)malloc(sizeof(int)*(length+1));
printf("请依次输入数组中的各个数值");
for (int i = 1; i <= length; i++)
scanf_s("%d", &arr[i]);
ShellSort(arr, length);
QuickSort(arr, 1, length);
Print(arr,length);
}
希尔和快排
最新推荐文章于 2024-08-31 15:34:19 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)