The implementation of several common sorting algorithms in java

Posted by dr4296 on Wed, 04 Dec 2019 03:32:56 +0100

Bubble sort

Bubble sorting is a sort method that finds the most value every time. After finding the most value, put it on one end of the array through the comparison process. Next time you traverse, you will not see it. Just traverse the rest of the array, find the most value from the rest of the array, and put it on one end.

The code is as follows:

package study06_base;

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

        // Bubble sort
        // Determine the maximum value (minimum value), the secondary value, until the minimum value (maximum value) through traversal to see the required order

        // Sort from small to large
        for (int i = 1 ; i <= arr.length ; i++) {
            for (int j = 0 ; j < arr.length-i ; j++) {
                if (arr[j] > arr[j+1]){
                    int temp;
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }

        for (int index: arr) {
            System.out.print(index + " ");
        }

        System.out.println();

        // Sort from large to small
        for (int i = 1 ; i <= arr.length ; i++) {
            for (int j = 0 ; j < arr.length-i ; j++) {
                if (arr[j] < arr[j+1]){
                    int temp;
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }

        for (int index: arr) {
            System.out.print(index + " ");
        }
    }
}

Steps:

  1. Compare adjacent elements. If the first is bigger than the second, exchange them.
  2. Do the same for data 0 to n-1. At this point, the largest number "floats" to the last position of the array.
  3. Repeat the above steps for all elements except the last one.
  4. Continue to repeat the above steps every time for fewer and fewer elements, until no one pair of numbers needs to be compared.



SelectionSort

Selection sorting is the most intuitive sort. Just like we have A deck of playing cards now, we need to sort it. If we want to sort from small to large, then we will find all A's in it first, and the order of other cards will not be processed. Then we will put A on the top of all playing cards, and then find all 2's. repeat this process until the end of sorting.

The code is as follows:

	package study06_base;

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

        //Sort from small to large
        for (int i = 0 ; i < arr.length ; i++){
            int index = i;     //  The minimum element subscript, because each time only the minimum value is found from the array that has not been sorted, let index = i
            for (int j = i+1 ; j < arr.length ; j++){  // Traverse the array to find the minimum value
                if (arr[j] < arr[index]){
                    index = j;      // Mark the subscript of the smallest element found at this time
                }
            }
            // Place the minimum value found at the top of the unsorted array
            int temp;
            temp = arr[i];
            arr[i] = arr[index];
            arr[index] = temp;
        }


        for (int a:arr) {
            System.out.print(a+" ");
        }


        System.out.println();

        //Sort from large to small
        for (int i = 0 ; i < arr.length ; i++){
            int index = i;     //  The maximum element subscript, because only the maximum value is found from the array that has not been sorted each time, let index = i
            for (int j = i+1 ; j < arr.length ; j++){  // Traverse the array to find the maximum value
                if (arr[j] > arr[index]){
                    index = j;      // Mark the subscript of the largest element found at this time
                }
            }
            // Place the maximum value found at the top of the unsorted array
            int temp;
            temp = arr[i];
            arr[i] = arr[index];
            arr[index] = temp;
        }


        for (int a:arr) {
            System.out.print(a+" ");
        }
    }
}

Steps:

  1. Find the smallest (largest) element in the unsorted sequence and store it at the beginning of the sorted sequence.
  2. Then continue to find the smallest (largest) element from the remaining unsorted elements, and put it at the end of the sorted sequence.
  3. And so on until all elements are sorted.

Topics: REST