Easy Tutorial
❮ Verilog Compile Instruction Programming Range ❯

Java Sorting Algorithm Analysis and Implementation

Category Programming Techniques

I. Overview:

This article provides the principles of several common sorting algorithms and their Java implementations, including both simple and advanced sorting algorithms, as well as other commonly used algorithmic knowledge.

II. Bubble Sort:

(1) Principle:

(2) Example:

Data to be sorted: 7, 6, 9, 8, 5, 1

First round of sorting process:

The pointer first points to 7, compares 7 with 6, 6<7, swaps the positions of 6 and 7, resulting in: 6,7,9,8,5,1
The pointer points to the second element 7, compares 7 with 9, 9>7, no need to swap, the result remains: 6,7,9,8,5,1
The pointer points to the third element 9, compares 9 with 8, 8<9, swaps the positions of 8 and 9, resulting in: 6,7,8,9,5,1
The pointer points to the fourth element 9, compares 9 with 5, 5<9, swaps 5 and 9, resulting in: 6,7,8,5,9,1
The pointer points to the fifth element 9, compares 9 with 1, 1<9, swaps 1 and 9, resulting in 6,7,8,5,1,9

After the first round of sorting, the largest number 9 is moved to the far right.

Proceed to the second round of sorting, the process is the same as above, but since the largest 9 is already on the far right, it does not need to be compared again, one less comparison, the result of the second round is: 6,7,5,1,8,9

Third round result: 6,5,1,7,8,9

Fourth round comparison result: 5,1,6,7,8,9

Fifth round comparison result: 1,5,6,7,8,9

The final sorted result is: 1,5,6,7,8,9. It can be seen that sorting N data requires N-1 rounds of sorting; the number of comparisons needed in the i-th round is N-i times.

(3) Coding approach:

Two nested loops are needed, where the outer loop i represents the number of sorting rounds, and the inner loop j represents the number of comparisons.

(4) Code implementation:

Example

``` package com.test.insertsort; /**

public ChooseSort(int[] array){
    this.array = array;
    this.length = array.length;
}

/**
 * Print all elements in the array
 */
public void display(){
    for(int i: array){
        System.out.print(i+" ");
    }
    System.out.println(); 
}

/**
 * Selection Sort algorithm
 */
public void chooseSort(){
    for(int i=0; i<length-1; i++){
        int minIndex = i;
        for(int j=minIndex+1;j<length;j++){
            if(array[j]<array[minIndex]){
                minIndex = j;
            }
        }
        int temp = array[i];
        array[i] = array[minIndex];
        array[minIndex] = temp; 
    }
}

public static void main(String[] args){
    int[] array={100,45,36,21,17,13,7};
    ChooseSort cs = new ChooseSort(array);
    System.out.println(

Comparison Times: In the first round of sorting, insertion sort compares at most once; in the second round of sorting, insertion sort compares at most twice; and so on, in the final round of sorting, it compares at most N-1 times, so the maximum number of comparisons for insertion sort is 1+2+...+N-1=N(N-1)/2. Despite this, in practice, insertion sort rarely compares this many times because once an element on the left that is smaller than the target element is found, the comparison stops, hence, the average number of comparisons for insertion sort is N(N-1)/4.

Number of Moves: The number of moves for insertion sort is almost the same as the number of comparisons, but the speed of moving is much faster than that of swapping.

In summary, the speed of insertion sort is about twice as fast as bubble sort (half the number of comparisons), and it is even faster than selection sort. For nearly ordered data, the speed of insertion sort is very fast, making it the most efficient sorting algorithm among simple sorts.


Quick Sort, Bubble Sort, Selection Sort, Insertion Sort, Merge Sort

I. Overview:

The above text introduces common simple algorithms: bubble sort, selection sort, and insertion sort. This article introduces advanced sorting algorithms: quick sort and merge sort. Before starting to introduce the algorithms, it first introduces the basic knowledge needed for advanced algorithms: partitioning, recursion, and also briefly introduces the binary search algorithm.

II. Partitioning:

Partitioning is the premise of quick sort, which is to divide the data into two groups, with data greater than a specific value in one group and data less than a specific value in another group. Quick sort is completed by partitioning and recursive operations.

(1) Principle:

Define a threshold, and traverse the elements from the far left and right towards the middle. Stop when you find a data on the left that is greater than the threshold, and stop when you find a data on the right that is less than the threshold. If at this time, neither the left nor the right has reached the middle, then swap the data on the left that is greater than the threshold with the data on the right that is less than the threshold; repeat the above process until the left pointer and the right pointer meet. At this point, all data on the left are less than the threshold, and all data on the right are greater than the threshold, and the partitioning is over. After partitioning, the data is still disordered, but closer to being ordered.

(2) Example:

Data to be partitioned: 7, 6, 9, 8, 5, 1, assume the threshold is 5

First round: The left pointer points to 7, and the right pointer points to 1. The left pointer moves back, and the right pointer moves left, finding the first element greater than 5 on the left, which is 7, and the first element less than 5 on the right, which is 1. Swap the positions of 7 and 1, result: 1, 6, 9, 8, 5, 7;

Second round: Start from 6 to find a number greater than 5, find 6, from the right side start from 5 to find a number less than 5, find 1, but at this point, since 6 is on the right side of 1, that is, the right pointer < left pointer, the left and right pointers cross, and the partitioning is over. The original number series is divided into two parts, the left sub-list has only one element, which is 1, and it is the sub-list less than the threshold; the right sub-list includes 5 elements, all of which are greater than the threshold of 5.

(3) Code Implementation:

Example

```java package com.test.insertsort;

/**

/** Array to be sorted and partitioned */
private int[] array;
/** Length of the array */
private int length;

public QuickSort(int[] array){
    this.array = array;
    this.length = array.length;
}

/**
 * Print elements
 */
public void printArray(){
    for(int i=0; i&lt;length; i++){
        System.out.print(array[i]+" ");
    }
    System.out.println();
}


/**
 * Partitioning
 * @return The partitioning pivot point
 */
public int partition(int left, int right, int pivot){
    // Starting point of the left pointer, left-1 is because in the following loop, the left pointer will move right each time,
    // This ensures that the
❮ Verilog Compile Instruction Programming Range ❯