主要的排序算法实现:
1 class Solution { 2 public: 3 //快排 时间复杂度:O(nlgn) 空间复杂度:O(lgn) 4 void func(vector<int>& nums, int low, int high){ 5 if(low < high){ 6 int mid = func1(nums, low, high); 7 func(nums, low, mid); 8 func(nums, mid+1, high); 9 } 10 } 11 int func1(vector<int>& nums, int low, int high){ 12 int key = nums[low]; 13 while(low < high){ 14 while(low < high && key <= nums[high]) high--; 15 nums[low] = nums[high]; 16 while(low < high && key >= nums[low]) low++; 17 nums[high] = nums[low]; 18 } 19 nums[low] = key; 20 return low; 21 } 22 //插入排序 时间复杂度:O(n2-n) 空间复杂度:O(1) 23 void insertSort(vector<int>& nums){ 24 for(int i = 1; i < nums.size(); i++){ 25 int temp = nums[i]; 26 for(int j = i-1; j >= 0; j--){ 27 if(temp < nums[j]){ 28 nums[j+1] = nums[j]; 29 nums[j] = temp; 30 } 31 else{ 32 break; 33 } 34 } 35 } 36 } 37 //希尔排序 时间复杂度:O(n2-n) 空间复杂度:O(1) 38 void shellSort(vector<int>& nums){ 39 for(int d = nums.size()/2; d>=1; d = d/2){ 40 for(int i = d; i < nums.size(); i++){ 41 int temp = nums[i]; 42 for(int j = i-d; j >= 0; j-=d){ 43 if(temp < nums[j]){ 44 nums[j+d] = nums[j]; 45 nums[j] = temp; 46 } 47 else{ 48 break; 49 } 50 } 51 } 52 } 53 } 54 //选择排序 时间复杂度:O(n2) 空间复杂度:O(1) 55 void selectSort(vector<int>& nums){ 56 int min = 0; 57 for(int i = 0; i < nums.size(); i++){ 58 int min = i; 59 for(int j = i+1; j < nums.size(); j++){ 60 if(nums[min] > nums[j]){ 61 min = j; 62 } 63 } 64 if(min != i) swap(nums[min], nums[i]); 65 } 66 } 67 //冒泡排序 时间复杂度:O(n2) 空间复杂度:O(1) 68 void bubleSort(vector<int>& nums){ 69 for(int i = 0; i < nums.size(); i++){ 70 for(int j = 1; j < nums.size(); j++){ 71 if(nums[j] < nums[j-1]) { 72 swap(nums[j], nums[j-1]); 73 } 74 } 75 } 76 } 77 //归并排序 时间复杂度:O(nlgn) 空间复杂度:O(n) 78 void merge(vector<int>& nums, int low, int high){ 79 if(low >= high) return; 80 int mid = low + (high - low) / 2; 81 merge(nums, low, mid); 82 merge(nums, mid+1, high); 83 mergeSort(nums, low, mid, high); 84 } 85 void mergeSort(vector<int>& nums, int low, int mid, int high){ 86 int l = low, m = mid+1, h = high; 87 vector<int> temp(high - low + 1); 88 int k = 0; 89 while(l <= mid && m <= high){ 90 if(nums[l] < nums[m]){ 91 temp[k++] = nums[l++]; 92 }else{ 93 temp[k++] = nums[m++]; 94 } 95 } 96 while(l <= mid) temp[k++] = nums[l++]; 97 while(m <= high) temp[k++] = nums[m++]; 98 for(int i = 0; i < temp.size(); i++){ 99 nums[i+low] = temp[i]; 100 } 101 } 102 //桶排 时间复杂度:O(n+c) 空间复杂度:O(n) 103 void bucketSort(vector<int>& nums){ 104 vector<int> bk(50000); 105 for(auto num : nums){ 106 bk[num]++; 107 } 108 int k = 0; 109 for(int i = 0; i < bk.size(); i++){ 110 while(bk[nums[i]]){ 111 nums[k++] = bk[i]; 112 bk[i]--; 113 } 114 } 115 } 116 //基数排序 时间复杂度:O(d(r+n)-d(n+rd)) 空间复杂度:O(n+rd) 117 void countSort(vector<int>& nums){ 118 int max = INT_MIN, min = INT_MAX; 119 for(int num : nums){ 120 if(num > max) max = num; 121 if(num < min) min = num; 122 } 123 vector<int> bk(max-min+1); 124 for(int num : nums){ 125 bk[num-min]++; 126 } 127 int k = 0; 128 for(int n = min; n <= max; n++){ 129 int cnt = bk[n-min]; 130 while(cnt--){ 131 nums[k++] = n; 132 } 133 } 134 } 135 //堆排序 时间复杂度:O(nlgn) 空间复杂度:O(1) 136 void heapify(vector<int>& nums, int index, int len){ 137 int left = 2*index+1, right = 2*index+2; 138 int maxindex = index; 139 if(left < len && nums[left] > nums[maxindex]) maxindex = left; 140 if(right < len && nums[right] > nums[maxindex]) maxindex = right; 141 if(index != maxindex){ 142 swap(nums[index], nums[maxindex]); 143 heapify(nums, maxindex, len); 144 }else{ 145 return; 146 } 147 } 148 void heapSort(vector<int>& nums){ 149 int n = nums.size(); 150 for(int i = n/2; i >=0; i--){ 151 heapify(nums, i, n); 152 } 153 for(int j = n-1; j >= 1; j--){ 154 swap(nums[0], nums[j]); 155 heapify(nums, 0, j); 156 } 157 } 158 159 160 vector<int> sortArray(vector<int>& nums) { 161 if(nums.empty()) return nums; 162 //func(nums, 0, nums.size()-1); 163 //insertSort(nums); 164 //selectSort(nums); 165 //bubleSort(nums); 166 //merge(nums, 0, nums.size()-1); 167 //bubleSort(nums); 168 //countSort(nums); 169 //heapSort(nums); 170 shellSort(nums); 171 return nums; 172 } 173 };