標籤:遞迴調用 就會 bsp 迴圈條件 快速 [] ring lag java代碼
以下為集中排序的java代碼實現(部分是在引用別人代碼):插入排序(InsertSort):
//代碼原理public static void iSort(int[] a){ for(int i = 1;i < a.length; i++){ if(a[i] < a[i-1]){ int temp = a[i]; while(temp < a[i-1]){ a[i] = a[i-1]; if(i-1 > 0){ i--; }else{ break; } } a[i-1] = temp; } }}//精簡代碼1public static void iSort2(int[] a){ for(int i = 1;i < a.length; i++){ if(a[i] < a[i-1]){ int temp = a[i]; while(temp < a[i-1]){ a[i] = a[i-1]; if(i-1 > 0){ i--; }else{ break; } } a[i-1] = temp; } } }//精簡代碼2for(current=1;current<=arr.length-1;current++){ //每當current變化,key和before都隨之變化 key = arr[current]; before = current-1;//紅色有序索引第一個值。 while(before>=0 && arr[before]>key){ arr[before+1] = arr[before];//大值向後移一位 before--; } //插入點:before+1 arr[before+1] = key;}
冒泡排序(BubbletSort):
public class BubbleSort { public static int[] bSort(int[] a){ for(int j = a.length-1; j >= 1; j--){// 如果flag沒有變為false那麼證明數組本身有序 boolean flag = true; for(int i = 0; i <= j-1; i++){ if(a[i] > a[i+1]){ int temp = a[i]; a[i+1] = a[1]; a[i] = temp; flag = false; } } if(flag) break; } return a; }}
選擇排序(SelectionSort):
/* * 選擇排序基本思路: * 把第一個元素依次和後面的所有元素進行比較。 * 第一次結束後,就會有最小值出現在最前面。 * 依次類推 */public class SelectionSort { public static void sort(int[] data) { for (int x = 0; x < data.length - 1; x++) { for (int y = x + 1; y < data.length; y++) { if (data[y] < data[x]) { SortTest.swap(data, x, y); } } } }}
選擇排序(QuickSort):
/** * 快速排序 * @author mly11 * */public class QuickSort { static int low = 0; static int high = 0; static int mid = 0; public static void main(String[] args) { int[] arr = {5,1,3,9,2,7,2,4}; // 開始的數組為 System.out.println("開始的數組為:"); for(int i = 0; i < arr.length; i++){ System.out.print(arr[i]+"\t"); } System.out.println();// 排序 judge(arr); // 排序後遍曆 System.out.println("排序後數組為:"); for(int i = 0; i < arr.length; i++){ System.out.print(arr[i]+"\t"); } System.out.println(); } // 判斷數組是否為空白 public static void judge(int[] arr){ if(arr.length > 0){ all(arr,0,arr.length-1); }else{ System.out.println("換個吧");; } } // 迴圈條件,遞迴調用迴圈體 public static void all(int[] arr,int low,int high){ if(low < high){// 用mid記錄每一次選擇的分界數位角標, mid = structure(arr, low, high); // 當low < mid -1的時候,從low到mid-1進行遞迴 if(low < mid - 1){ all(arr, low, mid-1); } // 當high>mid+1時候,從mid+1到high遞迴 if(high > mid +1){ all(arr, mid+1, high); } } } // 迴圈體 一次迴圈 public static int structure(int[] arr,int low,int high){// 當索引low小於high時,進行運算,讓小於所選值在左邊,否則在右邊/* 原理: 取出a[low]的資料存入temp,使a[low]為空白;開始迴圈: 當low < high 時,一直迴圈 while(low < high){ 當low從第一個開始時,從後向前一一尋找,如果比temp大,則high--; 否則交換a[low]和a[high],此時a[low]的值為a[high],a[high]為空白; 現在從前(a[low]被a[high]佔據的位置)向後尋找,如果比temp小,則low++; 否則將a[low]的位置存入a[high](上一步為空白的a[high]),此時a[low]為空白; } a[low] = temp; 返回low,此時low的位置之前資料全部<a[low],low之後的位置的資料全部>a[low];*/ int temp = arr[low]; while (low < high) { while (low < high && arr[high] >= temp) { high--; } arr[low] = arr[high]; while (low < high && arr[low] < temp) { low++; } arr[high] = arr[low]; } arr[low] = temp; return low; /*錯誤的方法 while(low < high){ int temp = arr[low]; while(arr[low] <= arr[high]){ high--; } arr[low] = arr[high]; low++; arr[high] = arr[low]; arr[low] = temp; } return low;*/ } }
以上代碼為自己在最開始接觸的時候寫的,不足之處請諒解。
幾種排序方式的java實現(01:插入排序,冒泡排序,選擇排序,快速排序)