冒泡排序

实现数组在排序方面的应用。有键盘输入一组整数,用冒泡法进行排序并输出。

import  java.io.*;

public class Array_Sort_Demo {
    public static void main(String[] args)throws IOException{
        BufferedReader buf;
        String str;
        int i,j,temp;
        int a[];
        int count=0;
        a=new int[8];
        System.out.println("input data:");
        for(i=0;i<a.length;i++){
            buf=new BufferedReader(new InputStreamReader(System.in));
            str=buf.readLine();
            a[i]=Integer.parseInt(str);
        }
        for(i=0;i<a.length;i++){
            if(i==0)
                System.out.print(a[i]);
            else
                System.out.print(","+a[i]);
        }
        System.out.println("");
        System.out.println("**********Sorted Course**********");
        for(i=1;i<a.length;i++){
            count++;
            for(j=0;j<a.length-i;j++)
                if(a[j]>a[j+1]){
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            System.out.print("第"+count+"趟:");
            for(j=0;j<a.length;j++){
                if(j==0)
                    System.out.print(a[j]);
                else
                    System.out.print("."+a[j]);
            }
            System.out.println("");
        }
        System.out.println("**********Sorted Result**********");
        for(j=0;j<a.length;j++){
            if(j==0)
                System.out.print(a[j]);
            else
                System.out.print("."+a[j]);
        }
        System.out.println("");
        System.out.println("");
    }

}

原文地址:https://www.cnblogs.com/ljs-666/p/7814430.html