由于很多大神已经总结过各种排序算法的性能及适合坏境,这里便不再总结,只是强调几个重点。
(1)快排的空间复杂度不是o(1),是o(logn)~o(n);
(2)四种不稳定的排序算法:简单选择,快排,希尔和堆排序
(3)冒泡排序最有情况下时间复杂度可优化为o(n);
(4)归并排序的空间复杂度为o(n);
(5)快排在整体倒序的情况下时间复杂度为o(n2)
(6)建一个最大堆的复杂度为o(n):具体证明可参考算法导论相关章节
(7)本文代码的归并排序的范围是[ )
(8)本文代码的堆排序为了方便从a[1]开始
(9)在添加了语句using namespace std;语句之后,swap函数才可以使用
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream> using namespace std; void print(int *number, int length) { if(number == NULL || length < 1) return; for(int i = 0; i < length; ++i) { printf("%d ", number[i]); } printf("\n"); } void SelectSort(int number[], int length) { if(number == NULL || length < 1) return; for(int i = 0; i < length - 1; ++i) { int k = i; for(int j = i + 1; j < length; ++j) { if(number[k] > number[j])//注意大小于号 k = j; } if(k != i) { swap(number[k], number[i]); } } } void BubbleSort(int number[], int length) { if(number == NULL || length < 1) return; for(int i = 1; i < length; ++i) { for(int j = 0; j < length - i; ++j) if(number[j] > number[j + 1]) swap(number[j], number[j + 1]); } } void InsertSort(int *number, int length) { if(number == NULL || length < 1) return; for(int i = 1; i < length; ++i) { int tmp = number[i]; int j; //在外面定义 for(j = i - 1; j >= 0; j--) { if(number[j] > number[i])//比较的顺序别弄错了 number[j + 1] = number[j];//最好检查一下 else break; } number[j+1] = tmp; } } void ShellSort(int number[], int length) { if(number == NULL || length < 1) return; int i, j, d; d = length/2; while(d >= 1) { for(i = d; i < length; ++i) { int tmp = number[i]; for(j = i - d; j >= 0; j -= d) { if(number[j] > number[i]) number[j + d] = number[j]; else break; } number[j + d] = tmp; } d /= 2; } } int partion(int number[], int length, int start, int end) { if(number == NULL || length < 1 || start < 0 || end >= length) return -1; int small = start - 1; for(int index = start; index < end; ++index) { ++small; if(number[index] < number[end]) { if(small != index) swap(number[small], number[index]); } } ++small; swap(number[small], number[end]); return small; } void QuickSort(int number[], int length, int start, int end)// [start, end] { if(number == NULL || length < 1) return; if(start == end) return; int index = partion(number, length, start, end); if(index > start) QuickSort(number, length, start, index - 1); if(index < end) QuickSort(number, length, index + 1, end); } void MergeSort(int number[], int length, int start, int end, int tmp[])//[start, end) { if(number == NULL || length < 1 || start < 0 || end < 1) return; //int middle = start + (end - start)/2; if(end - start > 1)//while(start < end) { int middle = start + (end - start)/2; int p = start; int q = middle; int i = start; MergeSort(number, length, start, middle, tmp); MergeSort(number, length, middle, end, tmp); while(p < middle || q < end) { if(q >= end || (p < middle && number[p] < number[q]))// q < end tmp[i++] = number[p++]; else tmp[i++] = number[q++]; } for(int index = start; index < end; ++index) number[index] = tmp[index]; } } void AjustHeap(int number1[], int i, int size) { if(number1 == NULL || i < 1 || size < 1) return; int leftChild = 2 * i; int rightChild = 2 * i + 1; int max = i; if(i <= size/2 )// max <= size 是错的 { if(leftChild <= size && number1[leftChild] > number1[max]) max = leftChild; if(rightChild <= size && number1[rightChild] > number1[max]) max = rightChild; if(max != i) { swap(number1[max], number1[i]); AjustHeap(number1, max, size); } } } void BuildMaxHeap(int number1[], int size) { if(number1 == NULL || size < 1) return; for(int i = size/2; i >= 1; --i) AjustHeap(number1, i, size); } void HeapSort(int number1[], int size) { if(number1 == NULL || size < 1) return; BuildMaxHeap(number1, size); for(int i = size; i >= 1; --i) { swap(number1[1], number1[i]); AjustHeap(number1, 1, i - 1); } } int main() { int number[10] = {3, 5, 2, 10, 4, 1, 8, 7, 6, 9}; //简单选择排序 SelectSort(number, 10); print(number, 11); //冒泡排序 BubbleSort(number, 10); print(number, 10); //插入排序 InsertSort(number, 10); print(number, 10); //希尔排序 ShellSort(number, 10); print(number, 10); //快速排序 QuickSort(number, 10, 0, 9); print(number, 10); //归并排序 int tmp[10]; MergeSort(number, 10, 0, 10, tmp); print(number, 10); //堆排序,为了方便,堆排序从a[1]开始 int number1[11] = {0, 3, 5, 2, 10, 4, 1, 8, 7, 6, 9}; HeapSort(number1, 10); print(number1, 10); printf("%d\n",number1[10]); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-18 22:44:37