@Jayfeather
2018-06-12T10:00:28.000000Z
字数 5085
阅读 818
数据结构与算法分析
// 排序算法.cpp: 定义控制台应用程序的入口点。//忽然发现互联网对于编程学习真的是个好东西//几乎所有的知识点都有很详细的讲解//而且大多数都是别人已经扫过雷,很少有错误的//虽然没有书本权威,但是容易理解//而且还能及时的与作者交流/*这是一个包含11个算法+选择驱动程序的学习笔记*//*懒得写注释了,凑活着看吧~*/#include "stdafx.h"#include<stdio.h>#include<stdlib.h>void swap(int *a, int *b);void sort(int A[], int num,int K);void BubbleSort(int A[], int num);void CocktailSort(int A[], int num);void SelectionSort(int A[], int num);void InsertionSort(int A[], int num);void InsertionSortDichotomy(int A[], int num);void ShellSort(int A[], int num);void MergeSort(int A[], int num);void merge(int A[], int left, int right, int *p);void mergearry(int A[], int left, int right, int mid, int *p);void HeapSort(int A[], int num);void Heapify(int A[], int i, int heapsize);int BuildHeap(int A[], int num);void QuickSort(int A[], int num);int Partion(int A[], int right,int left);void QuickSortcom(int A[], int right, int left);void CountingSort(int A[], int num);void LsdRadixSort(int A[], int n);int GetDigit(int x, int d);void CountingSortL(int A[], int num, int d);void InsertionSort(int A[], int left, int right);int MapToBucket(int x);void CountingSortB(int A[], int num);void BucketSort(int A[], int num);int main(){int num = 100,K=0;int A[100] = { 80, 9, 70, 22, 3, 63, 12, 81,73, 41, 90, 83, 27, 71, 88, 5, 40, 18, 25, 37, 55, 60, 93, 87, 17, 89, 99, 84, 32, 96, 62, 98, 77, 30, 23, 35, 47, 24, 21, 53, 95, 7, 85, 2, 65, 1, 39, 43, 76, 46, 42, 91, 4, 26, 52, 86, 34, 54, 38, 78, 31, 11, 66, 36, 50, 75, 16, 68, 56, 33,48, 15, 74, 69, 49, 6, 58, 10, 29, 92, 64,59, 28, 61, 45, 19, 14, 13, 44, 72, 94,20, 97, 51, 67, 79, 0, 82, 8, 57 };printf("Please insert the kind of sort you want:");scanf_s("%d", &K);sort(A, num,K);for (int i = 0; i < num; i++)printf("%d ", A[i]);return 0;}void swap(int *a, int *b){if (a == b)return;int temp;temp = *a;*a = *b;*b = temp;}void sort(int A[100], int num,int K){switch (K){case 1:BubbleSort(A, num);break;case 2:CocktailSort(A, num);break;case 3:SelectionSort(A, num);break;case 4:InsertionSort(A, num);break;case 5:InsertionSortDichotomy(A, num);break;case 6:ShellSort(A, num);break;case 7:MergeSort(A, num);break;case 8:HeapSort(A, num);break;case 9:QuickSort(A, num);break;case 10:CountingSort(A, num);break;case 11:LsdRadixSort(A, num);break;case 12:BucketSort(A, num);break;}return ;}void BubbleSort(int A[], int num){for (int i = 0; i < num;i++){for (int t = 0; t < num - i-1; t++){if (A[t] > A[t + 1])swap(&A[t], &A[t + 1]);}}}void CocktailSort(int A[], int num){int i = 0;while(i<(num+1)/2){for (int m = i; m < num - i-1; m++){if (A[m] > A[m + 1])swap(&A[m], &A[m + 1]);}for (int n = num - i-1; n > i; n--){if (A[n] < A[n -1])swap(&A[n], &A[n - 1]);}i++;}}void SelectionSort(int A[], int num){for (int i = 0; i < num; i++){int temp=i;for (int n = i; n < num; n++){if (A[n] < A[temp])temp = n;}swap(&A[temp], &A[i]);}}void InsertionSort(int A[], int num){for (int i = 1; i < num; i++){int temp = A[i],n=i-1;while(n>=0){if (temp <= A[n])A[n + 1] = A[n];elsebreak;n--;}A[n+1] = temp;}}void InsertionSortDichotomy(int A[], int num){for (int i = 1; i < num; i++){int left = 0, right = i - 1, mid = (left + right) / 2;int temp = A[i];while (left <= right){if (temp < A[mid])right = mid - 1;if (temp >= A[mid])left = mid + 1;mid = (left + right) / 2;}for (int n = i - 1; n >= left; n--){A[n + 1] = A[n];}A[left] = temp;}}void ShellSort(int A[], int num){int h=0;while (h < num / 3){h = 3*h + 1;}while (h != 0){for (int i = h; i < num; i++){int temp = A[i], n = i-h;while (n >= 0){if (temp <= A[n])A[n + h] = A[n];elsebreak;n=n-h;}A[n + h] = temp;}h = (h - 1) / 3;}}void MergeSort(int A[], int num){int *p = new int[num];merge(A, 0, num-1, p);//delete[] p;}void merge(int A[], int left, int right, int *p){int mid = (right + left) / 2;if ((right) == left)return;else{merge(A, left, mid, p);merge(A, mid + 1, right, p);mergearry(A, left, right, mid, p);}}void mergearry(int A[], int left, int right, int mid, int *p){int i = left, j = mid + 1, counter = 0, m = mid, n = right, *temp = p;while (j <=right && i <= mid){if (A[i] <= A[j]){p[counter] = A[i];i++;}else if (A[i] > A[j] ){p[counter] = A[j];j++;}counter++;}while (i <= mid){temp[counter++] = A[i++];}while (j <= right){temp[counter++] = A[j++];}for (int n = 0; n < counter; n++){A[left+n] = p[n];}}void HeapSort(int A[], int num){int Heapsize = BuildHeap(A, num);while (Heapsize > 1){swap(&A[0], &A[--Heapsize]);Heapify(A, 0, Heapsize);}}int BuildHeap(int A[], int num){int Heapsize = num;for (int i = (num - 2) / 2; i >= 0; i--){Heapify(A, i, Heapsize);}return Heapsize;}void Heapify(int A[], int i, int Heapsize){int left=2*i;int right = 2 * i + 1;int max = i;if (left<Heapsize&&A[left] > A[max]){max = left;}if (right<Heapsize&&A[right] > A[max]){max = right;}if (max != i){swap(&A[i], &A[max]);Heapify(A, max, Heapsize);}}void QuickSort(int A[], int num){int right = num, left = 0;QuickSortcom(A, right-1, left);}void QuickSortcom(int A[], int right, int left){if (left>=right)return;else{int mid = Partion(A, right, left);QuickSortcom(A, right, mid+1);QuickSortcom(A, mid-1, left);}}int Partion(int A[], int right, int left){int index = A[right];int tail = left;for (int n = left; n < right; n++){if (A[n] < index){swap(&A[n], &A[tail]);tail++;}}swap(&A[right], &A[tail]);return tail;}void CountingSort(int A[], int num){const int k = 100;int B[k] = { 0 };int C[100];for (int n = 0; n < num; n++){B[A[n]]++;}for (int n = 1; n < num; n++){B[n] += B[n - 1];}for (int n = num-1; n > 0; n--){C[--B[A[n]]] = A[n];}for (int n = 0; n < num; n++){A[n] = B[n];}}void LsdRadixSort(int A[], int num){for (int d = 1; d <3;d++){CountingSortL(A, num, d);}}int GetDigit(int x, int d){int Digit[3] = { 1,1,10 };return (x / Digit[d]) % 10;}void CountingSortL(int A[], int num, int d){const int k = 10;int C[k] = { 0 };int B[100];for (int n = 0; n < num; n++){C[GetDigit(A[n], d)]++;}for (int n = 1; n < k; n++){C[n] += C[n - 1];}for (int n = num - 1; n >= 0; n--){B[--C[GetDigit(A[n], d)]] = A[n];}for (int n = 0; n < num; n++){A[n] = B[n];}}