1.定义待排序表的长度为n。 2.初始化随机数发生器,产生n个随机数构成的顺序表。 3.创建输出排序表函数,取得系统时间函数,直接插入排序函数,冒泡排序函数,快速排序函数,直接选择排序函数,筛选函数,对排序函数,归并排序函数. 4.主程序调用各个排序函数,输出各排序算法对长度为n的顺序表进行排序所耗费的时间。用c语言写
时间: 2023-12-10 15:38:22 浏览: 94
下面是用C语言实现对长度为n的顺序表进行各种排序算法并输出排序所耗费时间的程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXSIZE 10000 //顺序表最大长度
//产生随机数填充顺序表
void initList(int list[], int n)
{
int i;
srand(time(NULL)); //初始化随机数发生器
for (i = 0; i < n; i++)
list[i] = rand() % 100; //产生0~99的随机数
}
//输出排序后的顺序表
void printList(int list[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", list[i]);
printf("\n");
}
//直接插入排序
void insertSort(int list[], int n)
{
int i, j, temp;
for (i = 1; i < n; i++)
{
temp = list[i];
j = i - 1;
while (j >= 0 && list[j] > temp)
{
list[j + 1] = list[j];
j--;
}
list[j + 1] = temp;
}
}
//冒泡排序
void bubbleSort(int list[], int n)
{
int i, j, temp;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1 - i; j++)
{
if (list[j] > list[j + 1])
{
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
}
//快速排序
void quickSort(int list[], int left, int right)
{
int i, j, pivot, temp;
if (left < right)
{
i = left;
j = right;
pivot = list[left];
while (i < j)
{
while (i < j && list[j] >= pivot)
j--;
if (i < j)
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
while (i < j && list[i] <= pivot)
i++;
if (i < j)
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
list[i] = pivot;
quickSort(list, left, i - 1);
quickSort(list, i + 1, right);
}
}
//直接选择排序
void selectSort(int list[], int n)
{
int i, j, k, temp;
for (i = 0; i < n - 1; i++)
{
k = i;
for (j = i + 1; j < n; j++)
{
if (list[j] < list[k])
k = j;
}
if (k != i)
{
temp = list[i];
list[i] = list[k];
list[k] = temp;
}
}
}
//堆排序
void heapSort(int list[], int n)
{
int i, temp;
for (i = n / 2 - 1; i >= 0; i--)
adjustHeap(list, i, n);
for (i = n - 1; i > 0; i--)
{
temp = list[0];
list[0] = list[i];
list[i] = temp;
adjustHeap(list, 0, i);
}
}
void adjustHeap(int list[], int i, int n)
{
int j, temp;
temp = list[i];
j = 2 * i + 1;
while (j < n)
{
if (j + 1 < n && list[j] < list[j + 1])
j++;
if (list[j] <= temp)
break;
list[i] = list[j];
i = j;
j = 2 * i + 1;
}
list[i] = temp;
}
//归并排序
void mergeSort(int list[], int left, int right, int temp[])
{
int mid, i, j, k;
if (left < right)
{
mid = (left + right) / 2;
mergeSort(list, left, mid, temp);
mergeSort(list, mid + 1, right, temp);
i = left;
j = mid + 1;
k = 0;
while (i <= mid && j <= right)
{
if (list[i] <= list[j])
temp[k++] = list[i++];
else
temp[k++] = list[j++];
}
while (i <= mid)
temp[k++] = list[i++];
while (j <= right)
temp[k++] = list[j++];
for (i = 0; i < k; i++)
list[left + i] = temp[i];
}
}
//筛选排序
void shellSort(int list[], int n)
{
int i, j, gap, temp;
for (gap = n / 2; gap > 0; gap /= 2)
{
for (i = gap; i < n; i++)
{
temp = list[i];
for (j = i - gap; j >= 0 && list[j] > temp; j -= gap)
list[j + gap] = list[j];
list[j + gap] = temp;
}
}
}
int main()
{
int list[MAXSIZE], temp[MAXSIZE];
int n = 10000; //待排序表的长度
clock_t start, end;
double duration;
initList(list, n);
printf("原始顺序表:\n");
printList(list, n);
start = clock();
insertSort(list, n);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("直接插入排序后的顺序表:\n");
printList(list, n);
printf("直接插入排序所耗费的时间为:%lf秒\n", duration);
initList(list, n);
start = clock();
bubbleSort(list, n);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("冒泡排序后的顺序表:\n");
printList(list, n);
printf("冒泡排序所耗费的时间为:%lf秒\n", duration);
initList(list, n);
start = clock();
quickSort(list, 0, n - 1);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("快速排序后的顺序表:\n");
printList(list, n);
printf("快速排序所耗费的时间为:%lf秒\n", duration);
initList(list, n);
start = clock();
selectSort(list, n);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("直接选择排序后的顺序表:\n");
printList(list, n);
printf("直接选择排序所耗费的时间为:%lf秒\n", duration);
initList(list, n);
start = clock();
heapSort(list, n);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("堆排序后的顺序表:\n");
printList(list, n);
printf("堆排序所耗费的时间为:%lf秒\n", duration);
initList(list, n);
start = clock();
mergeSort(list, 0, n - 1, temp);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("归并排序后的顺序表:\n");
printList(list, n);
printf("归并排序所耗费的时间为:%lf秒\n", duration);
initList(list, n);
start = clock();
shellSort(list, n);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("希尔排序后的顺序表:\n");
printList(list, n);
printf("希尔排序所耗费的时间为:%lf秒\n", duration);
return 0;
}
```
程序中定义了一个`MAXSIZE`常量表示顺序表的最大长度,`initList()`函数用于产生随机数填充顺序表,`printList()`函数用于输出排序后的顺序表。接下来分别实现了直接插入排序、冒泡排序、快速排序、直接选择排序、堆排序、归并排序和希尔排序算法。主程序调用各个排序函数,输出各排序算法对长度为n的顺序表进行排序所耗费的时间。
阅读全文