您的位置:首页 > 编程语言 > Java开发

选择排序--JAVA实现

2011-06-07 16:43 176 查看
[align=center]选择排序--JAVA实现[/align]
思想:
假设有N条数据,则暂且标记下标为0的数据为min(即最小值),使用out标记最左边未排序的数据,然后使用in标记下标为1的数据,依次与min作比较,如果比min小,则将该数据标记为min(最小值),当第一轮比较完后,最终的min与out标记数据交换,依次类推:
源码如下所示:

package org.iljava.datastructures;

/**
*
* @author Xiaolong_Long
*
*/
public class SelectSort {
public static int[] a = { 23,0,367,1,43,6,25,24433 }; // 预设数据数组

public static void main(String args[]) {
System.out.println("未执行选择排序操作前的结果依次输出为:");
for (int i :a) {
System.out.print(i+"\t");
}
int in, out, min;
for (out = 0; out < a.length - 1; out++) {
min = out;
for (in = out + 1; in < a.length; in++) {
//更新最小值
if (a[in] < a[min])
min = in;
}
//数据交换
int temp = a[out];
a[out]=a[min];
a[min]=temp;
}
System.out.println();
System.out.println("执行选择排序操作后的结果依次输出为:");
for (int i :a) {
System.out.print(i+"\t");
}
}


}
[color=red]运行结果如下所示:
未执行选择排序操作前的结果依次输出为:
23 0 367 1 43 6 25 24433
执行选择排序操作后的结果依次输出为:
0 1 6 23 25 43 367 24433[/color]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐