各种排序算法思想即实现

来源:互联网 发布:java 增删改查 缩写 编辑:程序博客网 时间:2024/05/01 11:41

下面的算法都会给出例子与分析,只写主要思想,有不懂的或者觉得我写的有错误可以留言,我都会关注的,谢谢~
选择排序:

public void selectSort(int[] a) {           for(int i = 0;i<a.length-1;i++){        int k = i;        for(int j = i;j<a.length;j++){            if(a[j]<a[k]){                k = j;            }        }        int min = a[i];        a[i] = a[k];        a[k] = min;    }}

原始数组: 5 3 16 6 9
第1次遍历: 3 5 16 6 9
第2次遍历: 3 5 16 6 9
第3次遍历: 3 5 6 16 9
第4次遍历: 3 5 6 9 16
每次在后面选择最小的一个与前面的数交换即可,前面的都是排好序的

插入排序:

public void insertSort(int[] a){    for(int i = 1;i<a.length;i++){        int num = a[i];        for(int j = i-1;j>=0;j--){            if(num<a[j]){                a[j+1] = a[j];            }else{                a[j+1] = num;                break;            }            if(j==0){                a[0] = num;            }        }    }}

原始数组: 5 3 16 1 9
第1次遍历: 3 5 16 1 9
第2次遍历: 3 5 16 1 9
第3次遍历: 1 3 5 16 9
第4次遍历: 1 3 5 9 16
就像起牌一样,每次把小牌往前面插,这样前面的数总是排序好的。

快速排序:

public int partition(int[] a,int low,int high){    int key = a[low];    while(low<high){        while(a[high]>=key&&high>low){            --high;        }        a[low] = a[high];        if(low<high){            low++;        }        while(a[low]<=key&&high>low){            ++low;        }        a[high] = a[low];        if(low<high){            high--;        }    }    a[low] = key;    return low;}public void qSort(int[] a,int s,int t){    if(s<t){        int privotkey = partition(a, s, t);        qSort(a, s, privotkey-1);        qSort(a, privotkey+1, t);    }}public void quickSort(int[] a){    qSort(a, 0, a.length-1);}

原始数组: 49 38 65 97 76 13 27 49
第1次partition: 27 38 13 49 76 97 65 49
第2次partition: 13 27 38 49 76 97 65 49
第3次partition: 13 27 38 49 49 65 76 97
第4次partition: 13 27 38 49 49 65 76 97
利用分治法的思想,以数组的第low个数为分界线,让数组的左边都小于a[low],右边都大于a[low]、

归并排序:

public static void mergeSort(int[] data) {      sort(data, 0, data.length - 1);  }  public static void sort(int[] data, int left, int right) {      if (left >= right)          return;      int center = (left + right) / 2;       sort(data, left, center);      sort(data, center + 1, right);      merge(data, left, center, right);      print(data);  }  public static void merge(int[] data, int left, int center, int right) {      int[] tmpArr = new int[data.length];      int mid = center + 1;      int third = left;      int tmp = left;      while (left <= center && mid <= right) {          if (data[left] <= data[mid]) {              tmpArr[third++] = data[left++];          } else {              tmpArr[third++] = data[mid++];          }      }        while (mid <= right) {          tmpArr[third++] = data[mid++];      }      while (left <= center) {          tmpArr[third++] = data[left++];      }       while (tmp <= right) {          data[tmp] = tmpArr[tmp++];      }  }  

利用分治法的思想,把数组分成两个子数组,把每个子数组都排好序,然后把它们合并成一个组。

计数排序:

private static void sort(int a[], int b[], int k)  {      //初始化计数数组      int c[] = new int[k];      for(int i = 0; i<k; i++)          c[i] = 0;      //计算数组中重复的次数      for(int i=0; i<a.length; i++)      {          c[a[i]] = c[a[i]]+1;      }      for(int i = 1; i<k; i++)      {          c[i] = c[i]+c[i-1];      }      //将a数组中的元素按照顺序复制到b中      for(int i = a.length-1; i>=0; i--)  //如果是从0到a.length,结果正确,相同元素逆序    {          b[c[a[i]]-1] = a[i];          c[a[i]] = c[a[i]]-1;      }      for (int i = 0; i < c.length; i++) {        System.out.println(c[i]);    }}  

对负数排序的扩展思路:计数排序要求元素能够作为数组的下标,自然不能是负数。我的思路是先把负数和非负数分离开来,对负数取绝对值,再对这两组数分别计数排序,最后再把两组数合并可以了。时间复杂度依旧是O(n),只是n会大一点。当然处理的都是整数。

基数排序:

public static int countDigit(int[] array) {    //求最大数的位数    int max = array[0];    for (int i = 1; i < array.length; i++) {        if (array[i] > max) {            max = array[i];        }    }    int time = 0;    while (max > 0) {        max /= 10;        time++;    }    return time;}private static void radixSort(int[] array,int radix, int distance) {       //array为待排序数组              array     //radix,代表基数                   10     //distance代表排序元素的位数        7     int length = array.length;       int[] temp = new int[length];//用于暂存元素       int[] count = new int[radix];//用于计数排序       int divide = 1;       System.out.println("distance"+distance);     for (int i = 0; i < distance; i++) {           System.arraycopy(array, 0,temp, 0, length);           Arrays.fill(count, 0);           for (int j = 0; j < length; j++) {               int tempKey = (temp[j]/divide)%radix;               count[tempKey]++;           }           for (int j = 1; j < radix; j++) {               count [j] = count[j] + count[j-1];           }           //个人觉的运用计数排序实现计数排序的重点在下面这个方法                       for (int j = length - 1; j >= 0; j--) {               int tempKey = (temp[j]/divide)%radix;               count[tempKey]--;               array[count[tempKey]] = temp[j];           }           divide = divide * radix;                       }             }  

基数排序就是按照关键字排序。常规的思维是先排高位数,排完高位数再排低位数。这样的话假如排三位数,那么排完百位再排十位的时候要考虑百位数是否相等,将大桶里分成了很多小桶,增加了难度,所以应该先从低位排起。

0 0
原创粉丝点击