package com.avaj.sort;

import java.util.Arrays;

/**
 * Created by rookie on 2017/1/16.
 */

public class JavaSort {

    private static final float BASE = 1000000.0f;
    private static int[] array = {19, 89, 588, 193, 1, 11, 543, 111, 7, 10};


    /**
     * 选择排序
     */
    private static void selectSort() {
        long startTime = System.nanoTime();    //获取开始时间
        int temp;
        for (int i = 0; i < array.length - 1; i++) {
            int k = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[k]) {
                    k = j;
                }
            }
            if (k != i) {
                temp = array[i];
                array[i] = array[k];
                array[k] = temp;
            }
        }

        long endTime = System.nanoTime();    //获取结束时间
        System.out.println("程序运行时间：" + (endTime - startTime) / BASE + "ms");    //输出程序运行时间
    }


    /**
     * 快速直接插入排序
     */
    private static void QuickInsertSort() {
        long startTime = System.nanoTime();    //获取开始时间
        int temp;
        for (int i = 1; i < array.length; i++) {
            int j = i - 1;
            temp = array[i];
            for (; j >= 0 && temp < array[j]; j--) {
                array[j + 1] = array[j];
            }
            array[j + 1] = temp;

            System.out.print("when i=" + i + " array is ");
            System.out.println(Arrays.toString(array));
        }
        long endTime = System.nanoTime();    //获取结束时间
        System.out.println("程序运行时间：" + (endTime - startTime) / BASE + "ms");    //输出程序运行时间
    }


    private static void BubbleSort() {
        long startTime = System.nanoTime();    //获取开始时间
        for (int i = array.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
                System.out.format("when i=%d ,j=%d     ", i, j);
                System.out.println(Arrays.toString(array));
            }

        }
        long endTime = System.nanoTime();    //获取结束时间
        System.out.println("程序运行时间：" + (endTime - startTime) / BASE + "ms");    //输出程序运行时间
    }

    private static void BubbleSort2() {
        long startTime = System.nanoTime();    //获取开始时间
        int temp;
        int flag = array.length - 1;
        while (flag > 0) {
            int n = flag - 1;
            flag = 0;
            for (int i = 0; i <= n; i++) {
                if (array[i] > array[i + 1]) {
                    temp = array[i];
                    array[i] = array[i + 1];
                    array[i + 1] = temp;
                    flag = i;
                }
                System.out.format("when i=%d ,flag=%d     ", i, flag);
                System.out.println(Arrays.toString(array));
            }
        }


        long endTime = System.nanoTime();    //获取结束时间
        System.out.println("程序运行时间：" + (endTime - startTime) / BASE + "ms");    //输出程序运行时间
    }


    private static int BinarySearch(int[] array, int element) {
        int pos = -1;
        int left, mid, right;
        left = 0;
        right = array.length - 1;
        while (left <= right) {
            mid = (left + right) / 2;

            if (element < array[mid]) {
                right = mid - 1;
            } else if (element > array[mid]) {
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return pos;
    }


    public static void main(String[] args) {


//        String[] strs = {"1111", "b", "c"};
//        List<String> lists = Arrays.asList(strs);
//        System.out.println("1111's length is " + strs[0].length());
//        System.out.println("b's length is " + strs[1].length());
//        System.out.println("array length is " + strs.length);
//        System.out.println("list size is " + lists.size());
//
//
//        System.out.println("array length is " + array.length);
//        List<Integer> mList = IntStream.of(array).boxed().collect(Collectors.toList());
//
//        System.out.println("list size is " + mList.size());

        System.out.println("The Original data is :");
        System.out.println(Arrays.toString(array));
//        QuickInsertSort();
        BubbleSort2();
//        selectSort();
        System.out.print("\nAfter Sort data is : ");
        System.out.println(Arrays.toString(array));

        int pos = BinarySearch(array, 89);

        if (pos != -1) {
            System.out.println("\nSearch result position=" + pos);
        } else {
            System.err.println("\nerror !");
        }


    }
}

@@ -17,16 +17,17 @@
 .sort;%0A%0A
+%0A
 import j
@@ -163,16 +163,17 @@
 000.0f;%0A
+%0A
     priv
@@ -187,64 +187,78 @@
 tic 
-int%5B%5D array = %7B19, 89, 588, 193, 1, 11, 543, 111, 7, 10%7D
+final int SIZE = 100 ;%0A%0A    private static int%5B%5D array = new int%5BSIZE%5D
 ;%0A%0A%0A
@@ -1337,16 +1337,18 @@
  temp;%0A%0A
+//
         
@@ -1395,24 +1395,26 @@
 rray is %22);%0A
+//
             
@@ -4228,24 +4228,131 @@
 t.size());%0A%0A
+%0A        for (int i = 0; i %3C SIZE; i++) %7B%0A            array%5Bi%5D = (int) (Math.random() * SIZE);%0A        %7D%0A%0A%0A
         Syst
@@ -4441,26 +4441,24 @@
 ng(array));%0A
-//
         Quic
@@ -4464,32 +4464,34 @@
 ckInsertSort();%0A
+//
         BubbleSo

update origin data