下面的代码只需要建立一个txt文件存储随机关键字即可,路径自己定义修改即可
运行结果



源文件
#include"堆排序的堆栈.h"
int FindMin(List list, int startIndex)
{
int i, minIndex = startIndex;
for (i = startIndex+1; i < list.n; i++)
{
if (list.D[i].key < list.D[minIndex].key)
{
minIndex = i;
}
}
return minIndex;
}
void swap(Entry * D, int i, int j)
{
Entry temp;
if (i == j)
return;
temp = *(D + i);
*(D + i) = *(D + j);
*(D + j) = temp;
}
void SelectionSort(List* list)
{
int minIndex=0, startIndex = 0;
while (startIndex < list->n - 1)
{
minIndex = FindMin(*list, startIndex);
swap(list->D, startIndex, minIndex);
startIndex++;
}
printf("\n简单排序后的list为:\n");
}
void InsertSort(List* list)
{
int i, j;
Entry insertItem;
for (i = 1; i < list->n; i++)
{
insertItem = list->D[i];
for (j = i - 1; j >= 0; j--)
{
if (insertItem.key < list->D[j].key)
list->D[j + 1] = list->D[j];
else
break;
}
list->D[j + 1] = insertItem;
}
printf("\n直接插入排序后的list为:\n");
}
void BubbleSort(List* list)
{
int i, j;
for (i = list->n - 1; i > 0; i--)
{
BOOL isSwap = FALSE;
for (j = 0; j < i; j++)
{
if (list->D[j].key > list->D[j + 1].key)
{
swap(list->D, j, j + 1);
isSwap = TRUE;
}
}
if (!isSwap)
break;
}
printf("\n冒泡排序后的list为:\n");
}
int Partition(List* list, int low, int high)
{
int i = low, j = high+1;
Entry pivot = list->D[low];
do
{
do
{
i++;
} while (i <= high && list->D[i].key < pivot.key);
do
{
j--;
} while (list->D[j].key > pivot.key);
if (i < j)
swap(list->D, i, j);
} while (i < j);
swap(list->D, low, j);
return j;
}
void QuickSort(List* list, int low, int high)
{
int k;
if (low < high)
{
k = Partition(list, low, high);
QuickSort(list, low, k - 1);
QuickSort(list, k + 1, high);
}
}
void QuickSort(List* list)
{
QuickSort(list, 0, list->n - 1);
printf("\n快速排序后的list为:\n");
}
void Merge(List* list, Entry* temp, int low, int n1, int n2)
{
int i = low, j = low + n1;
while (i <= low + n1 - 1 && j <= low + n1 + n2 - 1)
{
if (list->D[i].key <= list->D[j].key)
{
*temp++ = list->D[i++];
}
else
{
*temp++ = list->D[j++];
}
}
while (i <= low + n1 - 1)
{
*temp++ = list->D[i++];
}
while (j <= low + n1 + n2 - 1)
{
*temp++ = list->D[j++];
}
}
void MergeSort(List* list)
{
Entry temp[MaxSize];
int low, n1, n2, i, size = 1;
while (size < list->n)
{
low = 0;
while (low + size < list->n)
{
n1 = size;
if (low + 2 * size < list->n)
n2 = size;
else
n2 = list->n - low-size;
Merge(list, temp+low, low, n1, n2);
low += n1 + n2;
}
for (i = 0; i < low; i++)
{
list->D[i] = temp[i];
}
size *= 2;
}
printf("\n两路合并排序后的list为:\n");
}
void HeapSort(List* list)
{
int i;
PriorityQueue PQ;
CreatePQ(&PQ, list->n);
for (i = 0; i < list->n; i++)
{
Append(&PQ, list->D[i]);
}
for (i = PQ.n - 1; i > 0; i--)
{
queue_swap(PQ.elements, 0, i);
AdjustDown(PQ.elements, 0, i -1);
}
printf("\n堆排序后的list为:\n");
}
void create_key(int num)
{
clock_t start, end;
double duration;
start = clock();
int* key_list = (int*)malloc(sizeof(int) * num);
int i;
srand(time(NULL));
for (i = 0; i < num; i++)
{
key_list[i] = rand()%1000;
}
FILE* fp;
fopen_s(&fp, "C:\\Users\\HP\\Desktop\\数据结构实验4\\suiji.txt", "w");
if (fp == 0)
{
printf("\nfile error!\n");
exit(1);
}
for (i = 0; i < num; i++)
{
fprintf_s(fp, "%d%c", key_list[i],' ');
}
fclose(fp);
printf("\n随机关键字存储完毕!");
free(key_list);
end = clock();
duration = double(end - start);
printf(",存入文件txt中所花费的时间是%lfms\n", duration);
}
void keep_in_list(List* list,int num)
{
FILE* fp;
int i = 0;
char c = ' ';
Entry E;
KeyType temp;
fopen_s(&fp, "C:\\Users\\HP\\Desktop\\数据结构实验4\\suiji.txt", "r");
if (fp == 0)
{
printf("file error!!\n");
exit(1);
}
while (i < num)
{
fscanf_s(fp, "%d%c", &temp, &c);
Entry E;
E.data = 0;
E.key = temp;
list->D[list->n++] = E;
i++;
}
}
void Print(List list)
{
for (int i = 0; i < list.n; i++)
{
printf("%d ", list.D[i].key);
}
printf("\n");
}
void main()
{
List list;
InitList(&list);
int num;
clock_t start, end;
double duration;
printf("请输入产生随机关键字的总数\t");
scanf_s("%d", &num);
create_key(num);
keep_in_list(&list, num);
printf("\n原随机关键字为:\n");
Print(list);
start = clock();
InitList(&list);
keep_in_list(&list, num);
SelectionSort(&list);
Print(list);
DestoryList(&list);
end = clock();
duration = double(end - start);
printf("简单排序%d个关键字所花费的时间为%lf:\n",num, duration);
start = clock();
InitList(&list);
keep_in_list(&list, num);
InsertSort(&list);
Print(list);
DestoryList(&list);
end = clock();
duration = double(end - start);
printf("直接插入排序%d个关键字所花费的时间为%lf:\n", num, duration);
start = clock();
InitList(&list);
keep_in_list(&list, num);
BubbleSort(&list);
Print(list);
DestoryList(&list);
end = clock();
duration = double(end - start);
printf("冒泡排序%d个关键字所花费的时间为%lf:\n", num, duration);
start = clock();
InitList(&list);
keep_in_list(&list, num);
QuickSort(&list);
Print(list);
DestoryList(&list);
end = clock();
duration = double(end - start);
printf("快速排序%d个关键字所花费的时间为%lf:\n", num, duration);
start = clock();
InitList(&list);
keep_in_list(&list, num);
MergeSort(&list);
Print(list);
DestoryList(&list);
end = clock();
duration = double(end - start);
printf("两路合并排序%d个关键字所花费的时间为%lf:\n", num, duration);
start = clock();
InitList(&list);
keep_in_list(&list, num);
HeapSort(&list);
DestoryList(&list);
end = clock();
duration = double(end - start);
printf("\n堆排序%d个关键字所花费的时间为%lf:\n", num, duration);
}
堆排序的堆栈.h
#pragma once
#include"排序算法基础结构.h"
typedef struct priorityQueue
{
Entry* elements;
int n;
int maxSize;
}PriorityQueue;
void AdjustUp(Entry heap[], int current)
{
int p = current;
Entry temp;
while (p > 0)
{
if (heap[p].key > heap[(p - 1) / 2].key)
{
temp = heap[p];
heap[p] = heap[(p - 1) / 2];
heap[(p - 1) / 2] = temp;
p = (p - 1) / 2;
}
else
break;
}
}
void AdjustDown(Entry heap[], int current, int border)
{
int p = current;
int maxChild;
Entry temp;
while (2 * p + 1 <= border)
{
if ((2 * p + 2 <= border) && (heap[2 * p + 2].key > heap[2 * p + 1].key))
{
maxChild = 2 * p + 2;
}
else
{
maxChild = 2 * p + 1;
}
if (heap[p].key >= heap[maxChild].key)
{
break;
}
else
{
temp = heap[p];
heap[p] = heap[maxChild];
heap[maxChild] = temp;
p = maxChild;
}
}
}
void CreatePQ(PriorityQueue* PQ, int mSize)
{
PQ->maxSize = mSize;
PQ->n = 0;
PQ->elements = (Entry*)malloc(mSize * sizeof(Entry));
}
void Destory(PriorityQueue* PQ)
{
free(PQ->elements);
PQ->n = 0;
PQ->maxSize = 0;
}
BOOL IsEmpty(PriorityQueue* PQ)
{
if (PQ->n == 0)
return TRUE;
else
return FALSE;
}
BOOL IsFull(PriorityQueue* PQ)
{
if (PQ->n == PQ->maxSize)
return TRUE;
else
return FALSE;
}
int Size(PriorityQueue* PQ)
{
return PQ->n;
}
void Append(PriorityQueue* PQ, Entry x)
{
if (IsFull(PQ))
return;
PQ->elements[PQ->n] = x;
PQ->n++;
AdjustUp(PQ->elements, PQ->n - 1);
}
void Serve(PriorityQueue* PQ, Entry* x)
{
if (IsEmpty(PQ))
return;
*x = PQ->elements[0];
PQ->n--;
PQ->elements[0] = PQ->elements[PQ->n];
AdjustDown(PQ->elements, 0, PQ->n - 1);
}
void queue_swap(Entry *E, int start, int last)
{
Entry temp = E[start];
E[start] = E[last];
E[last] = temp;
}
排序算法基础结构.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<time.h>
typedef int KeyType;
typedef int DataType;
typedef int BOOL;
#define FALSE 0
#define TRUE 1
#define MaxSize 100000
typedef struct entry
{
KeyType key;
DataType data;
}Entry;
typedef struct list
{
int n;
Entry *D;
}List;
void InitEntry(Entry* E, KeyType key, DataType data)
{
E->data = data;
E->key = key;
}
void InitList(List* list)
{
list->n = 0;
list->D = (Entry*)malloc(sizeof(Entry) * MaxSize);
}
void DestoryList(List* list)
{
list->n = 0;
free(list->D);
}
void listAppend(List* list, Entry E)
{
if (list->n < MaxSize)
{
list->D[list->n++] = E;
}
else
{
return;
}
}