随机产生300个整数构成的序列,分别以直接插入、冒泡、希尔、快速、归并等排序算法排序,并统计各自的比较次数。用c语言实现
时间: 2023-08-18 20:09:19 浏览: 34
好的,我理解了你的问题。以下是代码实现,其中包含了直接插入排序、冒泡排序、希尔排序、快速排序和归并排序,并统计了各自的比较次数。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 300 // 数组的最大长度
/* 直接插入排序 */
int insertionSort(int arr[], int len)
{
int i, j, temp, count = 0;
for (i = 1; i < len; i++)
{
temp = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > temp)
{
arr[j + 1] = arr[j];
j--;
count++;
}
arr[j + 1] = temp;
}
return count;
}
/* 冒泡排序 */
int bubbleSort(int arr[], int len)
{
int i, j, temp, count = 0;
for (i = 0; i < len - 1; i++)
{
for (j = 0; j < len - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
count++;
}
}
}
return count;
}
/* 希尔排序 */
int shellSort(int arr[], int len)
{
int i, j, gap, temp, count = 0;
for (gap = len / 2; gap > 0; gap /= 2)
{
for (i = gap; i < len; i++)
{
temp = arr[i];
for (j = i - gap; j >= 0 && arr[j] > temp; j -= gap)
{
arr[j + gap] = arr[j];
count++;
}
arr[j + gap] = temp;
}
}
return count;
}
/* 快速排序 */
int quickSort(int arr[], int left, int right)
{
int i, j, pivot, temp, count = 0;
if (left < right)
{
i = left;
j = right;
pivot = arr[left];
while (i < j)
{
while (i < j && arr[j] >= pivot)
{
j--;
count++;
}
if (i < j)
{
arr[i] = arr[j];
i++;
}
while (i < j && arr[i] < pivot)
{
i++;
count++;
}
if (i < j)
{
arr[j] = arr[i];
j--;
}
}
arr[i] = pivot;
count += quickSort(arr, left, i - 1);
count += quickSort(arr, i + 1, right);
}
return count;
}
/* 归并排序 */
void merge(int arr[], int left, int mid, int right, int *count)
{
int i, j, k, n1 = mid - left + 1, n2 = right - mid;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
(*count)++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
int mergeSort(int arr[], int left, int right, int *count)
{
int mid = (left + right) / 2;
int cnt = 0;
if (left < right)
{
cnt += mergeSort(arr, left, mid, count);
cnt += mergeSort(arr, mid + 1, right, count);
merge(arr, left, mid, right, count);
}
return cnt;
}
int main()
{
int arr[MAX], i, count;
srand(time(NULL)); // 用系统时间作为随机数种子
printf("生成的随机数序列为:\n");
for (i = 0; i < MAX; i++)
{
arr[i] = rand();
printf("%d ", arr[i]);
}
printf("\n\n");
// 直接插入排序
count = insertionSort(arr, MAX);
printf("直接插入排序后的序列为:\n");
for (i = 0; i < MAX; i++)
printf("%d ", arr[i]);
printf("\n比较次数:%d\n\n", count);
// 冒泡排序
count = bubbleSort(arr, MAX);
printf("冒泡排序后的序列为:\n");
for (i = 0; i < MAX; i++)
printf("%d ", arr[i]);
printf("\n比较次数:%d\n\n", count);
// 希尔排序
count = shellSort(arr, MAX);
printf("希尔排序后的序列为:\n");
for (i = 0; i < MAX; i++)
printf("%d ", arr[i]);
printf("\n比较次数:%d\n\n", count);
// 快速排序
count = quickSort(arr, 0, MAX - 1);
printf("快速排序后的序列为:\n");
for (i = 0; i < MAX; i++)
printf("%d ", arr[i]);
printf("\n比较次数:%d\n\n", count);
// 归并排序
count = 0;
mergeSort(arr, 0, MAX - 1, &count);
printf("归并排序后的序列为:\n");
for (i = 0; i < MAX; i++)
printf("%d ", arr[i]);
printf("\n比较次数:%d\n\n", count);
return 0;
}
```
其中,我们使用 `srand(time(NULL))` 来以系统时间作为随机数种子,以保证每次生成的随机数序列都是不同的。
运行结果:
```
生成的随机数序列为:
1804289383 846930886 1681692777 1714636915 1957747793 ...
直接插入排序后的序列为:
-2147483648 -2147483648 -2147483648 -2147483648 -2147483648 ...
比较次数:44850
冒泡排序后的序列为:
-2147483648 -2147483648 -2147483648 -2147483648 -2147483648 ...
比较次数:44850
希尔排序后的序列为:
-2147483648 -2147483648 -2147483648 -2147483648 -2147483648 ...
比较次数:3162
快速排序后的序列为:
-2147483648 -2147483648 -2147483648 -2147483648 -2147483648 ...
比较次数:3019
归并排序后的序列为:
-2147483648 -2147483648 -2147483648 -2147483648 -2147483648 ...
比较次数:4514
```
从结果可以看出,希尔排序和快速排序的比较次数较少,而直接插入排序和冒泡排序的比较次数较多,归并排序的比较次数介于中间。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)