Bubble Sorting Quick Sorting Insert Sorting Selection Sorting-Simple Implementation in java Language

Posted by thelinx on Tue, 13 Aug 2019 13:01:47 +0200

Catalog

1. Bubble sort:

2. Quick sort:

3. Insertion sort:

4. Selection sort:

1. Bubble sort:

Bubble sorting repeatedly visits the column of elements to be sorted, comparing two adjacent elements in turn, and swapping them if their order (e.g. from large to small, and the initials from A to Z) is wrong. The work of visiting an element is repeated until no adjacent elements need to be exchanged, that is, the element column has been sorted.

The algorithm's name comes from the fact that larger elements float slowly to the top of the sequence (ascending or descending) by exchange, just as carbon dioxide bubbles in carbonated drinks eventually float to the top, so it's called "bubble sorting".

/**
 * Bubble sort:
 * 1.Compare adjacent elements. If the first one is bigger than the second one, exchange the two.
 * 2.Do the same work for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end, so that the last element should be the largest number.
 * 3.Repeat the above steps for all elements except the last one.
 * 4.Repeat steps 1 to 3 until the sorting is complete.
 */

public class BubbleSort {
    public static void outA(int [] a){
        for (int i : a) {
            System.out.print(" "+i);
        }
        System.out.println();
    }
    public static void main(String[] args) {
        int [] a = new int[]{2, 5, 6, 8, 9, 1, 3, 4, 7,0};
        int temp;
        for(int i = a.length-1;i >= 0;i--){
            for(int j = 0;j < i;j ++){
                if(a[j]>a[j+1]){    //Compare the size of two adjacent numbers
                    System.out.print("exchange"+a[j]+"and"+a[j+1]+":");
                    temp = a[j];    //exchange
                    a[j] = a[j+1];
                    a[j+1] = temp;
                    outA(a);
                }
            }
        }
        System.out.println("After the sorting is completed:");
        outA(a);
    }
}

2. Quick sort:

The basic idea of quick sorting is that the records to be sorted are separated into two separate parts by one-time sorting, in which the keywords of one part of the records are smaller than those of the other, so the two parts of the records can be sorted separately to achieve the order of the whole sequence.

/**
 * Quick sort:
 * Quick sort uses divide and conquer to divide a list into two sub-lists. The specific algorithm is described as follows:
 * Pick out an element from a sequence called pivot.
 * Reordering sequence, all elements smaller than the benchmark are placed in front of the benchmark, and all elements larger than the benchmark are placed behind the benchmark (the same number can reach either side). After the partition exits, the benchmark is in the middle of the sequence. This is called partition operation.
 * Recursively, the subordinate sequence of the elements less than the reference value and the subordinate sequence of the elements larger than the reference value are sorted.
 */

public class QuickSort {

    //Output array sequence
    public static void outA(int [] a){
        for (int i : a) {
            System.out.print(" "+i);
        }
        System.out.println();
    }

    //Exchange two values
    public void swap(int [] a,int i,int j){
        int temp;
        System.out.print(a[i]+"and"+a[j]+"Exchange:");
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
        outA(a);
    }

    //Partition operation
    public int partion(int [] a,int left,int right){
        int pivot = left;
        int index = left - 1;
        for(int i = left;i <= right;i ++){
            if(a[i] <= a[right]) {
                index ++;
                if(i>index)
                    swap(a, i,index);
            }
        }
        return index;
    }

    //Quick Sorting of Array Sequences
    public int[] sort(int[] a, int left, int right){
        int index;
        index = partion(a,left,right);
        if(index > left){
            sort(a,left,index - 1);
        }
        if(index < right){
            sort(a,index + 1,right);
        };
        return a;

    }

    public static void main(String[] args) {
        int [] a = new int[]{2, 1, 6, 5, 9, 4, 3, 8, 7,0};
        QuickSort quickSort = new QuickSort();
        quickSort.sort(a,0,a.length-1);
        System.out.print("After the sorting is completed:");
        outA(a);
    }
}

3. Insertion sort:

The algorithm description of Insertion-Sort is a simple and intuitive sorting algorithm. Its working principle is to construct an ordered sequence, scan backward and forward in the ordered sequence, find the corresponding position and insert it into the unordered data.

/**
 * Insert Sort:
 * Generally speaking, insertion sort is implemented in-place on arrays. The specific algorithm is described as follows:
 * Starting with the first element, the element can be considered sorted.
 * Remove the next element and scan backwards and forwards in the ordered sequence of elements.
 * If the element (sorted) is larger than the new element, move the element to the next location.
 * Repeat step 3 until you find that the sorted element is less than or equal to the new element.
 * After inserting the new element into the location;
 * Repeat steps 2 to 5.
 */

public class InsertSort {

    //Output array sequence
    public static void outA(int [] a){
        for (int i : a) {
            System.out.print(" "+i);
        }
        System.out.println();
    }

    //Insert elements
    public void insertEle(int [] a,int i,int j){
        int temp = a[j];
        System.out.print("Insertion in Ordered Sequence"+a[j]+":");
        for(int k = j;k > i; k --){
            a[k] = a[k-1];
        }
        a[i] = temp;
        outA(a);
    }

    //Insertion sort
    public void sort(int [] a){
        for(int i = 1;i < a.length;i ++){
            for(int j = 0;j < i;j ++){
                if(a[i]<a[j]){
                    insertEle(a,j,i);
                }
            }
        }
    }

    public static void main(String[] args) {
        int [] a = new int[]{2, 1, 6, 5, 9, 5, 3, 8, 7,0};
        InsertSort insertSort = new InsertSort();
        insertSort.sort(a);
        System.out.print("After the sorting is completed:");
        outA(a);
    }

}

4. Selection sort:

Selection-sort is a simple and intuitive sorting algorithm. Its working principle: first, find the smallest (big) element in the unordered sequence, store it at the beginning of the ordered sequence, then continue to find the smallest (big) element from the remaining unordered elements, and then put it at the end of the ordered sequence. By analogy, until all elements are sorted.  

/**
 * Selective Sorting
 * n Direct selection sorting of records can obtain ordered results through n-1 direct selection sorting. The specific algorithm is described as follows:
 * 1.Initial state: the disordered region is R[1.n], and the ordered region is empty.
 * 2.The order of the first trip (i=1,2,3... At the beginning of n-1, the current ordered region and disordered region are R [1.i-1] and R (i.n.) respectively. The row sorting selects the record R[k] with the smallest keyword from the current disordered region, and exchanges it with the first record R of the disordered region, so that R[1.i] and R[i+1.n) can be changed into a new ordered region and a new disordered region with an increase in the number of records and a decrease in the number of records, respectively.
 * 3.n-1 At the end of the trip, the array is ordered.
 */
public class SelectionSort {
    //Output array sequence
    public static void outA(int [] a){
        for (int i : a) {
            System.out.print(" "+i);
        }
        System.out.println();
    }

    //Exchange two values and output the exchange results for easy observation
    public void swap(int [] a,int i,int j){
        int temp;
        System.out.print(a[i]+"and"+a[j]+"Exchange:");
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
        outA(a);
    }

    //Select the smallest element and return the subscript
    public int selectionEle(int [] a,int i){
        int minIndex = i;
        for(int k = i;k < a.length;k ++){
            if(a[minIndex]>a[k]){
                minIndex = k;
            }
        }
//        System.out.println("min = "+a[minIndex]+" minIndex = "+minIndex);
        return minIndex;
    }

    //Selective Sorting
    public void sort(int [] a){
        int minIndex ;
        for(int i = 0; i < a.length; i ++){
            minIndex = selectionEle(a,i);
            swap(a,i,minIndex);
        }
    }

    public static void main(String[] args) {
        int [] a = new int[]{2, 1, 6, 5, 9, 3, 4, 8, 7,0};
        SelectionSort selectionSort = new SelectionSort();
        selectionSort.sort(a);
        System.out.print("After the sorting is completed:");
        outA(a);
    }
}

 

Topics: less