一、题目
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note: You are not suppose to use the library's sort function for this problem.
Example:
Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Follow up:
- A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's. - Could you come up with a one-pass algorithm using only constant space?
二、解答
本题目实际上考察的而是排序问题,这里总结了7种排序算法的C++经典写法。7种排序算法分别是冒泡,简单选择,直接插入,希尔排序,堆排序、归并排序、快排。
分别的复杂度,及稳定性为下表所示
排序算法 | 平均情况 | 最好情况 | 最差情况 | 辅助空间 | 稳定性 |
冒泡 | O(n2) | O(n) | O(n2) | O(1) | 稳定 |
简单选择 | O(n2) | O(n2) | O(n2) | O(1) | 不稳定 |
直接插入 | O(n2) | O(n) | O(n2) | O(1) | 稳定 |
希尔排序 | O(nlogn)~O(n2) | o(n1.3) | O(n2) | O(1) | 不稳定 |
堆排序 | O(nlogn) | O(nlogn) | O(nlogn) | O(1) | 不稳定 |
归并 | O(nlogn) | O(nlogn) | O(nlogn) | O(n) | 稳定 |
快排 | O(nlogn) | O(nlogn) | O(n2) | O(logn)~O(n) | 不稳定 |
稳定性记忆方法:一起(希)快(快排)选(简单选择)排队(堆)
下面是各种排序的代码
class Solution { public: void sortColors(vector<int>& nums) { //method 1; Bubble sort O(n2) 最好O(n)最差O(n2) beat 22% // if(nums.empty()) // return; // int n = nums.size(); // bool flag = true; // for(int i = 0; i != n && flag; ++i) // { // flag = false; // for(int j = n - 2; j >= i; --j)//从后面开始循环 // { // if(nums[j] < nums[j + 1]) // { // swap(nums[j + 1], nums[j]); // flag = true; // } // } // } // return; //METHOD 2:simple select sort 简单选择排序 找到最小的再进行交换 O(n2) 最好O(n2) 最差O(n2) beat 99% // if(nums.empty()) // return; // int n = nums.size(); // for(int i = 0; i != n; ++i) // { // int min = i; // for(int j = i + 1; j != n; ++j) // { // if(nums[j] < nums[min]) // min = j; // } // if(i != min) // swap(nums[i], nums[min]); // } // return; //MEHTOD 3: straight insertion sort 直接插入 O(n2) 最好O(n)最差O(n2)beat 100% // if(nums.empty()) // return ; // for(int i = 1; i != nums.size(); ++i) // { // if(nums[i] < nums[i - 1]) // { // int temp = nums[i]; // int j;//方便后面的j处理 // for(j = i - 1; nums[j] > temp; --j) // nums[j + 1] = nums[j]; // nums[j + 1] = temp;//切记此时的时候是nums[j+ 1]没有用了,要覆盖也是覆盖j+ 1 // } // } // return ; //method 4: shell sort O(nlogn) - O(n2) 最好O(n1.3)最差O(n2) beat 100% // if(nums.empty()) // return; // int n = nums.size(); // int increment = n; // do // { // increment = increment / 3 + 1; // for(int i = increment; i < n; ++i) // { // if(nums[i] < nums[i - increment]) // { // int temp = nums[i]; // int j = i - increment;//初始化 // for(j = i - increment; j >= 0 && nums[j] > temp; j -= increment) // nums[j + increment] = nums[j]; // nums[j+ increment] = temp; // } // } // } // while(increment > 1); // return; //Method 5: Heap sort 三个都是O(nlogn)//简单选择的扩展,每次记录比较的结果 升序一般是大顶堆, 降序一般是小顶堆 堆一定是完全二叉树 // if(nums.empty()) // return; // int n = nums.size(); // // create // for(int i = n / 2 - 1; i >= 0; --i)//最大的具有子孩子的节点是n/2-1 // { // adjustHeap(nums, i, n); // } // //adjust // for(int i = n - 1; i >= 0; --i) // { // swap(nums[0], nums[i]);//将堆顶元素与末尾元素交换 // adjustHeap(nums, 0, i);//调整剩下的i长度的元素为大顶堆 // } // return; //METHOD 6: merge sort 三个都是O(nlogn) 辅助空间是O(n)稳定 // if(nums.empty()) // return; // int n = nums.size(); // vector<int> res(n);//辅助空间 // merge_sort(nums, res, 0, n -1); // return; //METHOD 7: quick sort 最好 平均O(nlogn) 最坏是O(n2) if(nums.empty()) return; int n = nums.size(); quick_sort(nums, 0, n-1); return; } //the subfunction of Heap sort: adjust heap function // void adjustHeap(vector<int> & nums, int i, int len) // { // int temp = nums[i]; // for(int k = 2 * i + 1; k < len; k = 2 * k + 1)//i是父节点,k = 2*i+1表示父节点i的左孩子 以后仍递增为其左孩子 // { // if(k + 1 < len && nums[k] < nums[k + 1])//若存在右孩子的,找左右孩子的最大值作为当前k // ++k; // if(nums[k] > temp)//如果子节点大于父节点,将子节点值赋给父节点(不用进行交换) // { // nums[i] = nums[k]; // i = k; //i记录了每次的最大值 节点的索引, 后面只需要交换一次,将最小值temp给索引i的节点 // } // } // nums[i] = temp;//直交换一次,最小值赋值给上面原最大值 节点的索引处 // return; // } //the subfunction 1 of merge sort: divide-agency // void merge_sort(vector<int> & nums, vector<int> & res, int left, int right) // { // if(left < right)//递归截止条件 // { // int mid = left + (right - left) /2; // //divide // merge_sort(nums, res, left, mid); // merge_sort(nums, res, mid + 1, right); // //agency // merge(nums, res, left, right, mid); // } // } //the subfunction 2 of merge sort: merge sort function // void merge(vector<int> & nums, vector<int> & res, int left, int right, int mid) // { // int i = left;//左半边序列的起始元素索引[left, mid] // int j = mid+ 1;//右半边序列的起始元素索引[mid+1, right] // int t = 0;//中间变量,每次t从0开始存储 // while(i <= mid && j <= right) // { // res[t++] = (nums[i] <= nums[j]) ? nums[i++] : nums[j++]; // } // while(i <= mid || j <= right) // { // res[t++] = (i <= mid) ? nums[i++] : nums[j++];//注意i == mid 说明nums[i]用完了,此时应该赋值nums[j++] // } // //将排好序的序列赋回原值 // t = 0; // while(left <= right)//left递增到right停止 // { // nums[left++] = res[t++];//中间变量,每次t从0开始赋值给left // } // return; // } //the subfunction of quick sort: quick sort void quick_sort(vector<int> & nums, int left, int right) { int pivot; if(left < right) { pivot = findPivot(nums, left, right); quick_sort(nums, left, pivot - 1); quick_sort(nums,pivot + 1, right); } } //the subfunction of quick sort: quicl sork int findPivot(vector<int> & nums, int left, int right) { //int mid = left + (right - left) / 2; int pivot = nums[left]; while(left != right) { while(left != right && nums[right] >= pivot) --right; nums[left] = nums[right]; while(left != right && nums[left] <= pivot) ++left; nums[right] = nums[left]; } nums[left] = pivot; return left; } };