8种排序算法

java实现:

1、选择排序

查找最小值,然后将最小值与第0位交换。##查找最小值的序号,最后交换。

package demo01;
public class demo01 {
    public demo01() {
    }
    public int[] SelectSort(int[] a) {
        int len=a.length;
        if(a==null| len==0) {
            return a;
        }
        for(int i=0;i<len-1;i++) {
            int min=i;
            for(int j=i+1;j<len;j++) {
                if(a[j]<a[min]) {
                    min=j;
                }
            }
            int temp=a[i];
            a[i]=a[min];
            a[min]=temp;
        }
        return a;
    }
public static void main(String[] args) {
    int[] m= {9,12,1,7,2,17,1,5,91,4};
    demo01 demo01=new demo01();
    demo01.SelectSort(m);
    for(int i=0;i<m.length;i++) {
        System.out.print(m[i]+" ");
    }
}
}
1 12 9 7 2 17 1 5 91 4 
1 1 9 7 2 17 12 5 91 4 
1 1 2 7 9 17 12 5 91 4 
1 1 2 4 9 17 12 5 91 7 
1 1 2 4 5 17 12 9 91 7 
1 1 2 4 5 7 12 9 91 17 
1 1 2 4 5 7 9 12 91 17 
1 1 2 4 5 7 9 12 91 17 
1 1 2 4 5 7 9 12 17 91 
1 1 2 4 5 7 9 12 17 91 

2、插入排序

第k+1位与第k位对比,如果a[k+1]<a[k],就将k后移一位,直到左边的值小于它,然后将k+1位放到该位置。

public int[] InsertSort(int[] a) {
    int len=a.length;
    if(a==null| len==0) {
        return a;
    }
    for(int i=1;i<len;i++) {
        int j=i-1;
        int current=a[i];
        while(j>=0 && a[j]>current) {
            a[j+1]=a[j];
            j--;
            }
        a[j+1]=current;
        }
    return a;
}
{9,12,1,7,2,17,1,5,91,4};
9 12 1 7 2 17 1 5 91 4 
1 9 12 7 2 17 1 5 91 4 
1 7 9 12 2 17 1 5 91 4 
1 2 7 9 12 17 1 5 91 4 
1 2 7 9 12 17 1 5 91 4 
1 1 2 7 9 12 17 5 91 4 
1 1 2 5 7 9 12 17 91 4 
1 1 2 5 7 9 12 17 91 4 
1 1 2 4 5 7 9 12 17 91 
1 1 2 4 5 7 9 12 17 91 

2)将k+1与k对比,如果a[k+1]<a[k],则互换。

     for(int i=1;i<len;i++) {
          int j=i-1;
          int current=a[i];
          for(;j>=0;j--) {
              if(current<a[j]) {
                  int temp=a[j];
                  a[j]=a[j+1];
                  a[j+1]=temp;
              }
          }

 

3、冒泡排序

1)第一个开始比,如果比第二个大就换位置,每次都是和相邻的对比和交换值,一轮对比后最大值在最右边。

2)冒泡排序的优化:取一个标记位,如果从开始的第一对比到最后一个,相邻元素之间都没有进行交换,说明数组是有序的,无需对剩余元素重复比较。

public int[] BubbleSort(int[] a) {
      int len=a.length;
      if(a==null| len==0) {
          return a;
      }
      for(int i=0;i<len;i++) {
          for(int j=0;j<len-1-i;j++) {
              if(a[j]>a[j+1]) {
                  int temp=a[j+1];
                  a[j+1]=a[j];
                  a[j]=temp;
              }
          }
      }
      return a;
}
{9,12,1,7,2,17,1,5,91,4};
9 1 7 2 12 1 5 17 4 91 
1 7 2 9 1 5 12 4 17 91 
1 2 7 1 5 9 4 12 17 91 
1 2 1 5 7 4 9 12 17 91 
1 1 2 5 4 7 9 12 17 91 
1 1 2 4 5 7 9 12 17 91 
1 1 2 4 5 7 9 12 17 91 
1 1 2 4 5 7 9 12 17 91 
1 1 2 4 5 7 9 12 17 91 
1 1 2 4 5 7 9 12 17 91 
1 1 2 4 5 7 9 12 17 91 

2)优化

for(int i=0;i<len-1;i++) {
          boolean flag=true;
          for(int j=0;j<len-1-i;j++) {
              if(a[j]>a[j+1]) {
                  int temp=a[j+1];
                  a[j+1]=a[j];
                  a[j]=temp;
                  flag=false;
              }
          }
          if(flag) {
              break;
          }
}
{9,12,1,7,2,17,1,5,91,4};
9 1 7 2 12 1 5 17 4 91 
1 7 2 9 1 5 12 4 17 91 
1 2 7 1 5 9 4 12 17 91 
1 2 1 5 7 4 9 12 17 91 
1 1 2 5 4 7 9 12 17 91 
1 1 2 4 5 7 9 12 17 91 
1 1 2 4 5 7 9 12 17 91 

4、希尔排序(插入排序的变种)

 

posted @ 2021-11-29 21:39  min222  阅读(41)  评论(0编辑  收藏  举报