Bubble sort, select sort and insert sort

Posted by wrapper on Thu, 10 Oct 2019 00:08:28 +0200

Bubble Sorting: Compare two adjacent elements. If the order between them does not meet the requirements, they exchange positions with each other. Each round of comparison can determine the correct position of the largest element in the element participating in the current round of comparison. Once a maximum value is determined, the element will no longer participate in the next round of comparison.

import java.util.Arrays;

public class Bubble_Sort { //Bubble sort

	public static void main(String[] args) {
		int[] arr={8,6,4,9,0,1,5,7,2,3};
		for (int i = 0; i < arr.length; i++) {	// Number of rounds for comparison of outer cycle control
		/*
		 * Two comparisons, so the number of comparisons in the first round is arr.length- 1
		 * In addition, each round will determine the location of a maximum number, so the number of participants in the first round is arr.length-1-i.
		 */
			//The inner loop compares the rounds and exchanges positions in a regular order
			for (int j = 0; j < arr.length- 1 - i; j++) {
				int nextIndex = j + 1;
				if (arr[j] > arr[nextIndex]) {
					// If the left side of the adjacent two numbers is larger than the right side, the position is exchanged, i.e. the maximum number is moved backwards.
					int temp = arr[nextIndex];
					arr[nextIndex] = arr[j];
					arr[j] = temp;
				}
			}
		System.out.println(Arrays.toString(arr));
		}
	}
}

Selective Sorting: Look at each number, compare and select the minimum value, swap to the front, once a minimum value is determined, the element will no longer participate in the next round of comparison.

import java.util.Arrays;

public class Selection_sort {//Selection sort

	public static void main(String[] args) {
		int[] arr={8,6,4,9,0,1,5,7,2,3};
		  for(int i=0;i<arr.length-1;i++){ //Number of Outer Cycle Control Wheels
		       int index=i; //Record the current location
	           for(int j=i+1;j<arr.length;j++){//Inner loop finds the current minimum
	                if(arr[j] < arr[index]){
			         index=j; //Record Minimum Location
	                }		            
		    }
	           //Switch the smallest number to the current front
	           int temp=arr[index];
	           arr[index]=arr[i];
	           arr[i]=temp; 
	           System.out.println(Arrays.toString(arr));	
	       } 
	}
}

Insertion sort: Look at the original arrangement as two parts, the first part as an ordered arrangement (there is only one element at first), the latter part as the original disordered arrangement, and then let the elements in the disordered arrangement be queued into the ordered arrangement according to the order. The insertion position needs to meet the requirements of the order, when all the people in the disordered arrangement are queued up to the ordered arrangement. After that, the final result is the sort we need.

 

import java.util.Arrays;

public class Insertion_sort { //Insertion sort

	public static void main(String[] args) {
		int[] arr={9,6,5,8,7,3,4,1,2,0};
		//Outer circulation controls the number of wheels inserted into the height
		//Consider the first number in the original sequence as an ordered sequence
	    for(int i=1;i<arr.length;i++){
	         int temp=arr[i];//The first number of an unordered sequence is the second number of the original sequence.
	         int leftindex=i-1;
		 while(leftindex >= 0 && arr[leftindex]>temp){
			//If the number of the preceding ordered sequence is large, the larger number in the preceding sequence will fall back and give way to the position.
			arr[leftindex + 1] = arr[leftindex];
			leftindex --; //Continuation of Numbers in an Unordered Sequence and Comparison with the First Number in an Ordered Sequence
		  }
		// After finding the appropriate location, insert it into the corresponding position of the ordered sequence
		 arr[leftindex+1] = temp;
		 System.out.println(Arrays.toString(arr));
	    }
	}
}

 

 

Topics: Java