Bubble, select, fast, insert four sorts

1. Code

package com.xtm.java.test;


/**
 * Author: TianMing.Xiong
 * Date: Created in 18-3-7 下午4:34
 */
public class SynchronzedTest {

    public static void main(String args[]) {
           int array[]={12, 10, 11, 7, 6, 5, 4, 3, 2, 1};
           printArray(array);
//           fastSortArray(array,0,array.length-1);
//           selectSort1(array);
//           bubbleSort(array);
           insertSort(array);
           System.out.println();
           printArray(array);
    }

    /**
     * Insertion sort
     * @param array
     */
    private static void insertSort(int[] array) {
//        for(int i=1;i<array.length;i++){
//            int mValue=array[i];
//            int j;
//            for(j=i;j>0 && mValue<array[j-1];j--){
//                array[j]= array[j-1];
//            }
//            array[j]=mValue;
//        }

        for(int i =1;i<array.length;i++){
            int temp = array[i];
            int j = i;
            while(j>0 && temp < array[j-1] ){
                array[j] = array[j-1];
                j--;
            }
            array[j] = temp;
        }

    }

    /**
     * Bubble Sort
     * @param array
     */
    private static void bubbleSort(int[] array) {
        for(int i=0;i<array.length-1;i++){
            for(int j=0;j<array.length-i-1;j++){
                if(array[j]>array[j+1]){
                    int tmp = array[j];
                    array[j]=array[j+1];
                    array[j+1]=tmp;
                }
            }
        }
    }

    /**
     * selection sort
     * @param array
     */
    private static void selectSort1(int[] array) {

        int len = array.length;
        for(int i=0;i<len-1;i++){

            //1. Assuming that the first element has the smallest value, take a cup min to hold the smallest value
            int min = array[i];

            //2. Compare the following elements with the smallest element one by one, and exchange if it is smaller than the value of the smallest element (ensure that the min value is the smallest and does not destroy the original array data)
            for (int j=i+1;j<len;j++){
                if(array[j]<min){
                    int tmp = min;
                    min=array[j];
                    array[j]=tmp;
                }
            }

            //3. Assign the smallest value selected in this round to the current element
            array[i] = min;

        }
    }

    /**
     * selection sort
     * @param array
     */
    private static void selectSort2(int[] array) {

        int len = array.length;
        for(int i=0;i<len-1;i++){

            //find the element subscript of the minimum value
            int minIndex = i;
            for (int j=i+1;j<len;j++){
                if(array[j]<array[minIndex]){
                   minIndex = j;
                }
            }
            // swap if it is not the current element
            if(minIndex!=i){
                int tmp=array[minIndex];
                array[minIndex]=array[i];
                array[i]=tmp;
            }

        }
    }

    /**
     * Quick sort
     * @param array
     * @param firstIndex
     * @param lastIndex
     */
    private static void fastSortArray(int[] array, int firstIndex, int lastIndex) {
        int i=firstIndex;
        int j=lastIndex;
        int temp = 0;
        if(i>j){
            return;
        }
        int base = array[firstIndex];
        while (i<j){
            //Find out the subscript less than the benchmark from the right
            while (base<=array[j] && i<j){
                j--;
            }
            //Find the subscript greater than the base from the left
            while (base>=array[i] && i<j){
                i++;
            }
            // swap these two values
            temp=array[i];
            array[i]=array[j];
            array[j]=temp;
        }
        // Swap the current value with the reference value when it collides
        int t=array[i];
        array[i]=base;
        array[firstIndex]=t;

        // continue sorting
        fastSortArray(array,0,i-1);
        fastSortArray(array,j+1,lastIndex);

    }

    /**
     * print array
     * @param array
     */
    private static void printArray(int[] array) {
      for (int i=0;i<array.length;i++){
          if(i!=array.length-1){
              System.out.print(array[i]+"--");
          }else {
             System.out.print(array[i]);
          }
      }
    }


}





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325894618&siteId=291194637