【算法笔记】4.1排序

版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/xunalove/article/details/87889708

简单选择排序

对一个序列中的元素a[0] ~ a[i],令i从0到n-1枚举,进行n趟操作,每趟从
待排序部分[i,n-1]中选择最小的元素,令其与待排序部分的第一个元素a[i]
进行交换,这样元素a[i]就会与当前有序区间[0,i-1]形成新的有序区间[0,i]。
于是在进行n趟操作后,所有元素就会有序。

/*
简单选择排序
*/
#include<stdio.h>
#include<string.h>
int n=10;
void selectSort(int a[]){
    for(int i=0; i<n; i++){//进行n趟操作
        int k=i;
        for(int j=i; j<n; j++){//选出[i, n]中最小的元素,下标为k
            if(a[j] < a[k]){//交换位置
                int  temp = a[k];
                a[k] = a[j];
                a[j] = temp;
            }
        }
    }
}
int main()
{
    int a[10] = {1,2,4,6,8,3,5,7,9,0};
    selectSort(a);
    for(int i=0; i<10; i++)
        printf("%d ",a[i]);
    printf("\n");

}
/*
运行结果
0 1 2 3 4 5 6 7 8 9

*/


插入排序

将待插入元素一个个插入初始已有序部分中的过程,而插入位置的选择遵循了使插入后仍保存有序的原则,具体做法一般是从后往前枚举已有序部分来确定插入位置。

/*
插入排序
*/
#include<stdio.h>
#include<string.h>

void inselectSort(int a[], int n){
     for(int i=1; i<n; i++)     //进行n-1趟排序
     {
         int temp = a[i], j=i;  //temp临时存放a[i],j从i开始往前枚举
         while(j > 0 && a[j-1] > temp) //只要temp小于前一个元素a[j-1]
         {
             a[j] = a[ j - 1];//把a[j-1]后羿一位至a[j]
             j--;
         }
         a[j] = temp;  //插入位置为j
     }
}
int main()
{
    int a[6] = {5,2,4,6,3,1};
    inselectSort(a, 6);
    for(int i=0; i<6; i++)
        printf("%d ", a[i]);
    printf("\n");
    return 0;
}
/*
运行结果
1 2 3 4 5 6
*/


sort()函数

  1. 如何使用sort排序?
/*
sort(首元素地址(必填), 尾元素地址的下一个地址(必填), 比较函数(非必填));
如果不写比较函数,则默认对前面给出的区间进行递增排序。
*/
#include<stdio.h>
#include<string.h>

//sort()使用的头文件
#include<algorithm>
using namespace std;
int main()
{
    // int型 将a[0] ~ a[5]从小到大排序
    int a[6] = {9, 4, 2, 5, 6, -1};
    sort(a, a+6);
    for(int i=0; i<6; i++)
        printf("%d ", a[i]);
    printf("\n");


    //double型数组排序
    double b[] = {1.4, -2.1, 9};
    sort(b, b+3);
    for(int i=0; i<3; i++)
        printf("%.1f ", b[i]);
    printf("\n");


    //char型数组排序(默认为字典序):
    char c[] = {'T','W','A','K'};
    sort(c, c+4);
    for(int i=0; i<4; i++)
        printf("%c ",c[i]);
    printf("\n");
    return 0;
}
/*
运行结果
-1 2 4 5 6 9
-2.1 1.4 9.0
A K T W
*/


  1. 如何实现比较函数cmp
/*
(1)基本数据类型数组的排序
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
//大降小升(默认小)
bool cmp(int a, int b){//从大到小
    return a > b;  //可以理解为当a > b时把a放在b前面
}
int main()
{
    int a[6] = {9, 4, 2, 5, 6, -1};
    //sort(a, a+6);                   //默认从小到大
    sort(a, a+6, cmp);
    for(int i=0; i<6; i++)
        printf("%d ", a[i]);
    printf("\n");

    return 0;
}
/*
运行结果
9 6 5 4 2 -1
*/

/*
(2)结构体数组的排序
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node{
    int x, y;
}ssd[10];
//按照x从大到小排序(一级排序)
bool cmp(node a, node b)
{
    return a.x > b.x;
}

//先按照x从大到小排序,但当x相等的情况下,按照y的大小从小到大来排序(二级排序)
bool cmp1(node a, node b )
{
    if(a.x!=b.x) return a.x > b.x;
    else return a.y < b.y;
}
int main()
{
    ssd[0].x=2; ssd[0].y=2;
    ssd[1].x=1; ssd[1].y=3;
    ssd[2].x=3; ssd[2].y=1;
    sort(ssd, ssd + 3, cmp);//排序
    for(int i=0; i<3; i++)
        printf("%d %d\n",ssd[i].x, ssd[i].y);


    ssd[0].x=2; ssd[0].y=2;
    ssd[1].x=1; ssd[1].y=3;
    ssd[2].x=2; ssd[2].y=1;
    sort(ssd, ssd + 3, cmp1);//排序
    for(int i=0; i<3; i++)
        printf("%d %d\n",ssd[i].x, ssd[i].y);
    return 0;
}
/*运行结果
3 1
2 2
1 3

2 1
2 2
1 3

*/


/*
(3)容器的排序
在STL标准容器中,只有vector,string,deque是可以使用sort的。
这是因为像set,map这种容器使用红黑树实现的,元素本身有序,
故不允许使用sort排序。
*/
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool cmp(int a, int b){//因为vector中的int元素为int型,因此仍然是int的比较
    return a > b;
}
int main()
{
    //vector的排序
    vector<int>vi;
    vi.push_back(3);
    vi.push_back(1);
    vi.push_back(2);
    sort(vi.begin(), vi.end(), cmp);//对整个vector排序
    for(int i=0; i<3; i++)
    {
        printf("%d ", vi[i]);
    }
    printf("\n");
    //运行结果  3 2 1


    //string的排序
    string str[3] = {"bbbb", "cc", "aaa"};
    sort(str, str+3);//将string型数组按字典序从小到大输出
    for(int i=0; i<3; i++)
        cout<<str[i]<<endl;
    /*运行结果
     aaa
     bbbb
     cc
    */
    return 0;
}




猜你喜欢

转载自blog.csdn.net/xunalove/article/details/87889708
今日推荐