#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXSIZE 17
int a[MAXSIZE];
void display()
{
for (int i = 0; i < MAXSIZE; i++)
{
printf("%2d ", a[i]);
}
printf("\n");
}
void creatL()
{
srand(time(0));
for (int i = 1; i < MAXSIZE; i++)
{
a[i] = rand() % (MAXSIZE + 50);
}
display();
}
void insertSort()
{
creatL();
for (int i = 1 + 1; i < MAXSIZE; i++)
{
// a[0]=a[i];
// int j=i-1;
// for (; j>0&&a[0]<a[j]; j--)
// {
// a[j+1]=a[j];
// }
// a[j+1]=a[0];
a[0] = a[i];
int low, middle, hight;
low = 1;
hight = i - 1;
while (low <= hight)
{
middle = (low + hight) / 2;
if (a[0] >= a[middle])
{
low = middle + 1;
}
else
{
hight = middle - 1;
}
}
int j = i - 1;
for (; j >= low; j--)
{
a[j + 1] = a[j];
}
a[j + 1] = a[0];
}
display();
}
//希尔排序
void shellsort()
{
creatL();
int step = 1;
while (step < MAXSIZE / 3)
{
step = step * 3 + 1;
}
while (step > 0)
{
for (int i = 1 + step; i < MAXSIZE; i += step)
{
a[0] = a[i];
int j = i - step;
for (; j >= 0 && a[j] > a[0]; j -= step)
{
a[j + step] = a[j];
}
a[j + step] = a[0];
}
step = step / 3;
}
display();
}
void bubblesort()
{
creatL();
for (int i = 1; i < MAXSIZE; i++)
{
for (int j = 0; j < MAXSIZE - i; j++)
{
if (a[j] > a[j + 1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
display();
}
//快速排序
int partion(int low, int hight)
{
a[0] = a[low];
while (low < hight)
{
while (low < hight && a[hight] >= a[0])
{
--hight;
}
a[low] = a[hight];
while (low < hight && a[low] <= a[0])
{
++low;
}
a[hight] = a[low];
}
a[low] = a[0];
display();
return low;
}
void quicksort(int low, int hight)
{
if (low < hight)
{
int p = partion(low, hight);
quicksort(low, p);
quicksort(p + 1, hight);
}
}
void selectsort()
{
creatL();
for (int i = 0; i < MAXSIZE - 1; i++)
{
int min = i;
for (int j = i + 1; j < MAXSIZE; j++)
{
if (a[min] > a[j])
{
min = j;
}
}
if (min != i)
{
a[0] = a[min];
a[min] = a[i];
a[i] = a[0];
}
}
display();
}
//堆排序
//建立大根堆
void buildMaxHeap(int len)
{
void adjustMaxHeap(int, int);
for (int i = len / 2; i > 0; i--)
{
adjustMaxHeap(i, len);
}
}
//调整大根堆
void adjustMaxHeap(int k, int len)
{
a[0] = a[k];
for (int i = 2 * k; i < len; i *= 2)
{
if (i < len - 1 && a[i + 1] > a[i])
{
i++;
}
if (a[0] < a[i])
{
a[k] = a[i];
k = i;
}
else
{
break;
}
}
a[k] = a[0];
}
void heapsort()
{
creatL();
buildMaxHeap(MAXSIZE);
display();
for (int i = MAXSIZE - 1; i > 1; i--)
{
int t = a[i];
a[i] = a[1];
a[1] = t;
buildMaxHeap(i);
}
display();
}
//归并排序
int b[MAXSIZE]; //复制
void merge(int low, int mid, int hight)
{
for (int i = 1; i < MAXSIZE; i++)
{
b[i] = a[i];
}
int i = low, j = mid + 1, k = low;
for (; i <= mid && j <= hight; k++)
{
if (b[i] > b[j])
{
a[k] = b[j++];
}
else
{
a[k] = b[i++];
}
}
while (i <= mid)
{
a[k++] = b[i++];
}
while (j <= hight)
{
a[k++] = b[j++];
}
}
void mergesort(int low, int hight)
{
if (low < hight)
{
int mid = (low + hight) / 2;
mergesort(low, mid);
mergesort(mid + 1, hight);
merge(low, mid, hight);
}
}
//注意:数组中,a[0]存放辅助数据,实际数据为a[1]及后面的
int main()
{
creatL();
mergesort(1, MAXSIZE - 1);
display();
//heapsort();//堆排序
//selectsort();//选择排序
/*creatL();
quicksort(1, MAXSIZE - 1);//快速排序
display();*/
// bubblesort();//冒泡排序
// shellsort();//希尔排序
// insertSort();//插入排序
return 0;
}
内部排序(C语言)
最新推荐文章于 2023-05-23 16:26:59 发布
本文详细介绍了多种排序算法的实现,包括插入排序、希尔排序、冒泡排序、快速排序、选择排序和堆排序,并通过C语言进行了实现。每种算法都有对应的示例代码和排序过程展示,帮助读者深入理解各种排序算法的原理和效率。
摘要由CSDN通过智能技术生成