(3)排序算法——冒泡排序

冒泡排序算法需要遍历几次数组。在每次遍历中,比较连续相邻的元素。如果某一对元素是降序,则互换它们的值;否则,保持不变。第一次遍历之后,最后一个元素成为数组中的最大数。第二次遍历之后,倒数第二个元素成为数组中的第二大数。整个过程持续到所有元素都已排好序为至。其时间复杂度为O(n*n).

public class BubbleSort {

    public static void main (String[] args) {
        double[] myList = {5.0, 4.4, 1.9, 2.9, 3.4, 2.9, 3.5};
        System.out.println("My list before sort is: ");
        //prints the original list
        printList(myList);
        //bubbleSort(myList);
        bubble_sort(myList);
        //prints the sorted list
        System.out.println("My list after sort is: ");
        printList(myList);
    }

    public static void printList(double[] list) {
        for (int i = 0; i < list.length; i++)
            System.out.println(list[i]+"\t");
        System.out.println();
    }

    public static void bubble_sort(double list[]){
        int i, j;
        double t;
        for(i=0; i<list.length-1; i++) // i 控制扫描次数 
            for(j=0; j<list.length-1-i; j++) // j 控制第i 次扫描时下标的变化 
                if(list[j]>list[j+1]){
                    t=list[j];  
                    list[j]=list[j+1];  
                    list[j+1]=t;
                }
    }      

    public  static void bubbleSort(double[] list) {
        boolean changed = true;
        do {
          changed = false;
          for (int j = 0; j < list.length - 1; j++)
            if (list[j] > list[j+1]) {
              //swap list[j] wiht list[j+1]
              double temp = list[j];
              list[j] = list[j + 1];
              list[j + 1] = temp;
              changed = true;
            }
        } while (changed);
    }

    public static void bubbleSort(int[] list) {
        boolean needNextPass = true;
        for (int k = 1; k < list.length && needNextPass; k++) {
          // Array may be sorted and next pass not needed
          needNextPass = false;
          for (int i = 0; i < list.length - k; i++) {
            if (list[i] > list[i + 1]) {
              // Swap list[i] with list[i + 1]
              int temp = list[i];
              list[i] = list[i + 1];
              list[i + 1] = temp;
              needNextPass = true; // Next pass still needed
            }
          }
        }
    }

    /** The method for sorting the numbers */
     public static <E extends Comparable<E>> void bubbleSort(E[] list) {
        boolean needNextPass = true;
        for (int k = 1; k < list.length && needNextPass; k++) {
            // Array may be sorted and next pass not needed
            needNextPass = false;
            for (int i = 0; i < list.length - k; i++) {
                if (list[i].compareTo(list[i + 1]) > 0) {
                    // Swap list[i] with list[i + 1]
                    E temp = list[i];
                    list[i] = list[i + 1];
                    list[i + 1] = temp;
                    needNextPass = true; // Next pass still needed
                }
            }
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值