Explanation of java Bubble Sequence and Selective Sequence

  • 2021-10-16 01:32:41
  • OfStack

Catalog 1, Bubble Sort 2, Selective Sort Summary

1. Bubble sorting

The basic idea of bubble sorting (Bubble Sorting) is: by treating

Sort sequence from front to back (starting from the element with smaller subscript), compare the values of adjacent elements in turn, and exchange if reverse order is found, so that the elements with larger values gradually move from front to back, just like bubbles 1 under water gradually rising upward.

Because in the process of sorting, each element keeps approaching its own position, if it is compared once,
If there is no exchange in the past, it means that the sequence is orderly.

The Process of Graphical Bubble Sorting Algorithm

Original array: 3, 9,-1, 10, 20

The first sorting

(1) 3, 9,-1, 10, 20//If adjacent elements are in reverse order, exchange

(2) 3, -1, 9, 10, 20

(3) 3, -1, 9, 10, 20

(4) 3, -1, 9, 10, 20

The second sorting

(1)-1, 3, 9, 10, 20//Exchange

(2) -1, 3, 9, 10, 20

(3) -1, 3, 9, 10, 20

The third sorting

(1) -1, 3, 9, 10, 20

(2) -1, 3, 9, 10, 20

The fourth sorting

(1) -1, 3, 9, 10, 20

Summary bubble sorting rule

(1) A total of-1 loop of the size of the array

(2) The number of sequences per trip is gradually decreasing

(3) If we find that there is no exchange in a certain sort, we can end the bubble sort ahead of time. This is optimization


import java.util.Arrays;
public class BubbleSort {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
        int arr[]= {3,9,-1,10,-2};
        // No. 1 i+1 Trip sorting, putting the largest number at the end 
        int temp=0;// Temporary variable 
        for(int i=0;i<arr.length-1;i++) {// Define which round of sorting 
       	 for(int j=0;j<arr.length-1-i;j++) {
       		 if(arr[j+1]<arr[j]) {
       		  temp=arr[j];
       		 arr[j]=arr[j+1];
       		 arr[j+1]=temp;
       		 }
       		 }
        System.out.println(" Output number "+(i+1)+" Result of trip sorting ");
        System.out.println(Arrays.toString(arr));
        }
     
        }
	}

Run results:

Output the result of the first sort
[3, -1, 9, -2, 10]
Output the result of the second sort
[-1, 3, -2, 9, 10]
Output the result of the third sort
[-1, -2, 3, 9, 10]
Output the result of the fourth sort
[-2, -1, 3, 9, 10]

2. Selective sorting method

Sorting ideas:

Original array: 101, 34, 119, 1

Ranking in the first round: 1, 34, 119, 101

Ranking in the second round: 1, 34, 119, 101

Ranking in the third round: 1, 34, 101, 119

Description:

1. Select Sort 1 Common Array Size-1 Round Sort

2. Every round of sorting is another cycle, and the rule of cycle (code)

2.1 Assume that the current number is the minimum 2.2 Then compare with each subsequent number, and if a smaller number is found than the current number, re-determine the minimum number and get the subscript 2.3 When you traverse to the end of the array, you get the minimum number and subscript of this round 2.4 Exchange [Continue in Code]

import java.util.Arrays;
public class QuickSort {
    public static void main(String[] args) {
       //int []arr={ 8,3,2,1,7,4,6,5};
       int [] arr={101,34,109,1};
       quicksort(arr);
    }
    public static void quicksort(int []arr){
        for(int j=0;j<arr.length-1;j++) {
            int minindex=j;// Assume that the current subscript is the minimum subscript 
            int minnumber=arr[j];// Assume that the current element is the minimum 
            for (int i = 1+j; i < arr.length; i++) {
                if (arr[i] < minnumber) {// If it is assumed that the minimum value is not the minimum, 
                    minnumber = arr[i];// Reset minnumber
                    minindex = i;// Reset minindex
                }
            }
            // Swap the minimum value 
            arr[minindex] = arr[j];
            arr[j] = minnumber;
            System.out.println(" No. 1 "+(j+1)+" Wheel ");
            System.out.println(Arrays.toString(arr));
        }
    }
}

Summarize

That's all for this article. I hope I can bring you some help, and I hope you can pay more attention to more content of this site!


Related articles: