基础算法系列之排序算法-4. 简单选择排序 并解决hdu 1040 As easy as A+B (java实现)

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

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

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

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

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


 

简单选择排序

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


 

简单选择排序算法思想

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


 

代码实现

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]送回到剩余元素当中
		}
	}

 

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

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]+"  ");
		}

	}

 

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


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

 

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

 

代码实现:

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]送回到剩余元素当中
		}
	}

 

让我们来运行一下吧

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值