1. Bubble sort

Bubble sort: compare adjacent elements in pairs, and put the larger elements back. After the first attempt, the largest element appears at the largest index. Similarly, continue to get a sorted array. Rule:

  • 1. Compare them in pairs and put the bigger ones back.
  • 2. After the first comparison, the next one reduces the comparison by one element
  • 3. In the first comparison, there are 0 elements that are not.

    The second time, I have one element that is not.

    On the third comparison, there are two elements that are not.
  • 4. Compare the array length -1 times.

Code implementation

Public class Test1 {public static void main(String[] args) {// Define an array int[] array = {5, 6, 13, 2, 7, 23, 8, 11}; For (int I = 0; i < array.length; I++) {for (int j = 0; j < array.length - i - 1; If (array[j] > array[j +1]) {int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } System.out.println(Arrays.toString(array)); }}Copy the code

Running results:

[26, 47, 93, 90, 38, 16, 81, 63, 38] [16, 26, 38, 38, 47, 63, 80, 81, 90, 93]Copy the code

2. Select sort

Select sort: starting at index 0, the smallest element is compared to the next one, and after the first one, the smallest element appears at the smallest index. The other way around is you get a sorted array.

Rule:

  • 1: The first comparison is made from index 0 to the others and the second comparison is made from index 1 to the others

    And so on....
  • 2: The last comparison is between the number of elements of length -2 and the number of elements of length -1

Code implementation

Public class Test2 {public static void main(String[] args) {public static void main(String[] args) {int[] array = {5, 6, 13, 2, 7, 23, 8, 11}; for (int i = 0; i < array.length; i++) { for (int j = i + 1; j < array.length; J ++) {if (array[j] < array[I]) {int temp = array[j]; array[j] = array[i]; array[i] = temp; } } } System.out.println(Arrays.toString(array)); }}Copy the code

The running results are as follows:

[1, 3, 4, 5, 6, 7, 8, 9]
Copy the code

3. Insert sort

Insertion sort Insertion sort is similar to playing poker, starting with the second card, placing the smaller one in front of the larger one, and then placing it in order. Take a third card and find the right place to insert it so that the three cards are in order. In the fourth sheet until all insert order.

Code implementation

// The first for controls the number of inserts, and the second for loop is that after each insert, // finds the right place to insert the existing elements in order. public static int[] sort2(int[] ins){ for(int i=1; i<ins.length; i++){ int temp = ins[i]; // save the number that needs to be inserted each time; for(j=i; j>0&&ins[j-1]>temp; J --){// This has some optimization ins[j] = ins[J-1]; // Move the number greater than that needed to be inserted back. J} ins[j] = temp; } return ins; }Copy the code

I want to work my way up

Fly forward on the blades at the highest point

Let the wind blow dry tears and sweat

I want to work my way up

Waiting for the sun to watch its face

Little days have big dreams

I have my day

Let the wind blow dry tears and sweat

One day I will have my day