用C语言 1.A数组中存储500个2000以内的随机整数,完成以下操作: (1)对A数组中的元素进行直接插入排序,显示排序所用时间; (2)对A数组中的元素进行希尔排序,显示排序所用时间; (3)对A数组中的元素进行起泡排序,显示排序所用时间; (4)对A数组中的元素进行快速排序,显示排序所用时间。部分参考代码如下: #include <windows.h> …… int main() { LARGE_INTEGER freq, start, end; QueryPerformanceFrequency(&freq); //获取CPU的时钟频率(HZ) QueryPerformanceCounter(&start); //获取开始时钟计数 调用对应查找算法 QueryPerformanceCounter(&end); //获取结束时钟计数 double us = (end.QuadPart - start.QuadPart)*1000000.0 / freq.QuadPart; cout << "执行时间:" << us << " 微秒" << endl; //输入算法运行时间 …… }
时间: 2023-06-27 20:01:54 浏览: 109
实参、形参是指针-c程序设计数组与指针
以下是完整的程序代码,包括四种排序算法的实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define MAX_NUM 500
#define MAX_VAL 2000
void insert_sort(int arr[], int len);
void shell_sort(int arr[], int len);
void bubble_sort(int arr[], int len);
void quick_sort(int arr[], int left, int right);
int partition(int arr[], int left, int right);
int main()
{
int arr[MAX_NUM];
srand(time(NULL));
for (int i = 0; i < MAX_NUM; i++) {
arr[i] = rand() % MAX_VAL + 1;
}
LARGE_INTEGER freq, start, end;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);
insert_sort(arr, MAX_NUM);
QueryPerformanceCounter(&end);
double us = (end.QuadPart - start.QuadPart) * 1000000.0 / freq.QuadPart;
printf("直接插入排序执行时间:%.2f微秒\n", us);
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);
shell_sort(arr, MAX_NUM);
QueryPerformanceCounter(&end);
us = (end.QuadPart - start.QuadPart) * 1000000.0 / freq.QuadPart;
printf("希尔排序执行时间:%.2f微秒\n", us);
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);
bubble_sort(arr, MAX_NUM);
QueryPerformanceCounter(&end);
us = (end.QuadPart - start.QuadPart) * 1000000.0 / freq.QuadPart;
printf("冒泡排序执行时间:%.2f微秒\n", us);
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);
quick_sort(arr, 0, MAX_NUM - 1);
QueryPerformanceCounter(&end);
us = (end.QuadPart - start.QuadPart) * 1000000.0 / freq.QuadPart;
printf("快速排序执行时间:%.2f微秒\n", us);
return 0;
}
void insert_sort(int arr[], int len)
{
int i, j, temp;
for (i = 1; i < len; i++) {
temp = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > temp) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
void shell_sort(int arr[], int len)
{
int gap, i, j, temp;
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];
}
arr[j + gap] = temp;
}
}
}
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;
}
}
}
}
void quick_sort(int arr[], int left, int right)
{
if (left < right) {
int pivot = partition(arr, left, right);
quick_sort(arr, left, pivot - 1);
quick_sort(arr, pivot + 1, right);
}
}
int partition(int arr[], int left, int right)
{
int pivot = arr[left];
while (left < right) {
while (left < right && arr[right] >= pivot) {
right--;
}
arr[left] = arr[right];
while (left < right && arr[left] <= pivot) {
left++;
}
arr[right] = arr[left];
}
arr[left] = pivot;
return left;
}
```
注意:程序使用了Windows系统提供的`QueryPerformanceCounter()`函数来计时,需要在编译时链接`kernel32.lib`库。另外,为了方便起见,本程序中的排序算法都是采用升序排序。
阅读全文