Algorithm 04-Common sorting algorithm for chess and card games

Algorithm 04-Common sorting algorithm for chess and card games

1. Introduction

Common sorting algorithms for chess and card games include: chain radix sort, insertion sort, and Hill sort.

2. Chain radix sort

1. Basic idea

Radix sort (Radix sort) belongs to "distribution sort", which divides these elements into m groups according to a certain characteristic of data elements, and then sorts the elements in the group according to another characteristic, that is, a set of data into m groups of sorted data. Radix sort is a stable sort.

Chain radix sort is a radix sort based on the priority of data element features. First, according to the features of low priority, these elements are divided into m groups, and then they are concatenated into a set of data in a certain order; then according to high priority The features are grouped and concatenated. After sorting in this way, it becomes a set of data sorted by multiple features.

Chained radix sort is used for card game shuffling.

2. Code implementation

/**
 * 这里以麻将的排序来说明
 * @param list 麻将集合
 */
public static void linkedRadixSort(LinkedList<MaJiang> list) {
    if (Tool.isEmpty(list)) {
        return;
    }

    //先按点数分组
    LinkedList<MaJiang>[] rankList = new LinkedList[9];
    for (int i = 0; i < rankList.length; i++) {
        rankList[i] = new LinkedList<MaJiang>();
    }
    //按点数添加数据
    while (list.size() > 0) {
        MaJiang mj = list.remove();
        rankList[mj.rank - 1].add(mj);
    }
    //合并分组
    for (int i = 0; i < rankList.length; i++) {
        list.addAll(rankList[i]);
    }

    // 再按花色分组
    rankList = new LinkedList[3];
    for (int i = 0; i < rankList.length; i++) {
        rankList[i] = new LinkedList<MaJiang>();
    }
    //按花色添加数据
    while (list.size() > 0) {
        MaJiang mj = list.remove();
        rankList[mj.suit - 1].add(mj);
    }
    //合并分组
    for (int i = 0; i < rankList.length; i++) {
        list.addAll(rankList[i]);
    }
}

3. Insertion sort

1. Basic idea

The basic idea of ​​insertion sort is: insert a record to be sorted in each step, according to the size of its key code value, into the appropriate position in the previously sorted file until all the records are inserted.

Insertion sort is suitable for data that is almost already sorted, such as: the insertion of cards in chess and card games.

2. Code implementation

/**
 * @param arr 要排序的数组
 */
public static <E extends Comparable<E>> void insertSort(E[] arr) {
    if (arr == null || arr.length == 0) {
        return;
    }
    //从前往后排序,先把前面所有的元素排好序,然后把后面的元素插到前面拍好的队列中
    for (int i = 1; i < arr.length; i++) {
        E temp = arr[i];
        int j = i;
        //找到需要插入的位置
        while (j > 0 && temp.compareTo(arr[j - 1]) < 0) {
            //后移一位
            arr[j] = arr[j - 1];
            j--;
        }
        arr[j] = temp;
    }
}

4. Hill sort

1. Basic idea

Shell sort (Shell's Sort) divides the entire ordered sequence into several small subsequences for insertion sorting, which is a more efficient and improved version of the direct insertion sorting algorithm. Hill sort is an unstable sorting algorithm.

Sorting process: first take an integer d1 less than n as the first increment (also called step size), and group all the records of the file. All records with distances that are multiples of d1 are placed in the same group. First perform direct insertion sort in each group; then, take the second increment d2

2. Code implementation

/**
 * @param arr 要排序的数组
 */
public static <E extends Comparable<E>> void shellSort(E[] arr) {
    if (Tool.isEmpty(arr)) {
        return;
    }
    shellSort(arr, 4);
    shellSort(arr, 1);
}

/**
 * 按步长进行希尔排序
 * @param arr  要排序的数组
 * @param step 步长
 */
public static <E extends Comparable<E>> void shellSort(E[] arr, int step) {
    if (Tool.isEmpty(arr)) {
        return;
    }
    for (int m = 0; m < step; m++) {
        for (int i = m + step; i < arr.length; i += step) {
            E temp = arr[i];
            int j = i;
            //与上一个步长的数据比较
            while (j - step >= 0 && temp.compareTo(arr[j - step]) < 0) {
                //后移一个步长位
                arr[j] = arr[j - step];
                j -= step;
            }
            arr[j] = temp;
        }
    }

}

The demo has been uploaded to gitee, and students who need it can download it!

Previous: Algorithm 03 - Divide and Conquer

Next

Guess you like

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