Java bubbling and selection sorting method (you can learn with hands)

Commonly used sorting methods in Java sorting

I believe that when many friends just started to learn the Java language, they all felt that the sorts were too unfriendly to our friends. Look at these methods, you can definitely rub them on the ground. , I will talk about some of the more common methods for everyone below. The first method is to learn conversation as long as you have hands.
The first one: directly call the java built-in method: Arrays.sort (array that needs to be sorted); sort in ascending order

importjava.util.Arrays;
public class Px1 {
    
    
   public static void main(String[] args) {
    
    
     int []arr= {
    
    25,37,29,65};
//   注意:一定要导入这个才行:import java.util.Arrays;
    	 Arrays.sort(arr);//调用方法便可以实现从小到大进行排序
	System.out.println(Arrays.toString(arr));
   }
}
//运行结果:[25,29, 37, 65]

The second method: the selection and sorting method: the
realization principle: is to use a double-layer loop to select a number from arr[0] and compare it with other numbers. After the round, there is a maximum or minimum value, and then this Place the position in the first position of the array subscript, and then continue to select a number from arr[1] for the second time to compare with the remaining numbers until the sorting is completed.

public class Px2 {
    
    
 public static void main(String[] args) {
    
    
//      创建一个数组
        int[] arr = new int[] {
    
     12, 15, 26, 36, 10, 56, 26 };
        Sort(arr); //调用排序这个方法
        //输出排序完之后的结果
            System.out.print("排序后的结果:"+Arrays.toString(arr));
 }
    public static void Sort(int[] arr) {
    
    
    int temp,n=0;//定义一个变量用于交换两个数的值,当然也可以不用定义这个变量,用其他的交换方式也可以
      for (int i = 0; i < arr.length; i++) {
    
    
        for (int j= i+1; j < arr.length-1; j++) {
    
    
             if(arr[i]<arr[j]) //改变这里的大于小于符号可以改变数组的从大到小,或从小到大的排序方式
           {
    
    
              temp=arr[i];
              arr[i]=arr[j];
              arr[j]=temp;
           }
        }     
     }
   }
    /*
   第一次:将arr[0]这个值,也就是12,和 12, 15, 26, 36, 10, 56, 26这串数字做比较,得出的排序结果为:[56, 12, 15, 26, 10, 36, 26]

  第二次:将arr[1]这个值,也就是15,和 12, 15, 26, 36, 10, 56, 26这串数字做比较,得出的排序结果为:[56, 36, 12, 15, 10, 26, 26]

  第三次:将arr[2]这个值,也就是26,和 12, 15, 26, 36, 10, 56, 26这串数字做比较,得出的排序结果为:[56, 36, 26, 12, 10, 15, 26]

  第四次:将arr[3]这个值,也就是36,和 12, 15, 26, 36, 10, 56, 26这串数字做比较,得出的排序结果为:[56, 36, 26, 26, 10, 12, 15]

  第五次:将arr[4]这个值,也就是10,和 12, 15, 26, 36, 10, 56, 26这串数字做比较,得出的排序结果为:[56, 36, 26, 26, 15, 10, 12]

  第六次:将arr[5]这个值,也就是56,和 12, 15, 26, 36, 10, 56, 26这串数字做比较,得出的排序结果为:[56, 36, 26, 26, 15, 12, 10]

 第七次:将arr[6]这个值,也就是26,和 12, 15, 26, 36, 10, 56, 26这串数字做比较,得出的排序结果为:[[56, 36, 26, 26, 15, 12, 10]

 这里经过七次的比较得出了最后的排序结果:[56, 36, 26, 15, 12, 10, 26],相信大家也看出来了,这样的排序
 会有多余的计算,会降低计算机的计算效率,但是为了大家能更清楚的明白选择排序法的原理,我就先这样做了,
 大家明白了这个原理,大家可以对这个排序进行改进, 去掉多余的计算次数,提高代码的运行效率。

   */
}

Effect picture: The
Insert picture description here
third type: bubble sorting
principle: the so-called bubble sorting method is to compare two adjacent numbers. If whoever is larger or smaller, exchange positions and compare with the next adjacent element. The first pass After comparing, put the maximum or minimum value at the end, and then in the second pass, until the comparison is completed, the sorting is completed.

Here is the first trip: I won’t write about the rest, and everyone will push it slowly so that everyone can understand.

Need to sort data from small to large: 15,12,25,13

First time: 12,15 ,25,13

Second time: 12, 15,25 ,13

Third time: 12,15, 13,25

After three adjacent comparisons, the largest value is placed in the last position until the data is sorted

public class Px3 {
    
    
    public static void main(String[] args) {
    
    
//       创建一个数组
          
int[] arr = new int[] {
    
     12, 15, 26, 36, 10, 56, 26 };
        Sort(arr); //调用排序这个方法         
//输出排序完之后的结果
              System.out.print("排序后的结果:"+Arrays.toString(arr));
    }
       public static void Sort(int[] arr) {
    
    

       int temp,n=0;//定义一个变量用于交换两个数的值,当然也可以不用定义这个变量,用其他的交换方式也可以

         for (int i = 0; i < arr.length; i++) {
    
    
          for (int j= 0; j < arr.length-1; j++) {
    
    
               if(arr[j]<arr[j+1]) //改变这里的大于小于符号可以改变他的从大到小,或从小到大
             {
    
    
                temp=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=temp;           
	}         
} 
         System.out.println("第"+i+"次排序结果:"+Arrays.toString(arr));
        }
      }
}

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46188681/article/details/106015402