#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
int a = 1, b = 2;
swap(a, b);
cout << a << ' ' << b << '\n';
vector<int> va = {1,2,3}, vb = {4, 5};
swap(va, vb);
return 0;
}
#include<iostream>
using namespace std;
void swap(int &a, int &b){
int t = a;
a = b;
b = t;
}
int main(){
int a = 1, b = 2;
swap(a, b);
cout << a << ' ' << b << '\n';
return 0;
}
bool is_sorted(const vector<int> &S){
int n = S.size();
for (int i = 0; i + 1 < n; ++i)
if (S[i] > S[i+1])
return false;
return true;
}
void simple_sort(vector<int> &S){
int n = S.size();
while(!is_sorted(S)){
for (int i = 0; i + 1 < n; ++i){
if (S[i] > S[i+1])
swap(S[i], S[i+1]);
}
}
}
void bubble_sort(vector<int> &S){
int n = S.size();
bool is_sorted;
do{
is_sorted = true;
for (int i = 0; i + 1 < n; ++i){
if (S[i] > S[i+1]){
swap(S[i], S[i+1]);
is_sorted = false;
}
}
}while(is_sorted);
}
void bubble_sort(vector<int> &S){
int n = S.size();
for (int t = 0; t < n-1; ++t)
for (int i = 0; i + 1 < n; ++i)
if (S[i] > S[i+1])
swap(S[i], S[i+1]);
}
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
5 | 3 | 6 | 1 | 2 |
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
5 | 3 | 6 | 1 | 2 |
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
5 | 3 | 2 | 1 | 6 |
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
5 | 3 | 2 | 1 | 6 |
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
1 | 3 | 2 | 5 | 6 |
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
1 | 3 | 2 | 5 | 6 |
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
1 | 2 | 3 | 5 | 6 |
int find_max_id(const vector<int> &S, int k){
int res = 0;
for (int i = 1; i < k; ++i)
if (S[i] > S[res])
res = i;
return res;
}
int selection_sort(vector<int> &S){
int n = S.size();
for (int i = n-1; i > 0; --i){
int max_id = find_max_id(S, i);
swap(S[i], S[max_id]);
}
}
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
5 | 3 | 6 | 1 | 2 |
Sorted
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
5 | 3 | 6 | 1 | 2 |
Sorted
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
5 | 3 | 6 | 1 | 2 |
Sorted
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
3 | 5 | 6 | 1 | 2 |
Sorted
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
3 | 5 | 6 | 1 | 2 |
Sorted
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
3 | 5 | 6 | 1 | 2 |
Sorted
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
3 | 5 | 1 | 6 | 2 |
Sorted
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
3 | 1 | 5 | 6 | 2 |
Sorted
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
1 | 3 | 5 | 6 | 2 |
Sorted
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
1 | 3 | 5 | 6 | 2 |
Sorted
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
1 | 3 | 5 | 2 | 6 |
Sorted
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
1 | 3 | 2 | 5 | 6 |
Sorted
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
1 | 2 | 3 | 5 | 6 |
Sorted
void insert(vector<int> &S, int k){
while(k > 0){
if (S[k] < S[k-1])
swap(S[k], S[k-1]);
--k;
}
}
void insertion_sort(vector<int> &S){
int n = S.size();
for(int i = 0; i < n; ++i)
insert(S, i);
}
陣列S
陣列S
陣列A
陣列B
分別對A,B進行排序
陣列S
陣列A
陣列B
分別對A,B進行排序
陣列S
合併排序好的A,B
vector<int> merge_sort(const vector<int> &S){
if (S.size() <= 1)
return S;
int mid = S.size() / 2; //將陣列拆兩半
vector<int> A(S.begin(), S.begin() + mid);
vector<int> B(S.begin() + mid, S.end());
A = merge_sort(A);
B = merge_sort(B);
return merge(A, B); // 怎麼做?
}
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
1 |
---|
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
1 | 2 |
---|
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
1 | 2 | 3 |
---|
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
1 | 2 | 3 | 4 |
---|
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
1 | 2 | 3 | 4 | 5 |
---|
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
1 | 2 | 3 | 4 | 5 | 6 |
---|
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
1 | 2 | 3 | 4 | 5 | 6 | 7 | 10 |
---|
vector<int> merge(const vector<int> &A, const vector<int> &B){
int n = A.size(), m = B.size(), i = 0, j = 0;
vector<int> ans;
while (i < n && j < m)
if (B[j] < A[i])
ans.push_back(B[j++]);
else
ans.push_back(A[i++]);
while (i < n)
ans.push_back(A[i++]);
while (j < m)
ans.push_back(B[j++]);
return ans;
}
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
5 | 3 | 6 | 1 | 2 |
\(i\)
\(j\)
int inversion_count(const vector<int> &S){
int n = S.size(), cnt = 0;
for(int i = 0; i < n; ++i)
for(int j = i+1; j < n; ++j)
if(S[i] > S[j]) ++cnt;
return cnt;
}
陣列S
陣列A
陣列B
分別計算A,B逆序數對數量
陣列S
計算B[i] > A[j]的數量
合併
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
inversion count: 0
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
1 |
---|
inversion count: 4
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
1 | 2 |
---|
inversion count: 4
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
1 | 2 | 3 |
---|
inversion count: 4
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
1 | 2 | 3 | 4 |
---|
inversion count: 6
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
1 | 2 | 3 | 4 | 5 |
---|
inversion count: 8
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
1 | 2 | 3 | 4 | 5 | 6 |
---|
inversion count: 10
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|
inversion count: 10
2 | 3 | 7 | 10 |
---|
1 | 4 | 5 | 6 |
---|
1 | 2 | 3 | 4 | 5 | 6 | 7 | 10 |
---|
inversion count: 10
int inversion_count;
vector<int> merge(const vector<int> &A, const vector<int> &B){
int n = A.size(), m = B.size(), i = 0, j = 0;
vector<int> ans;
while (i < n && j < m)
if (B[j] < A[i]){
ans.push_back(B[j++]);
inversion_count += n - i;
}else
ans.push_back(A[i++]);
while (i < n)
ans.push_back(A[i++]);
while (j < m)
ans.push_back(B[j++]);
return ans;
}
int inversion_count;
vector<int> merge(const vector<int> &A, const vector<int> &B){
int n = A.size(), m = B.size(), i = 0, j = 0;
vector<int> ans;
while (i < n && j < m)
if (B[j] < A[i]){
ans.push_back(B[j++]);
inversion_count += n - i;
}else
ans.push_back(A[i++]);
while (i < n)
ans.push_back(A[i++]);
while (j < m)
ans.push_back(B[j++]);
return ans;
}
int inversion_count(const vector<int> &S){
inversion_count = 0;
merge_sort(S);
return inversion_count;
}
0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|
6 | 3 | 5 | 1 | 3 | 2 |
1 | 2 |
---|
3 | 3 |
---|
6 | 5 |
---|
\(<\)
\(>\)
\(=\)
vector<int> quick_sort(const vector<int> &S){
if(S.size() <= 1) return S;
vector<int> less, equal, greater;
int x = rand() % S.size();
for(int i: S){
if(i < S[x]) less.push_back(i);
else if (i == S[x]) equal.push_back(i);
else greater.push_back(i);
}
less = quick_sort(less);
greater = quick_sort(greater);
for(int i: equal)
less.push_back(i);
for(int i: greater)
less.push_back(i);
return less;
}
#include<algorithm>
using namespace std;
int main(){
int A[] = {7,1,2,2,7,1,2,2};
vector<int> B = {3,2,1,0};
sort(A, A+4);
// A = {1,2,2,7,7,1,2,2}
sort(A, A+8);
// A = {1,1,2,2,2,2,7,7}
sort(B.begin(), B.begin()+2);
// B = {2,3,1,0}
sort(B.begin(), B.end());
// B = {0,1,2,3}
return 0;
}
#include<algorithm>
using namespace std;
int main(){
int A[] = {7,1,2,2,7,1,2,2};
vector<int> B = {3,2,1,0};
stable_sort(A, A+4);
// A = {1,2,2,7,7,1,2,2}
stable_sort(A, A+8);
// A = {1,1,2,2,2,2,7,7}
stable_sort(B.begin(), B.begin()+2);
// B = {2,3,1,0}
stable_sort(B.begin(), B.end());
// B = {0,1,2,3}
return 0;
}
void stoogeSort(int s[], int L, int R){
if (s[R] < s[L]) swap(s[L], s[R]);
if (R-L <= 1) return;
int p = (R-L+1) / 3;
stoogeSort(s, L, R-p);
stoogeSort(s, L+p, R);
stoogeSort(s, L, R-p);
}