前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >选择排序算法(Selection Sort)

选择排序算法(Selection Sort)

作者头像
itlemon
发布2020-04-03 18:00:06
5440
发布2020-04-03 18:00:06
举报
文章被收录于专栏:深入理解Java深入理解Java

【选择排序算法基本思想和案例】

选择排序:

         每一趟从待排序的数据元素中选出最小(或者最大)的一个元素,顺序放在已经排好序的数列的后面,直到全部待排序的数据元素排完。

 案例:

         初始数组资源【63    4    24    1    3    15】

         第一趟排序后【15    4    24    1    3】 63

         第二趟排序后【15    4     3    1】 24   63

         第三趟排序后【  1    4     3】15   24   63

         第四趟排序后【  1    3】 4    15   24   63

         第五趟排序后【  1】 3     4   15   24   63

算法主要代码:

代码语言:javascript
复制
 // 定义方法实现选择排序
 public static void selectionSort(int[] array) {
 int count = 1;
 while (count >= 1) {
 int index = 0;
 int max = array[0];
 for (int i = 1; i <= array.length - count; i++) {
 if (max < array[i]) {
					max = array[i];
					index = i;
 }
 }
 int temp = array[array.length - count];
			array[array.length - count] = max;
			array[index] = temp;
			count++;
 if (count == array.length - 1) {
 break;
 }
 }
 }
}
案例:

public class SelectSort {
 public static void main(String[] args) {
 int[] array = {63, 4, 24, 1, 3, 15};
 System.out.println("排序前:");
 for (int i : array) {
 System.out.print(i + "\t");
 }
 System.out.println();
		selectionSort(array);
 System.out.println("排序后:");
 for (int i : array) {
 System.out.print(i + "\t");
 }
 }
 
 // 定义方法实现选择排序
 public static void selectionSort(int[] array) {
 int count = 1;
 while (count >= 1) {
 int index = 0;
 int max = array[0];
 for (int i = 1; i <= array.length - count; i++) {
 if (max < array[i]) {
					max = array[i];
					index = i;
 }
 }
 int temp = array[array.length - count];
			array[array.length - count] = max;
			array[index] = temp;
			count++;
 if (count == array.length - 1) {
 break;
 }
 }
 }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-07-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档