一、冒泡排序
#include <iostream>
using namespace std;
void bubble_sort(int str[], int n);
int main() {
int str[10] = { 3, 5, 2, 4, 1, 7, 9, 8, 0, 6 };
bubble_sort(str, sizeof(str)/4);
for (int i = 0; i < sizeof(str)/4; i++)
printf("%d ", str[i]);
return 0;
}
void bubble_sort(int str[], int n) {
int temp;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) { //大的往后排
if (str[j] > str[j + 1]) {
temp = str[j];
str[j] = str[j + 1];
str[j + 1] = temp;
}
}
}
}
二、选择排序:
#include <iostream>
using namespace std;
void selection_sort(int str[], int n);
int main() {
int str[10] = { 3, 5, 2, 4, 1, 7, 9, 8, 0, 6 };
selection_sort(str, sizeof(str)/4);
for (int i = 0; i < sizeof(str)/4; i++)
printf("%d ", str[i]);
return 0;
}
void selection_sort(int str[], int n) {
int temp, minIndex;
for (int i = 0; i < n; i++) {
minIndex = i;
for (int j = i + 1; j < n; j++) {
if (str[j] < str[minIndex]) { //找出最小的数索引
minIndex = j;
}
}
temp = str[minIndex];
str[minIndex] = str[i];
str[i] = temp;
}
}
三、插入排序
#include <iostream>
using namespace std;
void insert_sort(int str[], int n);
int main() {
int str[10] = { 3, 5, 2, 4, 1, 7, 9, 8, 0, 6 };
insert_sort(str, sizeof(str)/4);
for (int i = 0; i < sizeof(str)/4; i++)
printf("%d ", str[i]);
return 0;
}
void insert_sort(int str[], int n) {
for (int i = 1; i < n; i++) {
int select = str[i];
for (int j = i - 1; j >=0; j--) {
if (str[j] > select && j == 0) { //选择数为最小排序列首
str[j + 1] = str[j];
str[j] = select;
break;
}
else if (str[j] > select && j != 0)
str[j + 1] = str[j];
else if(str[j] <= select){ // 选择数位于序列中间
str[j + 1] = select;
break;
}
}
}
}
或者
void insert_sort(int str[], int n) {
for (int i = 0; i < n; i++) {
int j = i - 1;
int select = str[i];
while (j >= 0 && str[j] > select) {
str[j + 1] = str[j];
j--;
}
str[j + 1] = select;
}
}
四、希尔排序
#include <iostream>
using namespace std;
void shell_sort(int str[], int n);
int main() {
int str[10] = { 3, 5, 2, 4, 1, 7, 9, 8, 0, 6 };
shell_sort(str, sizeof(str)/4);
for (int i = 0; i < sizeof(str)/4; i++)
printf("%d ", str[i]);
return 0;
}
void shell_sort(int str[], int n) {
int gap = n;
while(gap > 0){
gap /= 2;
for (int k = 0; k < gap; k++) { //k个组需要直接插入排序
for (int i = k; i < n; i += gap) {
int j = i - gap;
int select = str[i];
while (j >= 0 && str[j] > select) {
str[j + gap] = str[j];
j -= gap;
}
str[j + gap] = select;
}
}
}
}
五、归并排序
#include <iostream>
#include <vector>
using namespace std;
vector<int> merge_sort(vector<int> vec);
vector<int> merge(vector<int> str1, vector<int> str2);
int main() {
vector<int> vec = { 3, 5, 2, 4, 1, 7, 9, 8, 0, 6 };
vec = merge_sort(vec);
for (int i = 0; i < vec.size(); i++)
printf("%d ", vec[i]);
return 0;
}
vector<int> merge_sort(vector<int> vec) {
int mid = vec.size() / 2;
vector<int> str1, str2;
if (mid == 0)
return vec;
//左右数组分配
for (int i = 0; i < mid; i++) {
str1.push_back(vec[i]);
}
for (int i = 0; i < vec.size() - mid; i++) {
str2.push_back(vec[mid + i]);
}
return merge(merge_sort(str1), merge_sort(str2));
}
vector<int> merge(vector<int> str1, vector<int> str2) {
vector<int> result;
int i = 0, j = 0;
while (i < str1.size() && j < str2.size()) {
if (str1[i] < str2[j]) {
result.push_back(str1[i]);
i++;
}
else {
result.push_back(str2[j]);
j++;
}
}
while (i < str1.size()) {
result.push_back(str1[i]);
i++;
}
while (j < str2.size()) {
result.push_back(str2[j]);
j++;
}
return result;
}
归并的参数变形:
#include <iostream>
#include <vector>
using namespace std;
vector<int> merge_sort(vector<int> vec, int left, int right);
vector<int> merge(vector<int> str1, vector<int> str2);
int main() {
vector<int> vec = { 3, 5, 2, 4, 1, 7, 9, 8, 0, 6 };
vec = merge_sort(vec, 0, 9);
for (int i = 0; i < vec.size(); i++)
printf("%d ", vec[i]);
return 0;
}
vector<int> merge_sort(vector<int> vec, int left, int right) {
if (left == right) {
return vector<int>(1,vec[left]);
}
int mid = left + right >> 1;
return merge(merge_sort(vec, left, mid), merge_sort(vec, mid+1, right));
}
vector<int> merge(vector<int> str1, vector<int> str2) {
vector<int> result;
int i = 0, j = 0;
while (i < str1.size() && j < str2.size()) {
if (str1[i] < str2[j]) {
result.push_back(str1[i]);
i++;
}
else {
result.push_back(str2[j]);
j++;
}
}
while (i < str1.size()) {
result.push_back(str1[i]);
i++;
}
while (j < str2.size()) {
result.push_back(str2[j]);
j++;
}
return result;
}
六、快速排序
#include <iostream>
#include <vector>
using namespace std;
void quick_sort(vector<int>& nums, int l, int r) {
if (l >= r) return;
int i = l, j = r, x = nums[l + r >> 1];//这里x选随机最好
while (i < j){
while (nums[i] < x) i++;
while (nums[j] > x) j--;
if (i < j)
swap(nums[i], nums[j]);
else
quick_sort(nums, l, j), quick_sort(nums, j+1, r);
}
}
int main(){
vector<int> nums = { 9, 7, 8, 3, 1, 4, 5, 0, 6,2 };
quick_sort(nums, 0, nums.size() - 1);
for (int i = 0; i < nums.size(); i++){
cout << nums[i];
}
}
待更新。。。