排序算法—选择排序

选择排序

C++中的排序函数

sort函数的参数

sort(start_address,end_address,sort_handler)

start_address:排序数据的起始地址

end_address:排序数据的终止地址

sort_handler:排序方法

需要包含头文件: algorithm

sort_handler:排序方法

sort函数例子

#include <iostream>
#include <algorithm>

using namespace std;

int main(){
    int a[10] = {23,63,1,2,67,12,15,56,78,23};
    
    sort(a,a+10);

    for (int i = 0;i < 10;i++){
        cout << a[i] << ",";
    }
}
1,2,12,15,23,23,56,63,67,78,

sort函数例子—从大到小

#include <iostream>
#include <algorithm>

using namespace std;

bool campare(int a,int b){
    return a>b;
}


int main(){
    int a[10] = {23,63,1,2,67,12,15,56,78,23};
    
    sort(a,a+10,campare);

    for (int i = 0;i < 10;i++){
        cout << a[i] << ",";
    }
}
78,67,63,56,23,23,15,12,2,1

选择排序思想

假设,我们现在有这样的一个列表:[6,5,1,3,4],那么选择排序算法会如何进行从小到大排序呢?

1.选择整个列表中选择一个最小的数:1,并将它放在最前面的位置,结果:[1,6,5,3,4]

2.由于1已经固定,因此从后面四个数中选择一个最小的数:3,将它放在1的后面,结果:[1,3,6,5,4]

3.由于1,3已经固定,因此从后面三个数中选择一个最小的数:4,将它放在3的后面,结果:[1,3,4,6,5]

4.由于1,3,4已经固定,因此从后面二个数中选择一个最小的数:4,将它放在4的后面,结果:[1,3,4,5,6]

5.由于1,3,4,5已经固定,因此从后面一个数中选择一个最小的数:6,将它放在5的后面,结果:[1,3,4,5,6]

动画演示

选择排序实现

获取数组当中的最大值

#include <iostream>

using namespace std;

int main(){
    int a[7] = {20,10,45,35,15,13,36};
    int min = 1000;
    
    for (int i = 0;i < 7;i++){
        if (a[i] < min){
            min = a[i];
        }
    }

    cout << min << endl;
}

数值交换

#include <iostream>

using namespace std;

int main(){
    int a = 5;
    int b = 10;

    int temp = a;
    a = b;
    b = temp;

    cout << a << "," << b << endl;
}

第一轮交换

#include <iostream>

using namespace std;

int main(){
    int a[7] = {20,10,45,35,15,13,36};
    
    //确定要交换的位置
    int w = 0;

    //获取最小值
    int min = 0;
    for (int i = 1;i < 7;i++){
        if (a[i] < a[min]){
            min = i;
        }
    }

    //交换位置
    int temp = a[w];
    a[w] = a[min];
    a[min] = temp;

    //输出交换结果
    for (int i = 0;i < 7;i++){
        cout << a[i] <<" ";
    }
    

}

第二轮交换

    //第二轮交换
    //确定要交换的位置
    w = 1;

    //获取最小值
    min = 1;
    for (int i = 2;i < 7;i++){
        if (a[i] < a[min]){
            min = i;
        }
    }

    //交换位置
    temp = a[w];
    a[w] = a[min];
    a[min] = temp;

    //输出交换结果
    for (int i = 0;i < 7;i++){
        cout << a[i] <<" ";
    }

最终实现

#include <iostream>

using namespace std;

int main(){
    int a[7] = {20,10,45,35,15,13,36};
    int w,min,temp;

    for (int j = 0;j < 6;j++){
        //确定要交换的位置
        w = j;

        //获取最小值
        min = j;
        for (int i = j + 1;i < 7;i++){
            if (a[i] < a[min]){
                min = i;
            }
        }

        //交换位置
        temp = a[w];
        a[w] = a[min];
        a[min] = temp;

        //输出交换结果
        for (int i = 0;i < 7;i++){
            cout << a[i] <<" ";
        }
    
        cout << endl;
    }

}

如何按照从大到小的顺序排序

综合应用

将选择排序算法封装成函数,方便进行调用

//接受参数:数组a和数组大小
void selectSort(int a[],int size){
    int w,min,temp;

    for (int j = 0;j < size - 1;j++){
        //确定要交换的位置
        w = j;

        //获取最小值
        min = j;
        for (int i = j + 1;i < 7;i++){
            if (a[i] < a[min]){
                min = i;
            }
        }

        //交换位置
        temp = a[w];
        a[w] = a[min];
        a[min] = temp;
    }
}

Level3_3_选择排序

By yang he

Level3_3_选择排序

  • 225