简单排序之java实现



3种顺序排序方法。简单排序是指时间复杂度为O(n^2)的排序方法。

1)选择排序

将某个特定值有序的放置在最终位置上---外层循环控制最终位置的序号,内层循环从外层循环序号处向后遍历,找到最小值。

2)插入排序

插入排序将某个特定值插入到值列的某个子集中,并重复该过程---外层循环控制要插入的数据(从第二个数到最后一个),内层循环控制前面已排好序的的值后移。

3)冒泡排序

重复比较相邻的两个元素,并在必要的时候交换双方位置。---外层循环控制每次冒泡到达的最终位置,内层循环交换元素

4)其他:这里写的是一个通用的排序算法,不光是对数值排序,可以对对象排序。对象实现Comparable接口,对象就可以参与比较进行排序。

在对对象进行排序的方法中,就可以调用对象的public int compareTo(Object other)方法来比较对象的先后顺序。


一个可以比较先后顺寻的Student类(按照score大小顺序):

class Student implements Comparable{

private String name;
private double score;

public Student(String name,double score)
{
this.name = name;
this.score = score;
}


public int compareTo(Object other) {
int result;
if(score > ((Student) other).score)
return 1;
else if(score < ((Student) other).score)
return -1;
return 0;
}


public String toString(){
String result
= "";
result
= name + " " + score;
return result;
}

}


三种排序方法:

1 package Sort;
2
3  public class SimpleSort {
4
5
6 public static void main(String[] args) {
7
8 Student[] student = new Student[7];
9
10 student[0] = new Student("aa",90.32);
11 student[1] = new Student("bb",70.32);
12 student[2] = new Student("cc",85.32);
13 student[3] = new Student("dd",64.32);
14 student[4] = new Student("ee",91.32);
15 student[5] = new Student("ff",74.32);
16 student[6] = new Student("gg",79.32);
17
18 //selectionSort(student);
19   insertSort(student);
20 //bubbleSort(student);
21  
22 for(int i = 0;i < student.length;i++)
23 System.out.println(student[i]);
24 }
25
26
27 //选择排序
28   public static void selectionSort(Comparable[] data){
29
30 int min;//用min标识最小元素的下标
31   Comparable temp;
32 for(int index = 0;index < data.length-1;index ++){//外层循环控制每个位置得到一个最终的值
33   min = index;
34 for(int scan = index + 1;scan < data.length;scan ++)//内层循环从外层循环位置处向后扫描
35 if(data[scan].compareTo(data[min]) < 0)
36 min = scan;//只记录位置!!!!
37 //内层for循环完表明一个位置的元素已经选好,交换
38 temp = data[min];
39 data[min] = data[index];
40 data[index] = temp;
41 }
42 }
43
44
45 //插入排序
46 public static void insertSort(Comparable[] data){
47
48 for(int index = 1;index <= data.length-1;index ++)//外层循环控制每个待插入元素
49 {
50 Comparable key = data[index];//待插入元素
51 int position = index;//用position记录插入后的应该占领位置(如果不发生改变,初试值是它自己的位置)
52
53 //只记录应该插入的位置,最后一步赋值。!!!
54 //while(data[position-1].compareTo(key) > 0 && position > 0)
55 while(position > 0 && data[position-1].compareTo(key) > 0)
56 {
57 data[position] = data[position-1];
58 position--;
59 }
60 data[position] = key;
61 }
62 }
63
64
65 //冒泡排序
66 public static void bubbleSort(Comparable[] data){
67
68 int position,scan;
69 Comparable temp;
70 for(position = data.length - 1;position >= 0;position--)//外层循环控制每趟冒泡到的最终位置
71 {
72 for(scan = 0;scan <= position -1;scan ++)//内层循环不断交换相邻两个元素
73 {
74 if(data[scan].compareTo(data[scan+1]) > 0)
75 {
76 temp = data[scan];
77 data[scan] = data[scan+1];
78 data[scan+1] = temp;
79 }
80 }
81 }
82 }
83
84 }


注意插入排序的内层循环开始写成了这样while(data[position-1].compareTo(key) > 0 && position > 0),运行起来会导致数组越界的异常。且表达式的判断要把最先决条件放在前面,这一点容易忽略。


运行结果,将数组重新排列了---

dd  64.32

bb  70.32

ff  74.32

gg  79.32

cc  85.32

aa  90.32

ee  91.32

posted @ 2011-04-25 13:06  jinmengzhe  阅读(1435)  评论(1编辑  收藏  举报