默認為遞增順序;注:一下例子希望自己再次復習時,可以用筆在紙上畫畫內存圖。
包括有:
- 選擇排序
- 冒泡排序
- 插入排序
1.選擇排序
<--------------------------------------選擇排序--------------------------------------->
1、選擇排序(1):
選擇排序的思想是,每一次從待排序的數據元素中選出最小(或最大)的一個元素,存放在序列的起始位置,直到全部待排序的數據元素排完。
選擇排序1:
private static void selectionSort(int[] a){for(int i = 0;i<a.length;i++){for(int j = i+1;j<a.length;j++){if( a[j] <a[i]){int temp = a[i];a[i] = a[j];a[j] = temp;}}}
}
選擇排序2(最優):
private static void OptimizeNumSort(int[] a){int k,temp;// k and temp is not need to allocation internal storage everytime.for(int i=0;i<a.length;i++){k = i; //use k record minimum number at present ,hypothesis i is minimum./用k記錄那個最小的數for(int j = k+1;j<a.length;j++){if(a[j]<a[k]){k = j;}}if(i != k ){temp = a[i];a[i] = a[k];a[k] = temp;}}
}
?
python實現:
def selectionSort(arr):for i in range(len(arr) - 1):# 記錄最小數的索引minIndex = ifor j in range(i + 1, len(arr)):if arr[j] < arr[minIndex]:minIndex = j# i 不是最小數時,將 i 和最小數進行交換if i != minIndex:arr[i], arr[minIndex] = arr[minIndex], arr[i]return arr
?
<---------------------------------------冒泡排序-------------------------------------------------->
2、冒泡排序:
?基本思想 :
設排序表長為n,從后向前或者從前向后兩兩比較相鄰元素的值,如果兩者的相對次序不對(A[i-1] > A[i]),則交換它們,其結果是將最小的元素交換到待排序序列的第一個位置,我們稱它為一趟冒泡。下一趟冒泡時,前一趟確定的最小元素不再參與比較,待排序序列減少一個元素,每趟冒泡的結果把序列中最小的元素放到了序列的”最前面”。
比較相鄰的元素。如果第一個比第二個大,就交換他們兩個。
對每一對相鄰元素作同樣的工作,從開始第一對到結尾的最后一對。在這一點,最后的元素應該會是最大的數。
針對所有的元素重復以上的步驟,除了最后一個。
持續每次對越來越少的元素重復上面的步驟,直到沒有任何一對數字需要比較。?
冒泡排序是一種簡單的排序算法。它重復地走訪過要排序的數列,一次比較兩個元素,如果它們的順序錯誤就把它們交換過來。走訪數列的工作是重復地進行直到沒有再需要交換,也就是說該數列已經排序完成。這個算法的名字由來是因為越小的元素會經由交換慢慢“浮”到數列的頂端。?
?
算法描述
- 比較相鄰的元素。如果第一個比第二個大,就交換它們兩個;
- 對每一對相鄰元素作同樣的工作,從開始第一對到結尾的最后一對,這樣在最后的元素應該會是最大的數;
- 針對所有的元素重復以上的步驟,除了最后一個;
- 重復步驟1~3,直到排序完成。
?
private static void bubbleSort(int[] a){int len = a.length;int temp;for(int i = len-1; i>=1;i--){for(int j = 0;j<len-1;j++){if(a[j]>a[j+1]){temp = a[j];a[j] = a[j+1];a[j+1] = temp;}}}
}
?
來個小練習:對某個類的對象進行排序:
?
? ? 對日期(yeda-month-day)進行排序:
? ? 使用到條件運算符(?:)
public int compare(Date date){return (year>date.year)?1:year<date.year?-1:month>date.month?1:month<date.month?-1:day>date.day?1:day<date.day?-1:0;
}
詳細如下:
?
public class Test{public static void main(String[] args) {Date[] days = new Date[5]; days[0] = new Date(2006,5,4);days[1] = new Date(2006,7,4);days[2] = new Date(2008,5,4);days[3] = new Date(2004,5,9);days[4] = new Date(2004,5,4);for(int i=0;i<days.length;i++ ) {System.out.println(days[i]); }System.out.println("\nUp is not sort---------------------------Down after sort:\n");//bubbleSort(days);// call the mothod of bubbleSort().selectionSort(days);// call the mothod of selectionSort().for(int i=0;i<days.length;i++ ) {System.out.println(days[i]); }}/*Bubble sort and return sort a type;learn to bubbleSort' thinking;every sort will get the maximum.*/public static Date[] bubbleSort(Date[] a){int len = a.length;for(int i=len-1;i>=1;i--){for (int j = 0;j<=i-1 ;j++ ) {if(a[j].compare(a[j+1]) > 0){Date temp = a[j];a[j] = a[j+1];a[j+1] = temp;} } }return a;}/*selection sort */public static Date[] selectionSort(Date[] a){int len = a.length;int k;Date temp;for(int i =0 ; i<len;i++){k = i;for(int j = k+1;j<len;j++){if(a[k].compare(a[j]) == 1){k = j;}}if(k != i){temp = a[k];a[k] = a[i];a[i] = temp;}}return a;}}class Date{int year,month,day;Date(int y,int m,int d){year = y;month = m;day = d;}/*method compare() mainly to compare two Date objects' size.if a>b return 1;else if a<b return -1;else a = b return 0;according to return value ,whether to exchange objects'quote*/public int compare(Date date){return (year>date.year)?1:year<date.year?-1:month>date.month?1:month<date.month?-1:day>date.day?1:day<date.day?-1:0;}/*remember: when print an objects'quote ,is equal to call toString() method.so have to overwrite the toString*another:you can query the API/*/public String toString(){return "Year:Month:Day-- " + year + "-" + month + "-" + day;}
}
?
?
<--------------------------------插入排序-------------------------------->
3、插入排序(Insertion Sort)
插入排序(Insertion-Sort)的算法描述是一種簡單直觀的排序算法。它的工作原理是通過構建有序序列,對于未排序數據,在已排序序列中從后向前掃描,找到相應位置并插入。
3.1 算法描述
一般來說,插入排序都采用in-place在數組上實現。具體算法描述如下:
- 1、從第一個元素開始,該元素可以認為已經被排序;
- 2、取出下一個元素,在已經排序的元素序列中從后向前掃描;
- 3、如果該元素(已排序)大于新元素,將該元素移到下一位置;
- 4、重復步驟3,直到找到已排序的元素小于或者等于新元素的位置;
- 5、將新元素插入到該位置后;
- 6、重復步驟2~5。
private static void insertionSort(int[] a){System.out.println("Start....");int preIndex,current;for(int i = 1 ;i <a.length;i++){preIndex = i - 1;current = a[i];while(preIndex >= 0 && a[preIndex] >current){a[preIndex + 1] = a[preIndex];preIndex --;}a[preIndex + 1] = current;}
}
?
參考:
(1)http://www.cnblogs.com/wuxinyan/p/8615127.html(python 十大經典排序算法)
?