算法复杂度:
#include<iostream> #include<stdio.h> #include<math.h> using namespace std; class paixu { public: void bubbleSort0(int* pData, int length)//1,冒泡排序 { int temp; for (int i = 0; i != length; ++i) { for (int j = 0; j != length; ++j) { if (pData[i] < pData[j]) { temp = pData[i]; pData[i] = pData[j]; pData[j] = temp; } } } } void selectionSort(int* pData, int length)//2,选择排序 { int minIndex, temp; for (int i = 0; i < length - 1; i++) { minIndex = i; for (int j = i + 1; j < length; j++) { if (pData[j] <pData[minIndex]) // 寻找最小的数 { minIndex = j; // 将最小数的索引保存 } } temp = pData[i]; pData[i] = pData[minIndex]; pData[minIndex] = temp; } } void insertionSort(int* pData, int length)//3,插入排序 { int preIndex, current; for (int i = 1; i < length; i++) { preIndex = i - 1; current = pData[i]; while (preIndex >= 0 && pData[preIndex] > current) { pData[preIndex + 1] = pData[preIndex]; preIndex--; } pData[preIndex + 1] = current; } } void shellSort(int* pData, int length) //4,希尔排序 { for (int gap =floor(length / 2); gap > 0; gap = floor(gap / 2)) { for (int i = gap; i < length; i++) { int j = i; int current = pData[i]; while (j - gap >= 0 && current < pData[j - gap]) { pData[j] = pData[j - gap]; j = j - gap; } pData[j] = current; } } } int len; void heapSort(int* pData, int length)//5,堆排序(Heap Sort) { buildMaxHeap(pData, length); for (int i = length - 1; i > 0; i--) { swap(pData, 0, i); len--; heapify(pData, 0); } } void merge_sort(int *data, int start, int end, int *result)//6,归并排序 { if (1 == end - start)//如果区间中只有两个元素,则对这两个元素进行排序 { if (data[start] > data[end]) { int temp = data[start]; data[start] = data[end]; data[end] = temp; } return; } else if (0 == end - start)//如果只有一个元素,则不用排序 return; else { //继续划分子区间,分别对左右子区间进行排序 merge_sort(data, start, (end - start + 1) / 2 + start, result); merge_sort(data, (end - start + 1) / 2 + start + 1, end, result); //开始归并已经排好序的start到end之间的数据 merge(data, start, end, result); //把排序后的区间数据复制到原始数据中去 for (int i = start; i <= end; ++i) data[i] = result[i]; } } void QuickSort(int a[], int start, int end)//7,快速排序 { if (start > end) { return; } int i = start; int j = end; int k = a[i]; while (i < j) { while (i < j && a[j] > k)//先从后面开始找小于k的值 j--; a[i] = a[j];//找到小于k的值,替换a[i] while (i < j && a[i] < k)//从前面找大于k的值 i++; a[j] = a[i];//找到大于k的值,刚才a[j]的值已经赋刚才的a[i];a[j]的值用现在找到的大于k的值填充 } a[j] = k;//最后i = j; 把k的值赋给a[i]; QuickSort(a, i + 1, end); QuickSort(a, start, i - 1); } private: ///////////////////////////////(堆排序) void buildMaxHeap(int* pData, int length) { // 建立大顶堆 len = length; for (int i = floor(len / 2); i >= 0; i--) { heapify(pData, i); } } void heapify(int* pData, int i) { // 堆调整 int left = 2 * i + 1, right = 2 * i + 2, largest = i; if (left < len && pData[left] >pData[largest]) { largest = left; } if (right < len && pData[right] > pData[largest]) { largest = right; } if (largest != i) { swap(pData, i, largest); heapify(pData, largest); } } void swap(int*pData, int i, int j) { int temp = pData[i]; pData[i] = pData[j]; pData[j] = temp; } ////////////////////////////////////////////(归并排序) void merge(int *data, int start, int end, int*result) { int left_length = (end - start + 1) / 2 + 1;//左部分区间的数据元素的个数 int left_index = start; int right_index = start + left_length; int result_index = start; while (left_index < start + left_length && right_index < end + 1) { //对分别已经排好序的左区间和右区间进行合并 if (data[left_index] <= data[right_index]) result[result_index++] = data[left_index++]; else result[result_index++] = data[right_index++]; } while (left_index < start + left_length) result[result_index++] = data[left_index++]; while (right_index < end + 1) result[result_index++] = data[right_index++]; } //////////////////////////////////////////////// }; int main() { int Sqlist[] = { 0, 4, 2, 1, 3 }; int length = sizeof(Sqlist) / sizeof(int); int start = 0; int end = length - 1; int result[5]; //paixu().bubbleSort0(Sqlist, length); //paixu().selectionSort(Sqlist, length); //paixu().insertionSort(Sqlist, length); //paixu().shellSort(Sqlist, length); //paixu().heapSort(Sqlist, length); //paixu().merge_sort(Sqlist, start,end,result); paixu().QuickSort(Sqlist, start, end); for (int i = 0; i != length; ++i) { cout << Sqlist[i] << " "; } cout << endl; system("pause"); return 0; }