用c语言实现各种排序(插入、希尔、选择、堆排、冒泡、递归和非递归快排、归并)
由于快排的非递归实现需要用到堆,所以我们先导入堆的代码
Stack.h
#pragma once #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <stdbool.h> typedef int STDataType; typedef struct Stack { STDataType* a; int top; int capacity; }ST; void StackInit(ST* ps); void StackDestroy(ST* ps); void StackPush(ST* ps, STDataType x); void StackPop(ST* ps); STDataType StackTop(ST* ps); int StackSize(ST* ps); bool StackEmpty(ST* ps);
Stack.c
#include "Sort.h" #include "Stack.h" void StackInit(ST* ps) { ps->a = NULL; ps->top = 0; ps->capacity = 0; } void StackDestroy(ST* ps) { assert(ps); free(ps->a); ps->top = ps->capacity = 0; ps->a = NULL; } void StackPush(ST* ps, STDataType x) { assert(ps); if (ps->top == ps->capacity) { int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2; STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType)); if (tmp == NULL) { printf("realloc is field\n"); exit(-1); } ps->a = tmp; ps->capacity = newcapacity; } ps->a[ps->top] = x; ps->top++; } void StackPop(ST* ps) { assert(ps); assert(!StackEmpty(ps)); ps->top--; } STDataType StackTop(ST* ps) { assert(ps); assert(!StackEmpty(ps)); return ps->a[ps->top - 1]; } int StackSize(ST* ps) { assert(ps); assert(!StackEmpty(ps)); return ps->top; } bool StackEmpty(ST* ps) { assert(ps); if (ps->top == 0) { return true; } else { return false; } }
下面是排序的代码
Sort.h
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> // 插入排序 void InsertSort(int* a, int n); // 希尔排序 void ShellSort(int* a, int n); // 选择排序 void SelectSort(int* a, int n); // 堆排序 void AdjustDwon(int* a, int n, int root); void AdjustUp(int* a, int child); void HeapSort(int* a, int n); // 冒泡排序 void BubbleSort(int* a, int n); // 快速排序递归实现 // 快速排序hoare版本 int PartSort1(int* a, int left, int right); // 快速排序挖坑法 int PartSort2(int* a, int left, int right); // 快速排序前后指针法 int PartSort3(int* a, int left, int right); void QuickSort(int* a, int left, int right); // 快速排序 非递归实现 void QuickSortNonR(int* a, int left, int right); void MergeSort(int* a, int n);
Sort.c
#define _CRT_SECURE_NO_WARNINGS 1 #include "Sort.h" #include "Stack.h" // 插入排序 void InsertSort(int* a, int n) { for (int i = 0; i < n - 1; i++) { int end = i; int tmp = a[end + 1]; while (end >= 0) { if (a[end] > tmp) { a[end + 1] = a[end]; end--; } else { break; } } a[end +1] = tmp; } } // 希尔排序 void ShellSort(int* a, int n) { int gap = n; while (gap > 1) { gap = gap / 3 + 1; for (int i = 0; i < n - gap; i++) { int end = i; int tmp = a[end + gap]; while (end >= 0) { if (a[end] > tmp) { a[end + gap] = a[end]; end-=gap; } else { break; } a[end + gap] = tmp; } } } } void Swap(int* a, int* b) { int tmp = *a; *a = *b; *b = tmp; } // 选择排序 void SelectSort(int* a, int n) { int begin = 0; int end = n - 1; int less = 0; int more = 0; while (begin < end) { for (int cur = begin; cur <= end; cur++) { if (a[less] > a[cur]) { less = cur; } if (a[more] < a[cur]) { more = cur; } } Swap(&a[begin], &a[less]); if (begin == more) { more = less; } Swap(&a[end], &a[more]); begin++; end--; } } // 堆排序 //排升序建大堆 void AdjustDwon(int* a, int n, int root) { int parent = root; int child = parent * 2 + 1; while (child < n) { if (child + 1 < n && a[child+1] > a[child]) { child++; } if (a[child] > a[parent]) { Swap(&a[child], &a[parent]); parent = child; child = parent * 2 + 1; } else break; } } void AdjustUp(int* a, int child) { int parent = (child - 1) / 2; while (child > 0) { if (a[parent] < a[child]) { Swap(&a[parent], &a[child]); child = parent; parent = (child - 1) / 2; } else break; } } void HeapSort(int* a, int n) { for (int i = (n-1-1)/2; i >= 0; i--) { AdjustDwon(a, n, i); } for (int end = n - 1; end > 0; end--) { Swap(&a[0], &a[end]); AdjustDwon(a, end, 0); } } // 冒泡排序 void BubbleSort(int* a, int n) { for (int j = 0; j < n - 1; j++) { for (int i = 0; i < n - 1 -j; i++) { if (a[i] > a[i + 1]) { Swap(&a[i], &a[i + 1]); } } } } int GetMid(int* a, int left, int right) { int mid = (left + right) / 2; if (a[left] < a[right]) { if (a[right] < a[mid]) { return right; } else if (a[left] > a[mid]) { return left; } else { return mid; } } else // a[left]>=a[right] { if (a[mid] > a[left]) { return left; } else if (a[right] > a[mid]) { return right; } else { return mid; } } } // 快速排序递归实现 // 快速排序hoare版本 int PartSort1(int* a, int left, int right) { int mini = GetMid(a, left, right); Swap(&a[mini], &a[left]); int keyi = left; while (left < right) { while (left < right && a[right] >= a[keyi]) { right--; } while (left < right && a[left] <= a[keyi]) { left++; } Swap(&a[left], &a[right]); } Swap(&a[left], &a[keyi]); return left; } // 快速排序挖坑法 int PartSort2(int* a, int left, int right) { int mini = GetMid(a, left, right); Swap(&a[mini], &a[left]); int key = a[left]; int pit = left; while (left < right) { while (left < right && a[right] >= key) { right--; } a[pit] = a[right]; pit = right; while (left < right && a[left] <= key) { left++; } a[pit] = a[left]; pit = left; } a[pit] = key; return pit; } // 快速排序前后指针法 int PartSort3(int* a, int left, int right) { int mini = GetMid(a, left, right); Swap(&a[mini], &a[left]); int prev = left; int key = left; int cur = prev+1; while (cur <= right) { if (a[cur] < a[key] && ++prev!=cur) { Swap(&a[cur], &a[prev]); } cur++; } Swap(&a[key], &a[prev]); return prev; } void QuickSort(int* a, int left, int right) { if (left >= right) return; // 小区间优化,减少递归次数 if (left + right + 1 < 10) { InsertSort(a + left, right - left + 1); } else { int keyi = PartSort3(a, left, right); QuickSortNonR(a, left, keyi - 1); QuickSortNonR(a, keyi + 1, right); } } // 快速排序 非递归实现 void QuickSortNonR(int* a, int left, int right) { ST st; StackInit(&st); StackPush(&st, left); StackPush(&st, right); while (!StackEmpty(&st)) { int end = StackTop(&st); StackPop(&st); int begin = StackTop(&st); StackPop(&st); int key = PartSort3(a, begin, end); if (key + 1 < end) { StackPush(&st, key + 1); StackPush(&st, end); } if (begin < key - 1) { StackPush(&st, begin); StackPush(&st, key-1); } } StackDestroy(&st); } void _MergeSort(int* a, int left, int right, int* tmp) { if (left >= right) { return; } int mid = (right + left) / 2; _MergeSort(a, left, mid, tmp); _MergeSort(a, mid+1, right, tmp); int begin1 = left; int end1 = mid; int begin2 = mid + 1; int end2 = right; int i = left; while (begin1 <= end1 && begin2 <= end2) { if (a[begin1] < a[begin2]) { tmp[i++] = a[begin1++]; } else { tmp[i++] = a[begin2++]; } } while (begin1 <= end1) { tmp[i++] = a[begin1++]; } while (begin2 <= end2) { tmp[i++] = a[begin2++]; } for (int j = left; j <= right; j++) { a[j] = tmp[j]; } } void MergeSort(int* a, int n) { int* tmp = (int*)malloc(sizeof(int) * n); if (tmp == NULL) { printf("malloc fail\n"); exit(-1); } int left = 0; int right = n - 1; _MergeSort(a, left, right, tmp); free(tmp); tmp = NULL; }
测试代码
#include "Sort.h" void InsertSortTest() { int a[] = { 9,1,2,5,7,4,8,6,3,5 }; for (int i = 0; i < sizeof(a) / sizeof(int); i++) { printf("%d ", a[i]); } printf("\n"); InsertSort(a, sizeof(a) / sizeof(int)); printf("InSortTest:"); for (int i = 0; i < sizeof(a) / sizeof(int); i++) { printf("%d ", a[i]); } printf("\n"); } void ShellSortTest() { int a[] = { 9,1,2,5,7,4,8,6,3,5 }; for (int i = 0; i < sizeof(a) / sizeof(int); i++) { printf("%d ", a[i]); } printf("\n"); ShellSort(a, sizeof(a) / sizeof(int)); printf("ShellSortTest:"); for (int i = 0; i < sizeof(a) / sizeof(int); i++) { printf("%d ", a[i]); } printf("\n"); } void SelectSortTest() { int a[] = { 9,1,2,5,7,4,8,6,3,5 }; for (int i = 0; i < sizeof(a) / sizeof(int); i++) { printf("%d ", a[i]); } printf("\n"); SelectSort(a, sizeof(a) / sizeof(int)); printf("SelectSortTest:"); for (int i = 0; i < sizeof(a) / sizeof(int); i++) { printf("%d ", a[i]); } printf("\n"); } void HeapSortTest() { int a[] = { 9,1,2,5,7,4,8,6,3,5 }; for (int i = 0; i < sizeof(a) / sizeof(int); i++) { printf("%d ", a[i]); } printf("\n"); HeapSort(a, sizeof(a) / sizeof(int)); printf("HeapSortTest:"); for (int i = 0; i < sizeof(a) / sizeof(int); i++) { printf("%d ", a[i]); } printf("\n"); } void BubbleSortTest() { int a[] = { 9,1,2,5,7,4,8,6,3,5 }; for (int i = 0; i < sizeof(a) / sizeof(int); i++) { printf("%d ", a[i]); } printf("\n"); BubbleSort(a, sizeof(a) / sizeof(int)); printf("BubbleSortTest:"); for (int i = 0; i < sizeof(a) / sizeof(int); i++) { printf("%d ", a[i]); } printf("\n"); } void QuickSortTest() { int a[] = { 9,1,2,5,7,4,8,6,3,5 }; for (int i = 0; i < sizeof(a) / sizeof(int); i++) { printf("%d ", a[i]); } printf("\n"); QuickSort(a, 0, sizeof(a) / sizeof(int)-1); printf("QuickSortTest:"); for (int i = 0; i < sizeof(a) / sizeof(int); i++) { printf("%d ", a[i]); } printf("\n"); } void QuickSortNonRTest() { int a[] = { 9,1,2,5,7,4,8,6,3,5 }; for (int i = 0; i < sizeof(a) / sizeof(int); i++) { printf("%d ", a[i]); } printf("\n"); QuickSortNonR(a, 0, sizeof(a) / sizeof(int) - 1); printf("QuickSortNonRTest:"); for (int i = 0; i < sizeof(a) / sizeof(int); i++) { printf("%d ", a[i]); } printf("\n"); } void MergeSortTest() { int a[] = { 10,6,7,1,3,9,4,2 }; for (int i = 0; i < sizeof(a) / sizeof(int); i++) { printf("%d ", a[i]); } printf("\n"); MergeSort(a, sizeof(a) / sizeof(int)); printf("MergeSortTest:"); for (int i = 0; i < sizeof(a) / sizeof(int); i++) { printf("%d ", a[i]); } printf("\n"); } void testSort() { /*InsertSortTest(); ShellSortTest(); SelectSortTest(); HeapSortTest(); BubbleSortTest(); QuickSortTest(); QuickSortNonRTest();*/ MergeSortTest(); } void TestOP() { srand(time(0)); const int N = 100000; int* a1 = (int*)malloc(sizeof(int) * N); int* a2 = (int*)malloc(sizeof(int) * N); int* a3 = (int*)malloc(sizeof(int) * N); int* a4 = (int*)malloc(sizeof(int) * N); int* a5 = (int*)malloc(sizeof(int) * N); int* a6 = (int*)malloc(sizeof(int) * N); for (int i = 0; i < N; ++i) { a1[i] = rand(); a2[i] = a1[i]; a3[i] = a1[i]; a4[i] = a1[i]; a5[i] = a1[i]; a6[i] = a1[i]; } int begin1 = clock(); InsertSort(a1, N); int end1 = clock(); int begin2 = clock(); ShellSort(a2, N); int end2 = clock(); //int begin3 = clock(); //SelectSort(a3, N); //int end3 = clock(); //int begin4 = clock(); //HeapSort(a4, N); //int end4 = clock(); //int begin5 = clock(); //QuickSort(a5, 0, N - 1); //int end5 = clock(); //int begin6 = clock(); //MergeSort(a6, N); //int end6 = clock(); printf("InsertSort:%d\n", end1 - begin1); printf("ShellSort:%d\n", end2 - begin2); //printf("SelectSort:%d\n", end3 - begin3); //printf("HeapSort:%d\n", end4 - begin4); //printf("QuickSort:%d\n", end5 - begin5); //printf("MergeSort:%d\n", end6 - begin6); free(a1); free(a2); free(a3); free(a4); free(a5); free(a6); } int main() { testSort(); //TestOP(); return 0; }