排序算法
排序方法 | 平均情况 | 最好情况 | 最坏情况 | 辅助空间 | 稳定性 |
---|
冒泡排序 | O(n^2) | O(n) | O(n^2) | O(1) | 稳定 |
简单选择排序 | O(n^2) | O(n^2) | O(n^2) | O(1) | 稳定 |
直接插入排序 | O(n^2) | O(n) | O(n^2) | O(1) | 稳定 |
希尔排序 | O(nlogn)~O(n^2) | O(n^1.3) | O(n^2) | O(1) | 不稳定 |
堆排序 | O(nlogn) | O(nlogn) | O(nlogn) | O(1) | 不稳定 |
归并排序 | O(nlogn) | O(nlogn) | O(nlogn) | O(n) | 稳定 |
快速排序 | O(nlogn) | O(nlogn) | O(n^2) | O(nlogn)~O(n) | 不稳定 |
冒泡排序
void BubbleSort(vector<int>& A){
int size = A.size();
for (int i = 0; i < size; i++){
for (int j = 0; j <size-1-i; j++) {
if (A[j] > A[j+1]){
swap(A[j+1], A[j]);
}
}
}
}
void BubbleSort(vector<int>& A) {
int size = A.size();
for (int i = 0; i < size; i++){
bool flag = false;
for (int j = 0; j <size-1-i; j++) {
if (A[j] > A[j+1]){
swap(A[j+1], A[j]);
flag = true;
}
}
if (flag == false)
break;
}
}
简单选择排序
void SelectSort(vector<int>& A){
int size = A.size();
for (int i = 0; i < size; i++){
int min = i;
for (int j = i + 1; j < size; j++){
if (A[j] < A[min])
min = j;
}
if (min != i)
swap(A[i], A[min]);
}
}
直接插入排序
void InsertSort(vector<int>& A){
for (int i = 1; i < A.size(); i++) {
int insert = A[i];
int j = i - 1;
while (j >= 0 && A[j] > insert) {
A[j + 1] = A[j];
j--;
}
A[j + 1] = insert;
}
}
void ShellSort(vector<int>& A) {
int size = A.size();
int j,tmp;
for (int increment=size/2; increment> 0; increment /= 2)
for (int i = increment; i < size; i++) {
tmp = A[i];
for ( j = i; j >= increment; j -= increment)
if (tmp < A[j - increment])
A[j] = A[j - increment];
else
break;
A[j] = tmp;
}
}
堆排序
int Parent(int i){
return (i + 1) / 2 - 1;
}
int Left(int i){
return i * 2 + 1;
}
int Right(int i){
return i * 2 + 2;
}
void MaxHeapify(vector<int> &A, int i, int size){
int l = Left(i), r = Right(i);
int largest;
if (l < size&& A[l] > A[i])
largest = l;
else
largest = i;
if (r < size&&A[r] > A[largest])
largest = r;
if (largest != i) {
swap(A[i], A[largest]);
MaxHeapify(A, largest, size);
}
}
void BuildMaxHeap(vector<int> &A){
int sizes = A.size();
for (int i = (sizes - 1) / 2; i >= 0; i--) {
MaxHeapify(A, i, sizes);
}
}
void HeapSort(vector<int> &A){
BuildMaxHeap(A);
int size = A.size();
for (int i = size - 1; i > 0; i--) {
swap(A[0], A[i]);
size--;
MaxHeapify(A, 0, size);
}
}
归并排序
void Merge(vector<int> &A, int p, int q, int r){
vector<int> L;
vector<int> R;
for (int i = p; i <=q; i++)
L.push_back(A[i]);
for (int i = q+1; i <=r; i++)
R.push_back(A[i]);
L.push_back(INT_MAX);
R.push_back(INT_MAX);
int i = 0, j = 0;
for (int k = p; k <=r; k++){
if (L[i] <= R[j]){
A[k] = L[i];
i++;
}
else{
A[k] = R[j];
j++;
}
}
}
void MergeSort(vector<int> &A, int p, int r){
if (p < r){
int q = (p + r)>>1;
MergeSort(A, p, q);
MergeSort(A, q + 1, r);
Merge(A, p, q, r);
}
}
快速排序
void QuickSort(vector<int>& A, int p, int r){
if (p < r) {
int x = A[r];
int i = p - 1;
for (int j = p; j < r; j++) {
if (A[j] <= x) {
i++;
swap(A[i], A[j]);
}
}
swap(A[i + 1], A[r]);
int q = i + 1;
QuickSort(A, p, q - 1);
QuickSort(A, q + 1, r);
}
}
void QuickSort(vector<int>& A){
int p = 0;
int r = A.size() - 1;
QuickSort(A, p, r);
}