Exchange sort - Bubble Sort (Bubble Sorting)

Bubble sort

  1. Basic description: if you want a column to be sorted in ascending order, to treat the sort column values ​​from front to rear, sequentially comparing adjacent elements, if found in reverse order (after a former a large ratio) exchanged, so that the value of the larger element gradually moves from front to back, like a bubble under the water gradually as the risk
  2. Illustrates a bubble sort of
  • Diagram
  1. to sum up:
    • A total of n elements, large order row n - 1 times, each time ordering small number of large order in descending order
    • Optimization, when an order of a sort No change any element, then exit the loop, do not have to sort
    • Each time to determine the last element

Code

My code:

package bubble;

public class BubbleSort {
    public static void main(String[] args) {
        int arr[] = {3,9,-1,10,-2};
        //总共是五个元素,故而大排序是4次
        //大排序次数是数组长度-1次,
        int count = 0;
        //定义一个变量来计算排序次数
        for (int i = 0;i < arr.length - 1;i ++){
            //小排序随着大排序减少
            boolean isFlag = true;
            //每一次大排序之前isFlag都是真的,一旦经历了小排序那就是假的
            //每一次大排序完毕后都检测一下,有没有经历过小排序
            //如果没有那就直接退出,如果有那就再接着来
            for (int j = 0 ;j < arr.length - i - 1;j ++){
                //包含了一个零,所以这里请记住,特别容易错
                //逆序交换,有小到大排序,如果前一个比后一个大,那就交换排序顺序
                if(arr[j] > arr[j + 1]){
                    int temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                    count ++;
                    isFlag = false;
                }
            }
            if (isFlag){
                break;
            }
        }
        for (int i = 0;i < arr.length;i ++){
            System.out.println(arr[i]);
        }
        System.out.println(count);
    }
}

Course Code:

package bubble;

public class BubbleSort {
    public static void main(String[] args) {
        int arr[] = {2,3,-1,9,10};
        //总共是五个元素,故而大排序是4次
        //大排序次数是数组长度-1次,

    }
    public static void bubbleSort(int[] arr){
        int temp = 0;
        //临时变量,用来交换值
        boolean isFlag  = true;
        //isFlag是参数标量,
        for (int i = 0;i < arr.length - 1;i ++){
            //小排序随着大排序减少
            //每一次大排序之前isFlag都是真的,一旦经历了小排序那就是假的
            //每一次大排序完毕后都检测一下,有没有经历过小排序
            //如果没有那就直接退出,如果有那就再接着来
            for (int j = 0 ;j < arr.length - i - 1;j ++){
                //包含了一个零,所以这里请记住,特别容易错
                //逆序交换,有小到大排序,如果前一个比后一个大,那就交换排序顺序
                if(arr[j] > arr[j + 1]){
                    temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                    isFlag = false;
                }
            }

            if (isFlag){
                //如果再一次大排序中,一次小排序都没有发生过
                //isFlag就是真的不变
                break;
            }
            isFlag = true;
            //重置isFlag,进行下次判断,这是一定会执行的语句,没有必要加上else
          
        }
    }
}

Summary and Analysis:

  1. Bubble sort number sort large, array length minus one - outer loop arr.length - 1 times
  2. Small number of sorted array length minus 1 Save large order - the inner loop arr.length - 1 - i (i is a large order of the outer layer)
    large a total area reduction, a decrease greatly reduced total small
  3. Optimization techniques commonly used: the outer isFlag set to true, if the inner layer to be changed into false, adding the if statement determines whether or not the inner loop into the inner loop through isFlag The value each time it is determined to be reset End

Here Insert Picture Description
There is no need as I write so dispersed! !


Testing time sorting algorithm code

Code:

int[] arr = new int[80000];
        for(int i = 0;i < 80000;i ++){
            arr[i] = (int)(Math.random() * 8000000);
        }
        //随机产生八万个不同的数值的数组
        Date date1 = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
        String date1str = simpleDateFormat.format(date1);
        System.out.println("排序前的时间为" + date1str);
        bubbleSort(arr);
        Date date2 = new Date();
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
        String date2str = simpleDateFormat.format(date2);
        System.out.println("排序后的时间为" + date2str);

Summary analysis: almost forgotten, on common among the class of class time to review it.
Random number specific interval formula: RAND () * (BA) + A, whether in their closed interval plus a

Published 19 original articles · won praise 3 · Views 610

Guess you like

Origin blog.csdn.net/Blackoutdragon/article/details/103923560