package com.luna.sort; public class JavaSort { public static void main(String[] args) { int a[] = { 10, 1, 8, 20, 9, 67, 73, 91 }; // bubbleSort(a); // // insertSort(a); // // selectSort(a); quickSort(a); System.out.println("Quick sort result: "); for (int k = 0; k < a.length; k++) { System.out.print(a[k] + " "); } System.out.println(); } /** * Insertion sort * @param a */ public static void insertSort(int[] a) { int i, j, insertNote;// data to be inserted for (i = 1; i < a.length; i++) {// loop from the second element of the array to insert the elements in the array insertNote = a[i];// Set the second element in the array as the data to be inserted in the first loop j = i - 1; while (j >= 0 && insertNote < a[j]) { a[j + 1] = a[j];// If the element to be inserted is less than the jth element, move the jth element backwards j--; } a[j + 1] = insertNote;// until the element to be inserted is not less than the jth element, insert insertNote into the array } System.out.println("Insertion sort result: "); for (int k = 0; k < a.length; k++) { System.out.print(a[k] + " "); } System.out.println(); } /** * The best time complexity of bubble sort is O(n), that is, the array itself is in positive order; the worst time complexity is O(n*n) * @param array */ public static void bubbleSort(int []array){ for (int i = 0; i < array.length - 1; i++){ for (int j = 0; j < array.length - 1 - i; j++){ if (array[j] > array[j + 1]) { int t = array[j]; array[j] = array[j + 1]; array[j + 1] = t; } } } System.out.println("Bubble sort result: "); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println(); } /** * Selection sort algorithm * @param arr */ public static void selectSort(int[] arr) { if (arr == null || arr.length < 2) { return; } int index;// define the subscript for (int i = 0; i < arr.length; i++) { index = i;// The initial subscript is i for (int j = i + 1; j < arr.length; j++) { if (arr[j] < arr[index]) { index = j; } } if (index != i) { int temp = arr[index]; arr[index] = arr[i]; arr[i] = temp; } } System.out.println("Selection sort result: "); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); } /** * Quick sort * @param a * @param low * @param hight */ public static void sort(int a[], int low, int hight) { int i, j, index; if (low > hight) { return; } i = low; j = hight; index = a[i]; // use the first record of the subtable as the benchmark while (i < j) { // alternately scan from both ends of the table to the middle while (i < j && a[j] >= index) j--; if (i < j) a[i++] = a[j];// replace lower record with record smaller than base while (i < j && a[i] < index) i++; if (i < j) // replace the high order record with a record larger than the base a[j--] = a[i]; } a[i] = index;// replace the base value back to a[i] sort(a, low, i - 1); // recursively sort the low sublist sort(a, i + 1, hight); // recursively sort the high sublist } public static void quickSort(int a[]) { sort(a, 0, a.length - 1); } }
Classic implementation of Java sorting algorithm (bubble, fast, select, insert)
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325722948&siteId=291194637
Recommended
Ranking