Sorting algorithm auxiliary tools

  1. Generates a random integer array
  2. Generates a nearly ordered array of integers
  3. Checks whether an array is ordered, returning true for ordered and false for unordered
  4. Copy an integer array
  5. Swap the positions of two indexes in an array
  6. Print the array
public class SortTestUtils {

    /** * randomly generates an integer array *@paramSize Array size *@paramRangeL Array left range (inclusive) *@paramRangeR array right range (inclusive) *@return* /
    public static int[] generateRandomArray(int size,int rangeL,int rangeR) {
            int[] arr = new int[size];
            for(int i = 0; i < size; i++) { arr[i] = (int) (Math.random() * (rangeR-rangeL+1)) + rangeL;
            }
            return arr;
    }

    /** * generates a nearly ordered integer array *@paramSize Array size *@paramSwapTimes scrambles the ordered array *@return* /
    public static int[] generateAlmostOrderlyArray(int size,int swapTimes) {
            int[] arr = new int[size];
            for(int i = 0; i<size; i++) { arr[i] = i; }for(int i = 0; i < swapTimes; i++) {int index1 = (int) Math.random()*size;
                    int index2 = (int) Math.random()*size;
                    swap(arr,index1,index2);
            }
            return arr;
    }

    /** * If an array is ordered, return true for ordered and false for unordered@paramArr integer array *@return* /
    public static boolean isOrderly(int[] arr) {
            for(int i=0; i < arr.length-1; i++) {if(arr[i] > arr[i+1]) {
                            return false; }}return true;
    }

    /** * copy an integer array *@paramArr integer array *@return* /
    public static int[] copyIntArray(int[] arr) {
            int[] copyArr = new int[arr.length];
            for(int i = 0; i < arr.length; i++) { copyArr[i] = arr[i]; }return copyArr;
    }

    /** * Swap the positions of the two indexes in the array *@paramArr integer array *@paramI The first index *@paramJ Second index */
    public static void swap(int[] arr, int i, int j){
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
    }

    /** * prints the array *@paramArr integer array */
    public static void printArray(int[] arr) {
            for (int i : arr) {
                    System.out.print(i+"\t"); } System.out.println(); }}Copy the code

Two, three basic sorting algorithms

  1. Bubble sort
  2. Selection sort
  3. Insertion sort
/** * bubble sort *@paramArr integer array */
public static void bubbleSort(int[] arr) {
        for(int i = 0; i < arr.length-1; i++) {for(int j = 0; j < arr.length-1; j++) {if(arr[j+1] < arr[j]) {
                                SortTestUtils.swap(arr, j+1, j); }}}}/** * Select sort * - Select a smallest number, insert leftmost *@paramArr integer array */
public static void selectSort(int[] arr) {
        for(int i = 0; i < arr.length-1; i++) {int min = i;
                for(int j = i+1; j < arr.length; j++) {if(arr[j] < arr[min]) { min = j; } } SortTestUtils.swap(arr, i, min); }}/** * insert sort * - Randomly select a number, insert into the appropriate position *@paramArr integer array */
public static void insertSort(int[] arr) {
        for(int i = 1; i < arr.length; i++) {
                int insertValue = arr[i];
                int j = 0;
                for(j = i; j >0 && arr[j-1] > insertValue; j--) {
                        arr[j] = arr[j-1]; } arr[j] = insertValue; }}Copy the code