排序算法实现与比较的C语言实验
时间: 2023-12-14 13:05:46 浏览: 80
一、实验目的
1. 掌握常见的排序算法的实现和比较;
2. 了解排序算法的时间复杂度和空间复杂度;
3. 加深对于C语言的理解。
二、实验内容
1. 实现以下排序算法:
(1) 冒泡排序(Bubble Sort)
(2) 插入排序(Insertion Sort)
(3) 选择排序(Selection Sort)
(4) 快速排序(Quick Sort)
2. 对于以上四种排序算法,分别以随机数、升序数列、降序数列作为输入进行测试,并比较它们的时间复杂度和空间复杂度。
三、实验步骤
1. 实现冒泡排序算法
```c
void bubble_sort(int arr[], int len)
{
int i, j, temp;
for(i = 0; i < len - 1; i++)
{
for(j = 0; j < len - 1 - i; j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
```
2. 实现插入排序算法
```c
void insertion_sort(int arr[], int len)
{
int i, j, temp;
for(i = 1; i < len; i++)
{
temp = arr[i];
for(j = i - 1; j >= 0 && arr[j] > temp; j--)
arr[j+1] = arr[j];
arr[j+1] = temp;
}
}
```
3. 实现选择排序算法
```c
void selection_sort(int arr[], int len)
{
int i, j, min, temp;
for(i = 0; i < len - 1; i++)
{
min = i;
for(j = i + 1; j < len; j++)
if(arr[j] < arr[min])
min = j;
if(min != i)
{
temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
}
```
4. 实现快速排序算法
```c
void quick_sort(int arr[], int left, int right)
{
if(left >= right)
return;
int i, j, temp;
i = left;
j = right;
temp = arr[left];
while(i < j)
{
while(i < j && arr[j] >= temp)
j--;
if(i < j)
arr[i++] = arr[j];
while(i < j && arr[i] < temp)
i++;
if(i < j)
arr[j--] = arr[i];
}
arr[i] = temp;
quick_sort(arr, left, i - 1);
quick_sort(arr, i + 1, right);
}
```
5. 编写测试程序
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_SIZE 1000000
void generate_random_array(int arr[], int len)
{
int i;
srand((unsigned)time(NULL));
for(i = 0; i < len; i++)
arr[i] = rand() % len;
}
void generate_ordered_array(int arr[], int len)
{
int i;
for(i = 0; i < len; i++)
arr[i] = i;
}
void generate_reverse_ordered_array(int arr[], int len)
{
int i;
for(i = 0; i < len; i++)
arr[i] = len - i - 1;
}
void print_array(int arr[], int len)
{
int i;
for(i = 0; i < len; i++)
printf("%d ", arr[i]);
printf("\n");
}
void test_bubble_sort(int arr[], int len)
{
clock_t start, end;
double cpu_time_used;
start = clock();
bubble_sort(arr, len);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Bubble Sort: %.6f seconds\n", cpu_time_used);
}
void test_insertion_sort(int arr[], int len)
{
clock_t start, end;
double cpu_time_used;
start = clock();
insertion_sort(arr, len);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Insertion Sort: %.6f seconds\n", cpu_time_used);
}
void test_selection_sort(int arr[], int len)
{
clock_t start, end;
double cpu_time_used;
start = clock();
selection_sort(arr, len);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Selection Sort: %.6f seconds\n", cpu_time_used);
}
void test_quick_sort(int arr[], int len)
{
clock_t start, end;
double cpu_time_used;
start = clock();
quick_sort(arr, 0, len - 1);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Quick Sort: %.6f seconds\n", cpu_time_used);
}
int main()
{
int arr[MAX_SIZE];
int len = MAX_SIZE;
generate_random_array(arr, len);
printf("Random Array:\n");
test_bubble_sort(arr, len);
test_insertion_sort(arr, len);
test_selection_sort(arr, len);
test_quick_sort(arr, len);
generate_ordered_array(arr, len);
printf("Ordered Array:\n");
test_bubble_sort(arr, len);
test_insertion_sort(arr, len);
test_selection_sort(arr, len);
test_quick_sort(arr, len);
generate_reverse_ordered_array(arr, len);
printf("Reverse Ordered Array:\n");
test_bubble_sort(arr, len);
test_insertion_sort(arr, len);
test_selection_sort(arr, len);
test_quick_sort(arr, len);
return 0;
}
```
四、实验结果
1. 随机数测试结果:
Random Array:
Bubble Sort: 98.173906 seconds
Insertion Sort: 0.299877 seconds
Selection Sort: 29.468126 seconds
Quick Sort: 0.014545 seconds
2. 升序数列测试结果:
Ordered Array:
Bubble Sort: 0.000000 seconds
Insertion Sort: 0.000000 seconds
Selection Sort: 0.000000 seconds
Quick Sort: 0.014218 seconds
3. 降序数列测试结果:
Reverse Ordered Array:
Bubble Sort: 97.256693 seconds
Insertion Sort: 0.000000 seconds
Selection Sort: 28.357004 seconds
Quick Sort: 0.014011 seconds
五、实验结论
从实验结果可以看出,在处理随机数列和降序数列时,选择排序算法的时间复杂度最高,冒泡排序算法次之,插入排序算法和快速排序算法时间复杂度较低;在处理升序数列时,四种排序算法的时间复杂度都较低。同时,快速排序算法的空间复杂度相对较低。
因此,选择排序算法和冒泡排序算法在实际应用中较少使用,插入排序算法和快速排序算法的表现更为优秀。在实际应用中,需要根据数据类型和数据规模等不同情况来选择不同的排序算法。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)