3種順序排序方法。簡單排序是指時間複雜度爲O(n^2)的排序方法。

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章