我们在程序中经常需要对数据列表进行排序,有时候使用SOQL的order by 不一定能完全符合需求,需要对数据进行排序,排序可以有多种方式,不同的方式针对不同的场景。篇一只是简单的描述一下选择排序,插入排序以及插入排序优化版--希尔排序。

一.选择排序

选择排序的中心思想为第一轮找到数组中最小的值,将最小值和第一个元素交换位置,第二轮找到剩余数组的最小值,将其和第二个元素交换,以此类推。

选择排序的特点如下:

1.比较次数:n * (n-1) / 2
2.交换次数:N
4.运行时间和输入无关
5.数据移动次数是最小的,每次移动都只会改变两个数组的值

apex不像java,基础类型类似Integer,String等均没有实现Comparable接口,所以方法中均已Integer类型进行处理,有兴趣的可以自行修改成Comparable或者其他类型。

 1 public static Integer[] selectionSort(Integer[] datas) {
 2    Integer dataLength = datas.size();
 3    for(Integer i = 0;i < dataLength;i++) {
 4      Integer min = i;
 5      for(Integer j = i + 1;j < dataLength;j++) {
 6         if(datas[min] > datas[j]) {
 7            min = j;
 8         }
 9      }
10      Integer temp = datas[i];
11      datas[i] = datas[min];
12      datas[min] = temp;
13    }
14    return datas;
15 }

实现Comparable接口的代码:

 1 public static Comparable[] selectionSort1(Comparable[] datas) {
 2     Integer dataLength = datas.size();
 3     for(Integer i = 0;i < dataLength;i++) {
 4         Integer min = i;
 5         for(Integer j = i;j < dataLength;j++) {
 6             if(datas[min].compareTo(datas[j]) > 0) {
 7                 min = j;
 8              }
 9          }
10          Comparable temp = datas[i];
11          datas[i] = datas[min];
12          datas[min] = temp;
13      }
14      return datas;
15  }    

二.插入排序

插入排序的中心为对左边的第i(i在1~n之间)项进行排序,保证前i项是有序的,等排到最右边一位,则整个数组变为有序的数列。

插入排序特点:

 比较次数:N^2/4
 交换次数:N^2/4
 所需时间取决于数组元素的初始顺序,如果初始顺序相对有序,排序会快很多
 插入排序适用情景:
 1.数组中的每个元素距离他的最终位置不远
 2.一个有序的大数组接一个小数组
 3.数组中只有几个元素位置不正确
 实现规则:每一轮都对左边的i项数据进行排序,保证前i项有序
代码如下:

 1     public static Integer[] insertionSort(Integer[] datas) {
 2         Integer dataLength = datas.size();
 3         for(Integer i = 1;i < dataLength;i++) {
 4             for(Integer j = i; j > 0 && datas[j] < datas[j-1];j--) {
 5                 Integer temp = datas[j-1];
 6                 datas[j-1] = datas[j];
 7                 datas[j] = temp;
 8             }
 9         }
10         return datas;
11     }

三.希尔排序

插入排序适用于数组中只有几个元素不正确,但是如果最小的一位如果在数组的最后一位,相当于需要移动N个位置。希尔排序可以用于快速交换不相邻的元素以对数组的局部进行排序,思想为使数组中任意间隔为H的元素都是有序的。一个H有序数组就是H个互相独立的有序数组放在一起形成的一个数组。如果H很大,我们就可以将元素一次移动到很远的地方。H取值可以取2,3,4...n

关于希尔排序可以查看:https://baike.baidu.com/item/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F/3229428?fr=aladdin

下面代码描述为当H为3的排序  元素的间隔规律为a[n+1] = a[n] + 3 ^ n

public static String[] shellSort(String[] datas) {
        Integer dataLength = datas.size();
        Integer h = 1;
        while(h < dataLength/3) {
            h = 3 * h + 1;//1,4,13,40 ...
        }
        System.debug(LoggingLevel.INFO, '*** h: ' + h);
        System.debug(LoggingLevel.INFO, '*** dataLength: ' + dataLength);
        while(h >= 1) {
            for(Integer i = h;i < dataLength;i++) {
                //将a[i]插入到a[i-h],a[i-2h],a[i-3h]  ....中
                for(Integer j = i;j >= h && datas[j] < datas[j-h];j -= h) {
                    String temp = datas[j-h];
                    datas[j-h] = datas[j];
                    datas[j] = temp;
                }
                System.debug(LoggingLevel.INFO, '*** i: ' + i);
            }
            System.debug(LoggingLevel.INFO, '*** datas: ' + String.valueOf(datas));
            System.debug(LoggingLevel.INFO, '*** h: ' + h);
            h = h / 3;
            
        }
        return datas;
    }

当数组为 S H E L L S O R T E X A M P L E 情况下,使用H为3的每次运行如下图,图不全,内容自己补充。

salesforce零基础学习(七十九)简单排序浅谈   篇一

运行后,可以将数组排成升序的有序列表。

总结:此三种排序方式,单纯效率考虑的话,可以使用希尔排序,优化了插入排序的不足,同时没有特别的占用内存。篇二会描述一下其他排序方式。额,题外话,还是不太希望今天有人看到这篇博客的,祝大家七夕快乐。

分类:

技术点:

相关文章: