Algorithm-sorting algorithm

table of Contents

1. Summary of common sorting algorithms

1. Selection sort

2. Bubble sort

3. Insertion sort

4. Hill sort

5. Merge and sort

6. Quick sort

7. Count and sort

8. Cardinality sort

2. Algorithm mind map [Comprehensive] Download


1. Summary of common sorting algorithms

1. Selection sort

package com.java.z_exam.algorithm.c02_sort;


public class T_0002_SelectionSort {
    public static void main(String[] args) {
        int[] arr = {5, 3, 6, 8, 1, 7, 9, 4, 2};
        //int[] arr = {5,8,5,2,9};
        selectSort(arr);

        print(arr);
    }

 
    public static void selectSort(int[] arr) {

        for (int time = 0; time < arr.length - 1; time++) {
            int min = time; // 取当前剩下数组第一个元素下标为最小下标
            for (int i = time + 1; i < arr.length; i++) {
                // 找到比当前元素值更小的下标,从当前数组序列头开始一直到结尾
                if (arr[i] < arr[min]) {
                    min = i;
                }
            }

            //System.out.println("minPos: " + minPos);
            // 找到后,将两个值进行交换
            swap(arr, min, time);

            //System.out.println("经过第" + i + "次循环之后,数组的内容:");
        }

    }

    private static void swap(int[] arr, int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    private static void print(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

2. Bubble sort

package com.java.z_exam.algorithm.c02_sort;


// 冒泡排序
public class T_0004_BubbleSort {
    public static void main(String[] args) {
        int[] a = {9, 3, 1, 4, 6, 8, 7, 5, 2};
        bubbleSort(a);
        print(a);
    }

    
    public static void bubbleSort(int[] arr) {
        for (int time = 0; time < arr.length; time++) {
            boolean isFinished = true;
            for (int i = 0; i < arr.length - 1 - time; i++) {
                if (arr[i] > arr[i + 1]) {
                    swap(arr, i, i + 1);
                    isFinished = false;
                }
            }
            if (isFinished) {
                break;
            }
            //print(arr);
            //System.out.println("\t\t第" + (time + 1) + "次");
        }
    }

    private static void swap(int[] arr, int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    private static void print(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

3. Insertion sort

package com.java.z_exam.algorithm.c02_sort;


public class T_0005_InsertionSort {
    public static void main(String[] args) {
        //int[] a ={9,3,1,4,6,8,7,5,2};
        int[] a = {9, 6, 1, 3, 5};
        insertionSort(a);
        print(a);
    }

    public static void insertionSort(int[] arr) {
        for (int time = 1; time < arr.length; time++) {
            for (int i = time; i > 0; i--) {
                if (arr[i] < arr[i - 1]) {
                    swap(arr, i, i - 1);
                } else {
                    break; // 优化点,即已经找到了当前数合适的位置,内循环不再继续
                }
            }

            //print(arr);
            //System.out.println("第" + (time + 1) + "次");
        }
    }


    private static void swap(int[] arr, int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    private static void print(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

4. Hill sort

package com.java.z_exam.algorithm.c02_sort;

import java.util.Arrays;
import java.util.Random;


public class T_0006_ShellSort {
    public static void main(String[] args) {
        int[] a = {9, 6, 11, 3, 5, 12, 8, 7, 10, 15, 14, 4, 1, 13, 2};
        shellSort(a);
        print(a);
    }

    public static void shellSort(int[] arr) {
        // Knuth序列
        // h = 1
        // h = 3*h + 1
        int h = 1;
        while (h <= arr.length / 3) {
            h = h*3 + 1;
        }

        // 先考虑间隔固定的情况
        int processTimes = 0;
        //int gap = 4;
        for (int gap = h; gap > 0 ; gap = (gap-1)/3) { // 数组长度除以2这种效率不是最好

            for (int time = gap; time < arr.length; time++) {
                for (int i = time; i >= gap; i-=gap) {
                    if (arr[i] < arr[i - gap]) {
                        swap(arr, i, i - gap);
                        processTimes++;
                    }
                }

                //print(arr);
                //System.out.println("第" + (time + 1) + "次");
            }
        }
        System.out.println("shellSort gap = Knuth序列: processTimes:" + processTimes);
    }

    private static void swap(int[] arr, int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    private static void print(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

5. Merge and sort

package com.java.z_exam.algorithm.c02_sort;

public class T_0007_MergeSort {
    public static void main(String[] args) {
        int[] arr = {5, 3, 6, 8, 1, 7, 9, 4, 2};

        mergeSort(arr, 0, arr.length - 1);

        print(arr);
    }

    // 递归写法
    public static void mergeSort(int[] arr, int left, int right) {
        if (left == right) return;

        // 分成两半
        int mid = left + (right - left) / 2; // (left + right) / 2 // right + left 可能会越界
        // 左边排序
        mergeSort(arr, left, mid);
        // 右边排序
        mergeSort(arr, mid + 1, right);

        // 最后将左右两边合并
        merge(arr, left, mid + 1, right);
    }

    private static void merge(int[] arr, int leftPtr, int rightPtr, int rightBound){
        int mid = rightPtr - 1;
        int[] temp = new int[rightBound - leftPtr + 1];

        int i = leftPtr;
        int j = rightPtr;
        int k = 0;
        while (i <= mid && j <= rightBound){
            try {
                temp[k++] = arr[i] <= arr[j] ? arr[i++] : arr[j++];
            } catch (ArrayIndexOutOfBoundsException ex) {
                ex.printStackTrace();
            }
        }

        // 如果是左半节数组有遗留,将左半节数组进行移动
        while(i <= mid) temp[k++] = arr[i++];
        // 如果是右半节数组有遗留,将左半节数组进行移动
        while(j <= rightBound) temp[k++] = arr[j++];

        // print(temp);
        // 最后将排好序的数组,赋值给回arr
        for (int m = 0; m < temp.length; m++) {
            arr[leftPtr + m] = temp[m];
        }
    }

    private static void print(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

6. Quick sort

package com.java.z_exam.algorithm.c02_sort;

public class T_0008_QuickSort {
    public static void main(String[] args) {
        //int[] arr = {7, 3, 2, 8, 1, 9, 5, 4, 6};
        //int[] arr = {7, 3, 2, 8, 1, 9, 5, 4, 6, 0}; // 越界
        //int[] arr = {7, 3, 2, 8, 1, 9, 5, 4, 6, 10}; // 越界
        int[] arr = {7, 3, 2, 6, 8, 1, 9, 5, 4, 6, 10, 6}; //
        //int[] arr = {7, 3, 2, 10, 8, 1, 9, 5, 4, 6}; //
        //int[] arr = {4, 6};

        quickSort(arr, 0, arr.length - 1);

        print(arr);
    }

    // leftBound 左边位置,rightBound 右边位置
    public static void quickSort(int[] arr, int leftBound, int rightBound) {
        if (leftBound >= rightBound) return;

        int mid = partition(arr, leftBound, rightBound);

        // 继续处理左边的序列
        quickSort(arr, leftBound, mid -1);
        // 继续处理右边的序列
        quickSort(arr, mid+1, rightBound);
    }

    // 分区
    static int partition(int[] arr, int leftBound, int rightBound) {
        int pivot = arr[rightBound]; // pivot 存放基准数
        int left = leftBound;
        int right = rightBound - 1;

        while (left <= right) {
            while (left <= right && arr[left] <= pivot) left++;
            while (left <= right && arr[right] > pivot) right--;

            // 当left、right没有相遇时,交换两个数在数组中的位置
            if (left < right) swap(arr, left, right);
        }

        // 将分界点移动到需要的位置上
        swap(arr, left, rightBound);

        return left;
    }

    private static void swap(int[] arr, int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    private static void print(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

7. Count and sort

package com.java.z_exam.algorithm.c02_sort;

public class T_0008_QuickSort {
    public static void main(String[] args) {
        //int[] arr = {7, 3, 2, 8, 1, 9, 5, 4, 6};
        //int[] arr = {7, 3, 2, 8, 1, 9, 5, 4, 6, 0}; // 越界
        //int[] arr = {7, 3, 2, 8, 1, 9, 5, 4, 6, 10}; // 越界
        int[] arr = {7, 3, 2, 6, 8, 1, 9, 5, 4, 6, 10, 6}; //
        //int[] arr = {7, 3, 2, 10, 8, 1, 9, 5, 4, 6}; //
        //int[] arr = {4, 6};

        quickSort(arr, 0, arr.length - 1);

        print(arr);
    }

    // leftBound 左边位置,rightBound 右边位置
    public static void quickSort(int[] arr, int leftBound, int rightBound) {
        if (leftBound >= rightBound) return;

        int mid = partition(arr, leftBound, rightBound);

        // 继续处理左边的序列
        quickSort(arr, leftBound, mid -1);
        // 继续处理右边的序列
        quickSort(arr, mid+1, rightBound);
    }

    // 分区
    static int partition(int[] arr, int leftBound, int rightBound) {
        int pivot = arr[rightBound]; // pivot 存放基准数
        int left = leftBound;
        int right = rightBound - 1;

        while (left <= right) {
            while (left <= right && arr[left] <= pivot) left++;
            while (left <= right && arr[right] > pivot) right--;

            // 当left、right没有相遇时,交换两个数在数组中的位置
            if (left < right) swap(arr, left, right);
        }

        // 将分界点移动到需要的位置上
        swap(arr, left, rightBound);

        return left;
    }

    private static void swap(int[] arr, int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    private static void print(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

8. Cardinality sort

package com.java.z_exam.algorithm.c02_sort;

import java.util.Arrays;

public class T_0010_RadixSort {
    public static void main(String[] args) {
        int[] arr = {421,240,115,532,305,430,124};

        int[] result = radixSort(arr);
        // 第一步,应该先求最高位数

        System.out.println(Arrays.toString(result));
    }
    
    public static int[] radixSort(int[] arr) {
        int[] result = new int[arr.length];

        int[] count = new int[10]; // 0~9 十个数

        for (int time = 0; time < 3; time++) {

            int pow = (int) Math.pow(10, time);

            for (int i = 0; i < arr.length; i++) {
                // 将个数取出来,对个数位进行桶计数
                int mod = arr[i] / pow % 10;
                count[mod]++;
            }

            // 累加数组,数组值对应排序后下标值+1
            for (int i = 1; i < count.length; i++) {
                count[i] = count[i] + count[i - 1];
            }

            // 遍历原数组421,240,115,532,305,430,124
            for (int i = arr.length - 1; i >= 0; i--) {
                // 将个数取出来,对个数位进行桶计数
                int mod = arr[i] / pow % 10;
                result[--count[mod]] = arr[i];
            }

            System.out.println("第" + time + " 次基数排序:" + Arrays.toString(count));


            // 将排序好的数组,还原回arr,继续下一位排序
            System.arraycopy(result, 0, arr, 0, arr.length);
            // 清空count计数数组
            Arrays.fill(count, 0);
            System.out.println("第" + time + " 次数组后排序:" + Arrays.toString(arr));
        }

        return result;
    }
}

2. Algorithm mind map [Comprehensive] Download

Download link: https://download.csdn.net/download/weixin_32265569/12821284

 


At the end of the article, I recommend some popular technical blog links :

  1. JAVA related deep technical blog link
  2. Flink related technical blog links
  3. Spark core technology link
  4. Design Pattern-Deepin Technology Blog Link
  5. Machine learning-deep technology blog link
  6. Hadoop related technical blog links
  7. Super dry goods-Flink mind map, it took about 3 weeks to compile and proofread
  8. Deepen the core principles of JAVA JVM to solve various online faults [with case]
  9. Please talk about your understanding of volatile? --A recent "hardcore contest" between Xiao Lizi and the interviewer
  10. Talk about RPC communication, an interview question that is often asked. Source code + notes, package understanding
  11. In-depth talk about Java garbage collection mechanism [with schematic diagram and tuning method]

Welcome to scan the QR code below or search the public account "Big Data Senior Architect", we will push more and timely information to you, welcome to communicate!

                                           

       

Guess you like

Origin blog.csdn.net/weixin_32265569/article/details/108458392