各种排序(冒泡排序/归并排序/快速排序····)-C/C++
(题外话:CSDN什么时候能支持截屏啊,这输出结果还得保存本地图片再插入,也太麻烦了。所以以下必要的输出结果都采用复制粘贴。不必要的,就不展示了。//部分代码为手打,未运行,可能存在错误。)
1-sort()
自带函数。需要头文件" #include "和 “using namespace std;”
格式:sort(首元素地址(必填),尾元素地址的下一个地址(必填),比较函数(非必填));,比较函数不填则默认按递增排序。
程序1:一维数组排序,不含比较函数
#include<iostream>
#include <string>
#include<algorithm>
using namespace std;
int main()
{
int A[10] = {9,8,7,5,6,4,2,10,60,21};
sort(A,A+10);
cout << A[0] << endl;
cout << A[1] << endl;
cout << A[2] << endl;
cout << A[3] << endl;
cout << A[4] << endl;
cout << A[5] << endl;
cout << A[6] << endl;
cout << A[7] << endl;
cout << A[8] << endl;
cout << A[9] << endl;
system("pause");
return 0;
}
输出结果:
2
4
5
6
7
8
9
10
21
60
请按任意键继续. . .
程序2:二维数组(结构体实现)排序,含比较函数。
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
struct node {
int x;
int y;
};
bool cmp(node n1, node n2) {//按照y值从大到小排序
return n1.y > n2.y;
}
node no[500001];
int main() {
int n;
scanf_s("%d", &n);
for (int i = 0; i < n; i++)
{
scanf_s("%d%d", &no[i].x, &no[i].y);
}
sort(no, no + n, cmp);
cout << endl;
for (int i = 0; i < n; i++)
{
printf("%d %d\n", no[i].x, no[i].y)
}
system("pause");
return 0;
}
运行结果:
2-冒泡排序(一维数组)O(n²) 稳定
基本思想:遍历数组,每次把左右相邻的两个数比较大小,左边>右边,则交换位置,否则继续向下比较。n个数共需遍历n-1次。
#include<stdio.h>
int main(){
int a[10]={3,1,5,4,2};
for(int i=1;i<=4;i++){//进行n-1趟
//第i趟时从a[0]到a[n-i-1]都与他们下一个数比较,尾处的数排序后的为最大值,不用比较
for(int j=0;i<5-i;j--){
if(a[j]>a[j+1]){
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(int i=1;i<=4;i++){
printf("%d ",a[i]);
}
return 0;
}
输出结果:
1 2 3 4 5
3-选择排序 (直接选择O(n²))
基本思想:对一个序列A中的元素A[1]-A[n],共进行n次排序,每次选择待排序部分[i,n]中最小的数数,与A[i]交换。
void selectSort(){
int A[10] = {9,8,7,5,6,4,2,10,60,21};
for (int i = 0; i < 10; i++) {//进行n次排序
int k = i;
for (int j = i; j < 10; j++) {//选出[i,n]中最小的数,下标为k
if (A[j] < A[k]) {
k = j;
}
}
int temp = A[i];//交换,使最小的数
A[i] = A[k];
A[k] = temp;
}
return 0;
}
4-插入排序(直接插入排序 O(n²))稳定
基本思想:是将待插入元素一个个插入初始已经有序部分的过程,而插入位置的选择必须保持插入后仍然有序。具体做法一般是从后往前枚举已有序部分来确定插入位置。**
void innerSort(){
int A[10] = {9,8,7,5,6,4,2,10,60,21};
for (int i = 1; i <10; i++) {//进行n-1次排序
int temp = A[i],j=i;//temp存放A[i],j从i开始往前枚举
while (j > 0 && temp < A[j - 1])//只要temp小于前一个元素A[j-1]
{
A[j] = A[j - 1];//把A[j-1]后移一位至A[j]
j--;//依次向前比较
}
A[j] = temp;//插入位置为j
}
return 0;
}
5-归并排序 O(nlogn) 稳定
采用递归实现归并排序。思路:反复将当前区间[left,right]分为两半,对两个子区间[left,mid],[mid+1,right]分别递归进行排序,然后将两个已经排好序的子区间合并为有序序列。
#include<iostream>
#include <string>
#include<algorithm>
using namespace std;
const int maxn = 100;
//将数组A的[L1,R1]与[L2,R2]区间合并为有序区间,L2即R1+1
void merge(int A[],int L1,int R1,int L2,int R2) {
int i = L1, j = L2;//i指向A[L1],j指向A[L2]
int temp[maxn], index = 0;//temp临时存放合并后的数组,index为其下标
while (i <= R1 && j <= R2) {//将两个区间中小的区间放入temp
if (A[i] <= A[j])
{
temp[index++] = A[i++];
}
else
{
temp[index++] = A[j++];
}
}
while (i <= R1) temp[index++] = A[i++];//将剩余区间放入temp
while (j <= R2) temp[index++] = A[j++];//将剩余区间放入temp
for ( i = 0; i < index; i++)
{
A[L1 + i] = temp[i];//将合并后的序列赋值给数组A
}
}
//将array数组当前区间[left,right]进行归并排序
void mergeSort(int A[],int left,int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(A, left, mid);//递归归并排序左区间
mergeSort(A, mid+1, right);//递归归并排序右区间
merge(A,left,mid,mid+1,right);//将左右区间合并
}
}
int main()
{
int A[10] = { 9,8,7,5,6,4,2,10,60,21};
mergeSort(A,0,9);
cout << A[0] << endl;
cout << A[1] << endl;
cout << A[2] << endl;
cout << A[3] << endl;
cout << A[4] << endl;
cout << A[5] << endl;
cout << A[6] << endl;
cout << A[7] << endl;
cout << A[8] << endl;
cout << A[9] << endl;
system("pause");
return 0;
}
输出结果:
2
4
5
6
7
8
9
10
21
60
请按任意键继续. . .
6-快速排序 O(nlogn)
思路:调整序列中的元素,使当前序列最左端的元素在调整后满足左侧元素均比该元素小,右侧元素均比该元素大。对于该元素左侧和右侧的元素分别递归进行上一步调整,直到调整区间长度不超过1。
举例如下图:
#include<iostream>
#include <string>
#include<algorithm>
using namespace std;
const int maxn = 100;
//对区间[left][right]进行划分
int Partition(int A[],int left,int right) {
int temp = A[left];
while (left < right) //只要left与right不相遇
{
while (left < right && A[right] > temp)//反复左移right
{
right--; //将 A[right]移到 A[right]
}
A[left] = A[right];
while (left < right && A[left] <= temp)//反复右移left
{
left++; // 将A[left]移到 A[right]
}
A[right] = A[left];
}
A[left] = temp; //把temp放到left与right相遇的地方,此时left=right
return left;//返回相遇时的下标
}
//快速排序
void quickSort(int A[], int left, int right) {
if (left < right)
{
int pos = Partition(A, left, right);
quickSort(A, left, pos-1);
quickSort(A, pos + 1, right);
}
}
int main()
{
int A[10] = { 9,8,7,5,6,4,2,10,60,21};
quickSort(A,0,9);
cout << A[0] << endl;
cout << A[1] << endl;
cout << A[2] << endl;
cout << A[3] << endl;
cout << A[4] << endl;
cout << A[5] << endl;
cout << A[6] << endl;
cout << A[7] << endl;
cout << A[8] << endl;
cout << A[9] << endl;
system("pause");
return 0;
}
运行结果:
2
4
5
6
7
8
9
10
21
60
请按任意键继续. . .