c语言实现本实验完成直接插入、冒泡、直接选择、快速、堆、归并等排序算法,并根据运行时间比较各种排序算法的运行速度,采用随机数发生器产生待排序整型数据,值的范围在0到n*10之间,排序长度默认20),编写各种排序程序记录运行速度并做对比分析。1.定义待排序表的长度为n。 2.初始化随机数发生器,产生n个随机数构成的顺序表。 3.创建输出排序表函数,取得系统时间函数,直接插入排序函数,冒泡排序函数,快速排序函数,直接选择排序函数,筛选函数,对排序函数,归并排序函数。 4.主程序调用各个排序函数,输出各排序算法对长度为n的顺序表进行排序所耗费的时间。
时间: 2024-02-05 15:12:36 浏览: 71
下面是C语言实现排序算法的示例代码,包括直接插入排序、冒泡排序、直接选择排序、快速排序、堆排序和归并排序。同时,程序会记录各种排序算法的运行速度并做对比分析。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 20 // 排序长度
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
void directInsertionSort(int arr[], int n) {
clock_t start = clock();
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j+1] = arr[j];
j--;
}
arr[j+1] = key;
}
printf("直接插入排序用时:%lf s\n", (double)(clock()-start)/CLOCKS_PER_SEC);
printArray(arr, n);
}
void bubbleSort(int arr[], int n) {
clock_t start = clock();
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("冒泡排序用时:%lf s\n", (double)(clock()-start)/CLOCKS_PER_SEC);
printArray(arr, n);
}
void selectionSort(int arr[], int n) {
clock_t start = clock();
for (int i = 0; i < n-1; i++) {
int minIndex = i;
for (int j = i+1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
printf("直接选择排序用时:%lf s\n", (double)(clock()-start)/CLOCKS_PER_SEC);
printArray(arr, n);
}
int partition(int arr[], int left, int right) {
int pivot = arr[right];
int i = left - 1;
for (int j = left; j < right; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i+1];
arr[i+1] = arr[right];
arr[right] = temp;
return i+1;
}
void quickSort(int arr[], int left, int right) {
if (left < right) {
int pivotIndex = partition(arr, left, right);
quickSort(arr, left, pivotIndex-1);
quickSort(arr, pivotIndex+1, right);
}
}
void heapSort(int arr[], int n) {
clock_t start = clock();
for (int i = n/2-1; i >= 0; i--) {
int parent = i;
int child = 2*parent+1;
while (child < n) {
if (child+1 < n && arr[child+1] > arr[child]) {
child++;
}
if (arr[child] > arr[parent]) {
int temp = arr[child];
arr[child] = arr[parent];
arr[parent] = temp;
} else {
break;
}
parent = child;
child = 2*parent+1;
}
}
for (int i = n-1; i >= 1; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
int parent = 0;
int child = 2*parent+1;
while (child < i) {
if (child+1 < i && arr[child+1] > arr[child]) {
child++;
}
if (arr[child] > arr[parent]) {
int temp = arr[child];
arr[child] = arr[parent];
arr[parent] = temp;
} else {
break;
}
parent = child;
child = 2*parent+1;
}
}
printf("堆排序用时:%lf s\n", (double)(clock()-start)/CLOCKS_PER_SEC);
printArray(arr, n);
}
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) {
L[i] = arr[left+i];
}
for (int j = 0; j < n2; j++) {
R[j] = arr[mid+j+1];
}
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
}
}
while (i < n1) {
arr[k++] = L[i++];
}
while (j < n2) {
arr[k++] = R[j++];
}
}
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right-left)/2;
mergeSort(arr, left, mid);
mergeSort(arr, mid+1, right);
merge(arr, left, mid, right);
}
}
int main() {
int arr[N];
srand((unsigned)time(NULL));
for (int i = 0; i < N; i++) {
arr[i] = rand() % (N*10);
}
printf("原始数组:\n");
printArray(arr, N);
int arr1[N], arr2[N], arr3[N], arr4[N], arr5[N], arr6[N];
for (int i = 0; i < N; i++) {
arr1[i] = arr[i];
arr2[i] = arr[i];
arr3[i] = arr[i];
arr4[i] = arr[i];
arr5[i] = arr[i];
arr6[i] = arr[i];
}
directInsertionSort(arr1, N);
bubbleSort(arr2, N);
selectionSort(arr3, N);
clock_t start = clock();
quickSort(arr4, 0, N-1);
printf("快速排序用时:%lf s\n", (double)(clock()-start)/CLOCKS_PER_SEC);
printArray(arr4, N);
heapSort(arr5, N);
mergeSort(arr6, 0, N-1);
return 0;
}
```
程序会先生成一个长度为N的随机数组,然后对其进行六种排序算法的排序,并输出排序后的结果以及每种排序算法所用的时间。可以在main函数中修改N的值来改变排序长度。
阅读全文