Home  >  Article  >  Java  >  What loop controls the bubble sort algorithm?

What loop controls the bubble sort algorithm?

藏色散人
藏色散人forward
2019-04-10 16:51:413930browse

This article mainly introduces to you what loop is controlled by bubble sorting. Then the bubble sorting algorithm can be implemented by a double-layer for loop or a single for loop. Below we will introduce the implementation method of bubble sorting with specific code examples!

1. What is bubble sorting method?

Compare adjacent elements. If the first one is larger than the second one, swap their positions, and then continue to look down.

Two, two Steps of a bubble sorting method:

Sort the following arrays: (22,3,6,54,86,21,35,1,65,4)

1. Ordinary bubble sorting method:

Implementation steps:

1: Double-layer for loop nesting;

2. Determine if the conditions are met , exchange the positions of the two numbers;

public class BubbleSort {
    public static void main(String[] args) {
        
        int a[]={22,3,6,54,86,21,35,1,65,4};
        
        for(int i= 0 ;i<a.length;i++){    //第一层循环
            for(int j=0;j<a.length-i-1;j++){    //第二层循环
                if(a[j]>a[j+1]){  
                                  //交换位置
                    int tem =a[j];
                    a[j]=a[j+1];
                    a[j+1]=tem;
                }
            }
        }
        System.out.println("排好序:");
        for(int aa:a){
            System.out.print(aa+" ");
        }
    }
}

Result display:

What loop controls the bubble sort algorithm?

Double-layer for loop implements bubble sorting method

2. A single for loop implements bubble sorting method:

a.) Define the array to be sorted and the relevant length of the array

int a[]={22,3,6,54,86,21,35,1,65,4};
  int team=a.length-1;

b.) Sorting implementation:

1. A single for loop;

2. Determine whether the positions should be exchanged;

for(int i= 0 ;i<team;i++){
        if(a[i]>a[i+1]){
        int tem =a[i];
        a[i]=a[i+1];
        a[i+1]=tem;
        }
}

3. Determine whether the for loop ends;

4. If the for loop ends, -1 will be assigned to i, and the length of team will be reduced by 1, and the next loop will continue;

if(i==team-1){
            i=-1;
            team--;
}

c.) Complete program:

//单个for循环的冒泡排序法
public class BubbleSort {
    public static void main(String[] args) {
        
    int a[]={22,3,6,54,86,21,35,1,65,4};
    int team=a.length-1;
    for(int i= 0 ;i<team;i++){
        if(a[i]>a[i+1]){
        int tem =a[i];
        a[i]=a[i+1];
        a[i+1]=tem;
        }
        if(i==team-1){
            i=-1;
            team--;
        }
    }
    System.out.println("排好序:");
    for(int aa:a){
        System.out.print(aa+" ");
    }
    }
}

Result display:

What loop controls the bubble sort algorithm?

A single for loop implements bubble sorting

This article comes from the PHP Chinese website. For more relevant knowledge points, please go to the PHP Chinese website video course channel!

The above is the detailed content of What loop controls the bubble sort algorithm?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jianshu.com. If there is any infringement, please contact admin@php.cn delete