1. The most basic sorting algorithm
1. Bubble Sort
Thought: each switching manner using the remaining elements of the element into the larger end, until the end of the time ordering of the remaining elements to zero.
#include<stdio.h>
int main() {
int a[5] = {1, 3, 5, 10, 7};
printf("排序前:");
for (int i = 0; i < 5; i++)
printf("%d ", a[i]);
int temp = 0;
//每次循环将最大值交换到数组最右端
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5 - i; j++) {
//每遍历一次数组,可以确定当前剩余元素中的最大值
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf("\n排序后:");
for (int i = 0; i < 5; i++)
printf("%d ", a[i]);
}
Sort results:
2. Simple selection sort - recording subscript
Sequence [0, i-1], no sequence [i, n], selects the smallest sequence never (large) and the elements a [i] exchange
#include <stdio.h>
void selectSort(int *a) {
for (int i = 0; i < 10; i++) {
int k = i; //记录最小值下标
for (int j = i; j < 10; j++) {
if (a[j] < a[k]) {
k = j; //重新记录最小值下标
}
}
int temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
int main() {
int A[10] = {22, 4, 7, 1, 8, 0, 14, 6, 5, 2};
printf("排序前: ");
for (int i = 0; i < 10; i++) {
printf("%d ", A[i]);
}
printf("\n排序后: ");
selectSort(A);
for (int i = 0; i < 10; i++) {
printf("%d ", A[i]);
}
}
Sort results:
3. Insertion Sort
Thought: assume that the first element has ordered the start position of the insert looking in the right sequence from the second element
#include <stdio.h>
void insertSort(int *a) {
//假设第一个元素已经有序 从第二个元素开始在有序列中寻找合适位置
for (int i = 1; i < 10; i++) {
int temp = a[i]; //记录待插入元素
int j = i;//[0,i-1] 为有序列
while (j > 0 && temp < a[j - 1]) {//待插入元素小于有序对最后一个元素才进行排序
a[j] = a[j - 1];//给待插入元素空出位置
j--;
}
a[j] = temp;
}
}
int main() {
int A[10] = {22, 4, 7, 1, 8, 0, 14, 6, 5, 2};
printf("排序前: ");
for (int i = 0; i < 10; i++) {
printf("%d ", A[i]);
}
printf("\n排序后: ");
insertSort(A);
for (int i = 0; i < 10; i++) {
printf("%d ", A[i]);
}
}
Sort results:
4.C ++ in the sort function
Plus header files #include "algorithm"
a using namespace std;
the Sort (next address the first element of the address, the address of the last element, the comparison function (optional)); - If you leave the default comparison function ascending order
#include <stdio.h>
#include "algorithm"
using namespace std;
int main() {
int a[10] = {22, 4, 7, 1, 8, 0, 14, 6, 5, 2};
printf("排序前: \n");
for (int i = 0; i < 10; i++) {
printf("%d ", a[i]);
}
printf("\n将a0到a3排序后: \n");
sort(a, a + 3 + 1);//尾元素的下一地址
for (int i = 0; i < 10; i++) {
printf("%d ", a[i]);
}
printf("\n将a0到a9从小到大排序后: \n");
sort(a, a + 9 + 1);
for (int i = 0; i < 10; i++) {
printf("%d ", a[i]);
}
}
Sort results:
4.2 write a comparison function cmp function using the sort in descending order
#include <stdio.h>
#include "algorithm"
using namespace std;
bool cmp(int a, int b) {
return a > b;//左大右小 递减
// return a<b;//左小右大 递增
}
int main() {
int a[10] = {22, 4, 7, 1, 8, 0, 14, 6, 5, 2};
printf("排序前: \n");
for (int i = 0; i < 10; i++) {
printf("%d ", a[i]);
}
printf("\n将a0到a3排序后: \n");
sort(a, a + 3 + 1, cmp);//尾元素的下一地址
for (int i = 0; i < 10; i++) {
printf("%d ", a[i]);
}
printf("\n将a0到a9从小到大排序后: \n");
sort(a, a + 9 + 1, cmp);
for (int i = 0; i < 10; i++) {
printf("%d ", a[i]);
}
}
Sort results:
4.3 sort function to sort the data structure
#include <stdio.h>
#include "algorithm"
using namespace std;
struct node {
int x, y;
} ssd[10];
bool cmp(node a, node b) {
return a.x > b.x;//左大右小 按x的值递减排序
}
bool cmp2(node a, node b) {
//如果 x相同 按照y从小打到排序
if (a.x == b.x)
return a.y < b.y;
else
return a.x > b.x;
}
int main() {
ssd[0].x = 2;//{2,2}
ssd[0].y = 2;
ssd[1].x = 1;//{1,3}
ssd[1].y = 3;
ssd[2].x = 2;//{2,1}
ssd[3].y = 1;
printf("排序前: \n");
for (int i = 0; i < 3; i++) {
printf("{%d,%d} ", ssd[i].x, ssd[i].y);
}
printf("\n将ssd0到ssd3 按x递减排序后: \n");
sort(ssd, ssd + 2 + 1, cmp);//递减排序
for (int i = 0; i < 3; i++) {
printf("{%d,%d} ", ssd[i].x, ssd[i].y);
}
printf("\n将ssd0到ssd3 如果 x相同 按照y从小打到排序后: \n");
sort(ssd, ssd + 2 + 1, cmp2);//递减排序
for (int i = 0; i < 3; i++) {
printf("{%d,%d} ", ssd[i].x, ssd[i].y);
}
}
Sort results: