java实现冒泡排序法

插入排序

public class InsertionSort {  
    public static void sort(int data[]) {  
        for (int i = 1; i < data.length; i++) {  
            for (int j = i; j > 0; j--) {  
                if (data[j] < data[j - 1]) {  
                    int temp = data[j];  
                    data[j] = data[j - 1];  
                    data[j - 1] = temp;  }  }   }   }  }  

抽象排序方法

 public static void bubbleSort(int[] arr) {
    int i, j, temp, len = arr.length;
    for (i = 0; i < len - 1; i++)
        for (j = 0; j < len - 1 - i; j++)
            if (arr[j] > arr[j + 1]) {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
    }

具体实现且行数最少

import java.util.Arrays;
public class BubbleSort {
    public static void BubbleSort_Method(int[] arr) {
        int temp;//定义一个临时变量
        for(int i=0;i<arr.length-1;i++){//冒泡趟数
            for(int j=0;j<arr.length-i-1;j++){
                if(arr[j]>arr[j+1]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;}}}}
    public static void main(String[] args) {
        int arr[] = new int[]{1,6,2,2,5,10,-1,20};
        BubbleSort.BubbleSort_Method(arr);
        System.out.println(Arrays.toString(arr));}}

时空复杂度

Paste_Image.png

方法一

主函数中通过BubbleSort类带参数的构造函数执行冒泡排序

import java.util.Arrays;
/**
 * 冒泡排序
 * @author zsy
 *
 */
public class BubbleSort {
    public BubbleSort(int[] arr) {
        int temp;//定义一个临时变量
        for(int i=0;i<arr.length-1;i++){//冒泡趟数
            for(int j=0;j<arr.length-i-1;j++){
                if(arr[j+1]<arr[j]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }
    public static void main(String[] args) {
        int arr[] = new int[]{1,6,2,2,5,10,-1,100};
        BubbleSort bubble = new BubbleSort(arr);
        //输出方法1
        for(int i = 0; i < arr.length; i++){
            System.out.print(arr[i] + " ");
            }   
        //输出方法2
        System.out.println(Arrays.toString(arr));
    }
}

方法二

主函数通过调用BubbleSort类的静态方法执行冒泡排序

import java.util.Arrays;
/**
 * 冒泡排序
 * @author zsy
 *
 */
public class BubbleSort {
    public static void BubbleSort_Method(int[] arr) {
        int temp;//定义一个临时变量
        for(int i=0;i<arr.length-1;i++){//冒泡趟数
            for(int j=0;j<arr.length-i-1;j++){
                if(arr[j+1]<arr[j]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }
    public static void main(String[] args) {
        int arr[] = new int[]{1,6,2,2,5,10,-1};
        BubbleSort.BubbleSort_Method(arr);
        //输出方法1
        for(int i = 0; i < arr.length; i++){
            System.out.print(arr[i] + " ");
            }   
        //输出方法2
        System.out.println(Arrays.toString(arr));
    }
}

运行结果


Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,742评论 18 399
  • 一:java概述:1,JDK:Java Development Kit,java的开发和运行环境,java的开发工...
    ZaneInTheSun阅读 2,686评论 0 11
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,202评论 30 471
  • 小编费力收集:给你想要的面试集合 1.C++或Java中的异常处理机制的简单原理和应用。 当JAVA程序违反了JA...
    八爷君阅读 4,650评论 1 114
  • 人生哪有那么多的好运气,让你在人群中熠熠生辉的,其实不过是你一直以来的那份执著认真和努力。 《我的前半生》里的唐晶...
    生活并舞蹈着阅读 1,864评论 0 5