概述
//-------------------
// HeYun 20/5/8
//-------------------
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//直接插入排序,升序
void Insert_Sort(int* arr,int length)
{
int i;
for (i = 1; i < length; i++)
{
int temp = arr[i];
int j;
for (j = i - 1; j >= 0 && arr[j] > temp; j--)
{
arr[j + 1] = arr[j];
}
arr[j + 1] = temp;
}
}
//希尔排序
void Shell_Sort(int* arr, int length)
{
int i, j, k, temp, gap;
for (gap = length / 2; gap >= 1; gap /= 2)
{
for (i = 0; i < gap; i++)
{
for (j = i + gap; j < length; j+=gap)
{
if (arr[j - gap]>arr[j])
{
temp = arr[j];
for (k = j - gap; k >= 0 && arr[k] > temp; k-=gap)
{
arr[k + gap] = arr[k];
}
arr[k + gap] = temp;
}
}
}
}
}
//冒泡排序
void Bubble_Sort(int* arr, int length)
{
int i, j,temp;
for (i = 0; i < length - 1; i++)
{
for (j = 0; j < length - 1 - i; j++)
{
if (arr[j]>arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
//快速排序
void Swap(int* arr,int low,int high)
{
int temp;
temp = arr[low];
arr[low] = arr[high];
arr[high] = temp;
}
int Partition(int* arr, int low, int high)
{
int base = arr[low];//选择基数
while (low < high)
{
while (low < high&&arr[high] >= base)
{
high--;
}
Swap(arr,low,high);
while (low < high&&arr[low] <= base)
{
low++;
}
Swap(arr, low, high);
}
return low;
}
void Quick_Sort(int* arr, int low,int high)
{
if (low < high)
{
int base = Partition(arr, low, high);//确定基数的值,并开始分组进行排序。
Quick_Sort(arr, low, base - 1);
Quick_Sort(arr, base + 1, high);
}
}
//直接选择排序
void Choose_Sort(int* arr, int length)
{
int i, j, imin, temp;
for (i = 0; i < length - 1; i++)
{
imin = i;
for (j = i + 1; j < length; j++)
{
if (arr[j] < arr[imin])
{
imin = j;
}
}
if (i != imin)
{
temp = arr[i];
arr[i] = arr[imin];
arr[imin] = temp;
}
}
}
//堆排序,大根堆
void Heap_Sort(int* arr,int length)
{
int end = length - 1;//指向数组最后一个元素
while (end!=0)
{
while (1)
{
int parent = end / 2 - 1;//指向最后一个双亲结点
int tag=0;
while (parent >= 0)
{
if ((2 * parent + 1 <= end) && arr[parent] < arr[2 * parent + 1])//与左孩子比较
{
tag = 1;
int temp = arr[parent];
arr[parent] = arr[2 * parent + 1];
arr[2 * parent + 1] = temp;
}
if ((2 * parent + 1 <= end) && arr[parent] < arr[2 * parent + 2])//与右孩子比较
{
tag = 1;
int temp = arr[parent];
arr[parent] = arr[2 * parent + 2];
arr[2 * parent + 2] = temp;
}
parent--;//移动,找到向上的一个双亲
}
if (tag == 0)
{
int temp = arr[0];
arr[0] = arr[end];
arr[end] = temp;
break;
}
}
end--;
}
}
//二路归并排序
void Merge_Arr(int* arr, int head1, int tail1, int head2, int tail2)
{
int i = head1, j = head2, k = 0;
int *p = (int *)malloc(sizeof(int)*(tail2-head1+1));
int *point = p;
while (i <= tail1&&j <= tail2)
{
if (arr[i] <= arr[j])
p[k++] = arr[i++];
else
p[k++] = arr[j++];
}
while (i <= tail1)
{
p[k++] = arr[i++];
}
while (j <= tail2)
{
p[k++] = arr[j++];
}
for (k = 0; k <= tail2 - head1; k++)
{
arr[head1 + k] = p[k];
}
free(point);
}
void Merge_Sort(int* arr, int head, int tail)
{
int i = 0, j = 0;
if (head < tail)
{
Merge_Sort(arr, head, (head + tail) / 2);
Merge_Sort(arr, (head + tail) / 2 + 1, tail);
Merge_Arr(arr, head, (head + tail) / 2, (head + tail) / 2 + 1, tail);
}
}
//打印结果
void Print_Arr(int* arr, int length)
{
for (int i = 0; i < length; i++)
{
printf_s("%d ",arr[i]);
}
printf_s("n");
}
int main()
{
int arr[] = { 49, 38, 65, 97, 76, 13, 27, 38, 9, 16 };
int length = sizeof(arr) / sizeof(arr[0]); //求数组长度
int *temp=(int *)malloc(sizeof(int)*(length));
memcpy(temp,arr,sizeof(arr));
Insert_Sort(temp, length);
printf_s("直接插入排序:");
Print_Arr(temp, length);
memcpy(temp, arr, sizeof(arr));
Shell_Sort(temp, length);
printf_s("希尔排序 :");
Print_Arr(temp, length);
memcpy(temp, arr, sizeof(arr));
Bubble_Sort(temp, length);
printf_s("冒泡排序 :");
Print_Arr(temp, length);
memcpy(temp, arr, sizeof(arr));
Quick_Sort(temp, 0, length - 1);
printf_s("快速排序 :");
Print_Arr(temp, length);
memcpy(temp, arr, sizeof(arr));
Choose_Sort(temp, length);
printf_s("直接选择排序:");
Print_Arr(temp, length);
memcpy(temp, arr, sizeof(arr));
Heap_Sort(temp, length);
printf_s("堆排序 :");
Print_Arr(temp, length);
memcpy(temp, arr, sizeof(arr));
Merge_Sort(temp, 0, length - 1);
printf_s("二路归并排序:");
Print_Arr(temp,length);
return 0;
}
最后
以上就是美满皮皮虾为你收集整理的C语言数组各种排序算法的全部内容,希望文章能够帮你解决C语言数组各种排序算法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复