排序算法的Java完成全攻略。本站提示廣大學習愛好者:(排序算法的Java完成全攻略)文章只能為提供參考,不一定能成為您想要的結果。以下是排序算法的Java完成全攻略正文
Collections.sort()
Java的排序可以用Collections.sort() 排序函數完成。
用Collections.sort辦法對list排序有兩種辦法:
第一種是list中的對象完成Comparable接口,以下:
/** * 依據order對User排序 */ public class User implements Comparable<User>{ private String name; private Integer order; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getOrder() { return order; } public void setOrder(Integer order) { this.order = order; } public int compareTo(User arg0) { return this.getOrder().compareTo(arg0.getOrder()); } }
測試一下:
public class Test{ public static void main(String[] args) { User user1 = new User(); user1.setName("a"); user1.setOrder(1); User user2 = new User(); user2.setName("b"); user2.setOrder(2); List<User> list = new ArrayList<User>(); //此處add user2再add user1 list.add(user2); list.add(user1); Collections.sort(list); for(User u : list){ System.out.println(u.getName()); } } }
輸入成果以下
a b
第二種辦法是依據Collections.sort重載辦法來完成,例如:
/** * 依據order對User排序 */ public class User { //此處無需完成Comparable接口 private String name; private Integer order; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getOrder() { return order; } public void setOrder(Integer order) { this.order = order; } }
主類中如許寫便可:
public class Test{ public static void main(String[] args) { User user1 = new User(); user1.setName("a"); user1.setOrder(1); User user2 = new User(); user2.setName("b"); user2.setOrder(2); List<User> list = new ArrayList<User>(); list.add(user2); list.add(user1); Collections.sort(list,new Comparator<User>(){ public int compare(User arg0, User arg1) { return arg0.getOrder().compareTo(arg1.getOrder()); } }); for(User u : list){ System.out.println(u.getName()); } } }
輸入成果以下
a b
前者代碼構造簡略,然則只能依據固定的屬性排序,後者靈巧,可以暫時指定排序項,然則代碼不敷簡練
擇優用之。
經常使用排序算法
上面來看幾種經典排序算法的Java代碼理論:
冒泡排序
public static void bubbleSort(int A[], int n) { int i, j; for (i = 0; i < n - 1; i ++) { for (j = 0; j < n - i - 1; j ++) { if (A[j] > A[j + 1]) { A[j] = A[j] ^ A[j + 1]; A[j + 1] = A[j] ^ A[j + 1]; A[j] = A[j] ^ A[j + 1]; } } } }
直接拔出排序
public static void insertSort(int A[], int n) { int i, j, tmp; for (i = 1; i < n; i++) { tmp = A[i]; for (j = i - 1; j >= 0; j--) { if (A[j] > tmp) { A[j + 1] = A[j]; } else { break; } } A[j + 1] = tmp; } }
直接選擇排序
public static void selectSort(int A[], int n) { int i, j, loc; for (i = 0; i < n; i++) { loc = i; for (j = i + 1; j < n; j++) { if (A[j] < A[loc]) { loc = j; } } if (loc != i) { A[i] = A[i] ^ A[loc]; A[loc] = A[i] ^ A[loc]; A[i] = A[i] ^ A[loc]; } } }
堆排序
/** * 堆排序(從小到年夜) * * @param A * @param n */ public static void heapSort(int A[], int n) { int tmp; // 構建年夜根堆 buildMaxHeap(A, n); for (int j = n - 1; j >= 1; j--) { tmp = A[0]; A[0] = A[j]; A[j] = tmp; maxheapIfy(A, 0, j); } } /** * 構建年夜根堆 * * @param A * @param n */ private static void buildMaxHeap(int A[], int n) { for (int i = (n - 2) / 2; i >= 0; i--) { maxheapIfy(A, i, n); } } /** * 保護從下標i開端的最年夜堆 * * @param A * @param i * @param n */ private static void maxheapIfy(int A[], int i, int n) { int left, right, loc; while (i < n) { left = 2 * i + 1; right = 2 * i + 2; loc = i; if (left < n && A[left] > A[i]) { i = left; } if (right < n && A[right] > A[i]) { i = right; } if (loc != i) { A[i] = A[loc] ^ A[i]; A[loc] = A[loc] ^ A[i]; A[i] = A[loc] ^ A[i]; } else { break; } } }
疾速排序
public static void quickSort(int A[], int bt, int ed) { if (bt < ed) { int pivot = pivotPartition(A, bt, ed); quickSort(A, bt, pivot - 1); quickSort(A, pivot + 1, ed); } } private static void swapVar(int A[], int bt, int ed) { int mid = bt + (ed - bt) / 2; if (mid != bt) { A[bt] = A[bt] ^ A[mid]; A[mid] = A[bt] ^ A[mid]; A[bt] = A[bt] ^ A[mid]; } } private static int pivotPartition(int A[], int bt, int ed) { // 取中央值作為stand,避免數組有序湧現O(n^2)情形 swapVar(A, bt, ed); int stand = A[bt]; while (bt < ed) { while (bt < ed && A[ed] >= stand) { ed--; } if (bt < ed) { A[bt++] = A[ed]; } while (bt < ed && A[bt] <= stand) { bt++; } if (bt < ed) { A[ed--] = A[bt]; } } A[bt] = stand; return bt; }
合並排序
public static void mergeSort(int A[], int bt, int ed) { if (bt < ed) { int mid = bt + (ed - bt) / 2; mergeSort(A, bt, mid); mergeSort(A, mid + 1, ed); mergeArray(A, bt, mid, ed); } } private static void mergeArray(int A[], int bt, int mid, int ed) { int i, j, k, len = ed - bt + 1; int tmp[] = new int[len]; for (i = bt, j = mid + 1, k = 0; i <= mid && j <= ed; k++) { if (A[i] <= A[j]) { tmp[k] = A[i++]; } else { tmp[k] = A[j++]; } } while (i <= mid) { tmp[k++] = A[i++]; } while (j <= ed) { tmp[k++] = A[j++]; } for (i = 0; i < k; i++) { A[bt + i] = tmp[i]; } }
測試法式
來將以上算法歸結總結一下:
import java.util.Scanner; public class JavaSort { public static void main(String args[]) { Scanner cin = new Scanner(System.in); int A[], n; while (cin.hasNext()) { n = cin.nextInt(); A = new int[n]; for (int i = 0; i < n; i++) { A[i] = cin.nextInt(); } // bubbleSort(A, n); // insertSort(A, n); // selectSort(A, n); // heapSort(A, n); // quickSort(A, 0, n - 1); mergeSort(A, 0, n - 1); printArr(A); } } /** * 合並排序 * * @param A * @param bt * @param ed */ public static void mergeSort(int A[], int bt, int ed) { if (bt < ed) { int mid = bt + (ed - bt) / 2; mergeSort(A, bt, mid); mergeSort(A, mid + 1, ed); mergeArray(A, bt, mid, ed); } } /** * 歸並數組 * * @param A * @param bt * @param mid * @param ed */ private static void mergeArray(int A[], int bt, int mid, int ed) { int i, j, k, len = ed - bt + 1; int tmp[] = new int[len]; for (i = bt, j = mid + 1, k = 0; i <= mid && j <= ed; k++) { if (A[i] <= A[j]) { tmp[k] = A[i++]; } else { tmp[k] = A[j++]; } } while (i <= mid) { tmp[k++] = A[i++]; } while (j <= ed) { tmp[k++] = A[j++]; } for (i = 0; i < k; i++) { A[bt + i] = tmp[i]; } } /** * 疾速排序 * * @param A * @param bt * @param ed */ public static void quickSort(int A[], int bt, int ed) { if (bt < ed) { int pivot = pivotPartition(A, bt, ed); quickSort(A, bt, pivot - 1); quickSort(A, pivot + 1, ed); } } private static void swapVar(int A[], int bt, int ed) { int mid = bt + (ed - bt) / 2; if (mid != bt) { A[bt] = A[bt] ^ A[mid]; A[mid] = A[bt] ^ A[mid]; A[bt] = A[bt] ^ A[mid]; } } /** * 快排尋覓基准點地位 * * @param A * @param bt * @param ed * @return */ private static int pivotPartition(int A[], int bt, int ed) { // 取中央值作為stand,避免數組有序湧現O(n^2)情形 swapVar(A, bt, ed); int stand = A[bt]; while (bt < ed) { while (bt < ed && A[ed] >= stand) { ed--; } if (bt < ed) { A[bt++] = A[ed]; } while (bt < ed && A[bt] <= stand) { bt++; } if (bt < ed) { A[ed--] = A[bt]; } } A[bt] = stand; return bt; } /** * 堆排序(從小到年夜) * * @param A * @param n */ public static void heapSort(int A[], int n) { int tmp; // 構建年夜根堆 buildMaxHeap(A, n); for (int j = n - 1; j >= 1; j--) { tmp = A[0]; A[0] = A[j]; A[j] = tmp; maxheapIfy(A, 0, j); } } /** * 構建年夜根堆 * * @param A * @param n */ private static void buildMaxHeap(int A[], int n) { for (int i = (n - 2) / 2; i >= 0; i--) { maxheapIfy(A, i, n); } } /** * 保護從下標i開端的最年夜堆 * * @param A * @param i * @param n */ private static void maxheapIfy(int A[], int i, int n) { int left, right, loc; while (i < n) { left = 2 * i + 1; right = 2 * i + 2; loc = i; if (left < n && A[left] > A[i]) { i = left; } if (right < n && A[right] > A[i]) { i = right; } if (loc != i) { A[i] = A[loc] ^ A[i]; A[loc] = A[loc] ^ A[i]; A[i] = A[loc] ^ A[i]; } else { break; } } } /** * 直接選擇排序 * * @param A * @param n */ public static void selectSort(int A[], int n) { int i, j, loc; for (i = 0; i < n; i++) { loc = i; for (j = i + 1; j < n; j++) { if (A[j] < A[loc]) { loc = j; } } if (loc != i) { A[i] = A[i] ^ A[loc]; A[loc] = A[i] ^ A[loc]; A[i] = A[i] ^ A[loc]; } } } /** * 直接拔出排序 * * @param A * @param n */ public static void insertSort(int A[], int n) { int i, j, tmp; for (i = 1; i < n; i++) { tmp = A[i]; for (j = i - 1; j >= 0; j--) { if (A[j] > tmp) { A[j + 1] = A[j]; } else { break; } } A[j + 1] = tmp; } } /** * 冒泡排序 * * @param A * @param n */ public static void bubbleSort(int A[], int n) { int i, j; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (A[j] > A[j + 1]) { A[j] = A[j] ^ A[j + 1]; A[j + 1] = A[j] ^ A[j + 1]; A[j] = A[j] ^ A[j + 1]; } } } } /** * 打印數組 * * @param A */ public static void printArr(int A[]) { for (int i = 0; i < A.length; i++) { if (i == A.length - 1) { System.out.printf("%d\n", A[i]); } else { System.out.printf("%d ", A[i]); } } } }