Algorithm | Bubble Sort

As mentioned earlier,  selection sort  is based on element comparison. Now this is an algorithm based on adjacent element comparison.

Algorithm Description:

Use a sorting strategy from small to large.

Starting from the last element, in the direction from back to front, the adjacent elements are compared. If the latter element is found to be smaller than the previous element, it is exchanged until the end of the array, so that the smallest element can be found and placed. in the first position.

Then repeat the above strategy again, find the smallest element among the remaining elements and put it in the second position.

until the end of the cycle


The algorithm is implemented as:

public class BubbleSort {
    public static void main(String[] args) {
        int[] nums = new int[]{1, 15, 3, 78, 34, 23, 46, 2, 8, 34, 57};
        System.out.println("before insert sort "+ Arrays.toString(nums));
        sort(nums);
        System.out.println("after insert sort "+Arrays.toString(nums));


    }

    public static void sort(int[] arrays) {
        int length = arrays.length;
        for(int i=0; i< length; i++) {

            for(int j = length -1; j>0; j--) {
                if (arrays[j] < arrays[j - 1]) {
                    int temp = arrays[j];
                    arrays[j] = arrays[j-1];
                    arrays[j-1] = temp;
                }

            }

        }

    }
}


This process is like the floating of bubbles in water. Each time the outer loop ends, a smallest element (the smallest element in the remaining elements) floats up, so that until the end of the entire array, all elements become ordered.

There is a small optimization point here, that is, the number of loops in the inner loop does not need n times, because each time the float will make the first i elements of the array become ordered

optimization

 
 
public static void sort(int[] arrays) {
        int length = arrays.length;
        for(int i=0; i< length; i++) {

            for(int j = length -1; j>i; j--) {
                if (arrays[j] < arrays[j - 1]) {
                    int temp = arrays[j];
                    arrays[j] = arrays[j-1];
                    arrays[j-1] = temp;
                }

            }

        }

    }

The optimization point is the number of cycles of the inner loop, which is reduced from n times to ni times, and the condition is changed from j>0 to j>i

There is one point that needs to be explained here. We will write it like this in the insertion sort:

 
 
for(int j=i ; j> 0 && arrays[j] < arrays[j-1] ; j--) {

                int temp = arrays[j];
                arrays[j] = arrays[j - 1];
                arrays[j-1] = temp;
            }


We put the comparison condition in the body of the for() loop. In bubble sort, can we write it like this? For example:

for (int j = length - 1; j > i && arrays[j] < arrays[j - 1]; j--) {
                int temp = arrays[j];
                arrays[j] = arrays[j - 1];
                arrays[j - 1] = temp;

            }

The answer is definitely not, if you write like this, you must not get the correct sorting result,

For insertion sort, his condition is to find a suitable position in the sorted array to insert, and the process of comparison occurs between adjacent elements, as long as the element to be inserted is larger than the one in the sorted array. If the element is large, the loop is terminated immediately, and then inserted at the specified position.

But for bubble sort and selection sort, it must continue to compare until the end of the array, because it is compared in the unsorted array, it must traverse all the unsorted array elements.

Next: Insertion Sort

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325811784&siteId=291194637