Select Sort-Bubble Sort-Merge Sort-Quick Sort-Insert Sort

Select sort

  • Basic idea:
    Let the number of sorted sequences be N, i take 1, 2, 3 ... n-1, find the record with the smallest sorting code from N-i + 1 records (Ri, Ri + 1 .... Rn) , Exchange with the i-th record, complete the sequence sorting after performing N-1 times.
//选择排序
private static void selectSort(int[] arr, int n) {
    for (int i = 0; i < n - 1; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (arr[i] > arr[j]) {
                int t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
            }
        }
    }
}

Bubble Sort

  • The basic idea:
    compare adjacent elements in pairs, and exchange them in reverse order. Every time, the largest or smallest element will be "floated up", and finally reach order.
//冒泡排序
private static void bubbleSort(in[] arr, into n) {
    for (int i = 0; i < n - 1; ++i) {
        for (int j = 0; j < n - 1 - i; ++j) {
            if (arr[j] > arr[j + 1]) {
                int t = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = t;
            }
        }
    }
}

Merge sort

  • Combine several ordered sequences in pairs until the records to be sorted are all in an ordered sequence
//归并排序
private static void mergeSort(int[] arr, int n) {
    mergeSort(arr, 0, n - 1);
}

private static void mergeSort(int[] arr, int low, int high) {
    if (low < high) {
        int mid = (low + high) / 2;
        // 左边
        mergeSort(arr, low, mid);
        // 右边
        mergeSort(arr, mid + 1, high);
        // 左右归并
        merge(arr, low, mid, high);
    }
}

private static void merge(int[] arr, int low, int mid, int high) {
    int[] temp = new int[high - low + 1];
    int i = low;// 左指针
    int j = mid + 1;// 右指针
    int k = 0;
    // 把较小的数先移到新数组中
    while (i <= mid && j <= high) {
        if (arr[i] < arr[j]) {
            temp[k++] = arr[i++];
        } else {
            temp[k++] = arr[j++];
        }
    }
    // 把左边剩余的数移入数组
    while (i <= mid) {
        temp[k++] = arr[i++];
    }
    // 把右边边剩余的数移入数组
    while (j <= high) {
        temp[k++] = arr[j++];
    }
    // 把新数组中的数覆盖arr数组
    for (int k2 = 0; k2 < temp.length; k2++) {
        arr[k2 + low] = temp[k2];
    }
}

Quick sort

  • Basic idea:
    Split the data to be sorted into two independent parts by one-pass sorting, and all the data in one part is smaller than all the data in the other part, and then quickly sort the two parts according to this method.
//快速排序
private static void quickSoft(int arr[], int length) {
    quickSoft(arr, 0, length - 1);
}

private static void quickSoft(int arr[], int low, int high) {
    int pos;
    if (low < high) {
        pos = partition(arr, low, high);
        quickSoft(arr, low, pos - 1);
        quickSoft(arr, pos + 1, high);
    }
}

private static int partition(int[] arr, int low, int high) {
    int val = arr[low];
    while (low < high) {
        while (low < high && arr[high] >= val) {
            --high;
        }
        arr[low] = arr[high];
        while (low < high && arr[low] <= val) {
            ++low;
        }
        arr[high] = arr[low];
    }
    arr[low] = val;
    return low;
}

Insert sort

  • Basic idea:
    Each time a record to be sorted is inserted into an ordered sequence according to the size of its key code, until all records are sorted.
 //插入排序
private static void insertSoft(int[] arr, int n) {
    for (int i = 1; i < n; i++) {
        int t = arr[i];
        int k = i;
        for (int j = i - 1; j >= 0 && arr[j] > t; --j) {
            arr[j + 1] = arr[j];
            k = j;
        }
        arr[k] = t;
    }
}
Published 395 original articles · won 130 · 200,000 views +

Guess you like

Origin blog.csdn.net/qq_40507857/article/details/86512570