需进行n(n-1)/2次比较和记录移动,时间复杂度为O(n*n)


 

排序算法----冒泡排序java
import java.util.Arrays;
import java.util.Scanner;

/**
 * Created by Admin on 2017/3/26.
 */
public class test02 {
 public static void BubbleSort(int[] n){
       int temp;
       for(int i=0;i<n.length-1;i++)
           for(int j=i;j<n.length;j++){
           if(n[i]>n[j]){         //是对n[i]排序的,这里表示从小到大
               temp=n[i];
               n[i]=n[j];
               n[j]=temp;
           }
           }
    }

    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int num=1;
        while (num!=0) {
            num=scanner.nextInt();
            int[] n=new int[num];   // 这里new int要写初始化大小
            for(int i=0;i<num;i++)
            n[i] = scanner.nextInt();
            BubbleSort(n);
            System.out.println(Arrays.toString(n));
        }
    }
}
排序算法----冒泡排序java

排序算法----冒泡排序java

相关文章:

  • 2021-10-16
  • 2021-06-20
猜你喜欢
  • 2021-06-16
  • 2021-04-07
  • 2021-08-16
  • 2021-06-27
  • 2021-12-05
  • 2021-10-15
相关资源
相似解决方案