前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基础算法|4 简单选择排序

基础算法|4 简单选择排序

作者头像
ACM算法日常
发布2018-10-18 11:14:54
6330
发布2018-10-18 11:14:54
举报
文章被收录于专栏:ACM算法日常ACM算法日常

我们之前已经了解了三种基础算法,分别为二分查找算法,冒泡排序算法,以及直接插入排序算法。俗话说得好,温故而知新,所以现在就让我们简单回顾一下之前的三种算法吧。

二分查找算法——通过不断地二分搜索的区间,逐渐减小搜索范围,最终完成查找的目标。它是一种效率较高的查找算法,但是别忘了哟,使用它得有一个前提条件,那就是我们所要搜寻的数列是有序的。

冒泡排序算法——不断通过将小的数往上"冒",经过n-1(假设要排序的数有n个)次循环,最终形成了一个有序的数列。你是否还记得这种算法我们还可以逆向思维呢~那就是不断地将大的数往下"沉",从而达到排序的目的。这样我们在代码实现上就有两种方式了。

直接插入排序算法——就像打扑克牌一样,不断向一个已经排好序的数列中按顺序插入数据,最终当最后一个数插入完以后,得到的就是我们需要的有序数列了。

巩固了我们之前所学的东西,那我们就开始本篇文章的主题了——简单选择排序。


简单选择排序

简单选择排序,大家从这个名字就能体会出这个算法的思想,那就是不断通过选择来进行排序,那选择选择,到底选择的是什么呢~对了,数组的未排序的数中的最小值。那我们就来看看它算法思想吧。


简单选择排序算法思想

从要排序的数列中找出最小的数min,然后将其排到数组的最前面,即a[0]的位置(假设数组名为a,长度为n)。然后又在剩余的n-1个中找出最小值,将它排到a[1]的位置,如此经过n-1选择,排序最小值之后,我们就得到了一个有序数列。


代码实现

代码语言:javascript
复制
public static void easySelectSort(int[] a){
        for(int i=0;i<a.length;i++) {  //需进行n-1次排序
            int min =a[i];    //定义每次循坏中的最小值
            int k=i; //定义k跟踪最小值所在数组中的位置
            for(int j=i+1;j<a.length;j++){ //找出集合中剩余元素的最小值
                if(a[j]<min){
                    min = a[j];
                    k = j;
                }
            }
            int temp = a[i];  //保留a[i]的值
            a[i] = min; //将最小值插入到a[i]
            a[k] = temp;//将a[i]送回到剩余元素当中
        }
    }
</min){
</a.length;j++){ </a.length;i++) {  

那我们写个测试例子,来测试一下我们的算法吧。

代码语言:javascript
复制
public static void main(String[] args) {
        int[] a =new int[]{5,3,9,7,6,18,1,16,15};
        easySelectSort(a);
        for(int i=0;i<a.length;i++){
            System.out.print(a[i]+"  ");
        }

    }
</a.length;i++){

既然已经学习了简单选择排序算法,那我们还是老习惯,上题~。


HDU 1040 As easy as A+B

Problem Description These days, I am thinking about a question, how can I get a problem as easy as A+B? It is fairly difficulty to do such a thing. Of course, I got it after many waking nights. Give you some integers, your task is to sort these number ascending (升序). You should know how easy the problem is now! Good luck!

Input Input contains multiple test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test case contains an integer N (1<=N<=1000 the number of integers to be sorted) and then N integers follow in the same line. It is guarantied that all integers are in the range of 32-int.

Output For each case, print the sorting result, and one line one case.

Sample Input 2 ———— 测试用例的个数 3 2 1 3 ————第一个测试用例,第一个数表示数组的长度,后面的数表示元素值 9 1 4 7 2 5 8 3 6 9 ————第二个测试用例

Sample Output 1 2 3 1 2 3 4 5 6 7 8 9

分析:题意就是将一组进行排序(升序),感觉怎么样~是不是刚刚学习的东西又有用武之地的呢。

代码实现:

代码语言:javascript
复制
public static void main(String[] args) {
        System.out.println("请输入测试用例的个数:");
        Scanner input = new Scanner(System.in);
        int testCases = input.nextInt();  //定义变量testCases存储测试用例的个数
        for(int i =0;i<testcases;i++){   //输入testCases个测试用例
            System.out.println("请输入数组元素个数和对应的数组元素:");
            int count = input.nextInt();  //输入数组元素的个数
            int[] a = new int[count];
            for(int j =0;j<count;j++){  //输入数组的元素
                a[j] = input.nextInt();
            }
            easySelectSort(a);  //对数组进行简单选择排序
            for(int m=0;m<count;m++){
                System.out.print(a[m]+"  ");
            }
            System.out.println();
        }

    }

    public static void easySelectSort(int[] a){
        for(int i=0;i<a.length;i++) {  //需进行n-1次排序
            int min =a[i];    //定义每次循坏中的最小值
            int k=i; //定义k跟踪最小值所在数组中的位置
            for(int j=i+1;j<a.length;j++){ //找出集合中剩余元素的最小值
                if(a[j]<min){
                    min = a[j];
                    k = j;
                }
            }
            int temp = a[i];  //保留a[i]的值
            a[i] = min; //将最小值插入到a[i]
            a[k] = temp;//将a[i]送回到剩余元素当中
        }
    }
</min){
</a.length;j++){ </a.length;i++) {  </count;m++){
</count;j++){  </testcases;i++){   

让我们来运行一下吧

总述

本次我们学习了第四种基础算法——简单选择排序,你学会了吗ヾ(◍°∇°◍)ノ゙

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-10-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 ACM算法日常 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • HDU 1040 As easy as A+B
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档