`
hunter090730
  • 浏览: 190883 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

java 选择排序算法之一

    博客分类:
  • java
阅读更多
//选择排序(Selection Sort)的基本思想是:每一趟从待排序的记录中选出关键字最小的记录,
//顺序放在已排好序的子文件的最后,直到全部记录排序完毕。
//注意起始指针i,扫描指针j,记录本趟扫描最小记录指针k,尔后R[i]与R[k]两两交换,并继续下一轮相当的动作直至完全正确的排序为止
//常用的选择排序方法有直接选择排序和堆排序。
//如4,89,3,55,66,7,2,44,1
public class StraightSelectionSort {
public static void main(String[] args) {
int[] n = { 594, 89, 3, 55, 86, 127, 2, 44, 1 };
StraightSelectionSort.straightSelectionSort(n);
for (int i = 0; i < n.length; i++) {
System.out.print(n[i] + ",");
}

}

public static void straightSelectionSort(int[] n) {
for (int i = 0; i < n.length; i++) {
int k = i;//用k为当前最小记录的下标
int min = n[i];
for (int j = i + 1; j < n.length; j++) {
if (n[j] < min) {
min = n[j];
k = j;//找出第i趟最小的并记下其下标
}
}
int temp = n[i];
n[i] = min;
n[k] = temp;//让下标为i,k的记录互换以实现记录最小的放至前面已排好序组的后面
}
}
}
0
0
分享到:
评论
2 楼 hunter090730 2009-08-17  
其实在Java中用那个comparable接口也可以实现,它亦有许多优点
1 楼 luzl 2009-08-13  
在Java中应该用那个comparable接口!

相关推荐

Global site tag (gtag.js) - Google Analytics