常用的内部排序——C语言实现(选择排序,插入排序,冒泡排序,折半插入排序,冒泡排序,堆排序,快速排序,归并排序)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXSIZE 100
#define INF 0x3f
typedef int KeyType;
typedef int InfoType;
typedef struct {
KeyType key;InfoType weight;
}RedType;typedef struct
{
RedType r[MAXSIZE+1];int length;
}SqList;void init(SqList* L);
void print_L(SqList* L);
int SelectMinKey(SqList* L,int i);
void Swap(SqList* L, int x, int y);
void SelectSort(SqList* L);
void InsertSort(SqList* L);
void BubbleSort(SqList* L);
void BInsertSort(SqList* L);
void QuickSort_Recursion(SqList* L,int low,int high);
int Partition(SqList* L,int low,int high);
void HeapSort(SqList* L);
void Build_Heap(SqList* L);
void Heapify(SqList* L,int i);
void MergingSort(SqList* L,int l,int m,int r);
void MergeSort(SqList* L,int l,int r);int main() {
SqList* L = (SqList*)malloc(sizeof(SqList));clock_t start, finish;init(L);printf("Before sorting:\n");print_L(L);start = clock();MergeSort(L,0,L->length-1);finish = clock();double time = (double)(finish - start);printf("Sorted:\n");print_L(L);printf("Execution time: %f ms",time);free(L);return 0;
}
void init(SqList* L){
int a[] = {
49,37,67,89,23,15,35,47,49,98};L->length=sizeof(a)/4;for (int i = 0; i < L->length; ++i) {
L->r[i].key=a[i];L->r[i].weight = i;}
}
void print_L(SqList* L){
for (int i = 0; i < L->length; ++i) {
printf("key:%-4d weight:%-4d\n",L->r[i].key,L->r[i].weight);}
}
int SelectMinKey(SqList* L,int i){
int min=INF;int index;for (i; i < L->length; ++i) {
if (L->r[i].key<min){
min = L->r[i].key;index=i;}}return index;
}
void Swap(SqList* L, int x, int y){
RedType temp;if (L->r[x].key != L->r[y].key){
temp = L->r[x];L->r[x] = L->r[y];L->r[y] = temp;}
}
void SelectSort(SqList* L){
int index;for (int i = 0; i < L->length; ++i) {
index = SelectMinKey(L,i);Swap(L, i, index);}
}
void InsertSort(SqList* L){
int j;RedType R; for (int i = 1; i < L->length; ++i) {
if (L->r[i].key < L->r[i-1].key){
R = L->r[i];for (j = i-1; L->r[j].key > R.key ; j--)L->r[j+1] = L->r[j];L->r[j+1] = R;}}
}
void BubbleSort(SqList* L){
for (int i = 1; i < L->length; ++i) {
for (int j = 0; j < L->length-i; ++j) {
if (L->r[j].key > L->r[j+1].key){
Swap(L,j,j+1);}}}
}
void BInsertSort(SqList* L){
RedType R; int low,high,mid,i,j;for (i = 1; i < L->length; ++i) {
R = L->r[i];low = 0;high = i-1;while(low<=high){
mid = (low+high)/2;if(R.key < L->r[mid].key) high = mid-1;else low = mid+1;}for (j = i-1; j >= high+1 ; --j) L->r[j+1] = L->r[j];L->r[high+1] = R;}
}
void QuickSort_Recursion(SqList* L,int low,int high){
int pivotloc;if (low<high){
pivotloc = Partition(L,low,high);QuickSort_Recursion(L,low,pivotloc-1);QuickSort_Recursion(L,pivotloc+1,high);}
}
int Partition(SqList* L,int low,int high){
RedType R = L->r[low];int pivotkey = L->r[low].key;while(low<high){
while (low<high && L->r[high].key>=pivotkey) --high;L->r[low] = L->r[high];while(low<high && L->r[low].key<=pivotkey) ++low;L->r[high] = L->r[low];}L->r[low] = R;return low;
}
void HeapSort(SqList* L){
Build_Heap(L);int n = L->length;for (int i = n-1; i >=0 ; --i) {
Swap(L,i,0);L->length-=1;Heapify(L,0);}L->length = n;
}
void Build_Heap(SqList* L){
int last_node = L->length - 1;int Last_parent = (last_node - 1) / 2;for (int i=Last_parent; i >=0 ; --i) {
Heapify(L,i);}
}
void Heapify(SqList * L,int i){
int left = i * 2 + 1;int right = i * 2 + 2;int max = i;if(left < L->length && L->r[left].key > L->r[max].key)max = left;if(right < L->length && L->r[right].key > L->r[max].key)max = right;if(max != i){
Swap(L,i,max);Heapify(L,max);}}
void QuickSort_NotR(SqList *l){
}
void MergingSort(SqList* L,int l,int m,int r){
int LEFT_SIZE = m-l;int RIGHT_SIZE = r-m+1;RedType left[LEFT_SIZE];RedType right[RIGHT_SIZE];int i,j,k=l;for (i = 0; i < LEFT_SIZE; ++i) {
left[i] = L->r[k];k++;}for (j = m; j <=r ; ++j) {
right[j-m] = L->r[j];}i=0; j=0; k=l;while(i<LEFT_SIZE && j<RIGHT_SIZE){
if(left[i].key > right[j].key){
L->r[k] = left[i];i++;k++;}else{
L->r[k] = right[j];j++;k++;}}while(i < LEFT_SIZE){
L->r[k] = left[i];i++;k++;}while(j < RIGHT_SIZE){
L->r[k] = right[j];j++;k++;}
}
void MergeSort(SqList* L,int l,int r){
if(l==r) return;else{
int m = (l+r)/2;MergeSort(L,l,m);MergeSort(L,m+1,r);MergingSort(L,l,m+1,r);}
}