Several simple sorting methods in java

Introduce three Java sorting methods

  1. Bubble Sort
package cxcf;

import java.util.Arrays;

/*
 * 冒泡排序:
 * 时间复杂度:O(n^2)
 * 稳定:有没有跳跃式的比较  稳定的排序
 * 
 * 对冒泡排序进行优化
 * 
 * */
public class TestDemo7 {
    public static void bubbleSort(int[] array){
        int tmp=0;
        for(int i=0;i<array.length-1;i++){//趟数
            for(int j=0;j<array.length-1-i;j++){
                if(array[j]>array[j+1]){
                    tmp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=tmp;
                }
            }
        }
    }
    public static void main(String[] args){
        int[] array=new int[]{16,35,29,7,82,99};
        bubbleSort(array);
    System.out.println(Arrays.toString(array));
    }
}

Bubble optimization: reduce runtime

package cxcf;

import java.util.Arrays;

public class YouHuaMaoPao {

    public static void bubbleSort1(int[] array){

        int tmp=0;
        for(int i=0;i<array.length-1;i++){
            for(int j=0;j<array.length-1-i;j++){
                boolean swap=false;
                if(array[j]>array[j+1]){
                    tmp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=tmp;
                    swap=true;

                }

            }
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] array={133,34,23,1,4,90,66};
        bubbleSort1(array);
        System.out.println(Arrays.toString(array));
    }
}

Output result:
write picture description here

  • Code Analysis:
    write picture description here
  • direct insertion sort
package JiaYou;

import java.util.Arrays;

//常量池永远放的字符串
//写在方法内部属于栈
//直接插入排序:有跳跃式的比较,不稳定
//时间复杂度:O(n^2)
public class Day78 {
    public static void insrtSort(int[] array){
        int tmp=0;
        int j;
        for(int i=1;i<array.length;i++){
            tmp=array[i];//从i号位置开始进行比较
            for(j=i-1;j>=0;j--){
                if(array[j]>tmp){
                    array[j+1]=array[j];
                }else{//每次排序过后前面已经有序  找到第一个比tmp小的
                    break;
                }
            }
            array[j+1]=tmp;//tmp小的tmp  放到当前位置+1
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    int[] array={12,3,43,65,6};
    insrtSort(array);
    System.out.println(Arrays.toString(array));
    }

}

Output:
write picture description here
Code Analysis:

write picture description here

  • selection sort
package JiaYou;

import java.util.Arrays;

/*
 * 选择排序法
 * 不稳定,有跳跃式的交换
 * */
public class Day79 {
    public static void selectSort(int[] array){
        int tmp=0;
        int minIndex=0;
        for(int i=0;i<array.length;i++){
            minIndex=i;
            for(int j=i+1;j<array.length;j++){
                if(array[minIndex]>array[j]){
                    tmp=array[minIndex];
                    array[minIndex]=array[j];
                    array[j]=tmp;
                }
            }
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] array={12,3,43,65,6};
        selectSort(array);
        System.out.println(Arrays.toString(array));
    }

}

Output:
write picture description here
Code Analysis:
write picture description here

Guess you like

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