2-way insertion sort algorithm c



提示:以下是本篇文章正文内容,下面案例可供参考

one,

The 2-way insertion sort algorithm is improved on the basis of the half insertion sort, reducing the number of times it moves records during the sorting process to improve efficiency.

The specific implementation idea is: set another array d with the same size as the array storing records, add the first record in the unordered list to the position of d[0], and then start from the second record in the unordered list , Compare with d[0]: if the value is greater than d[0], it is added to its right side; otherwise, it is added to its left side.

The array d here can be understood as a circular array.

The process of sorting an unordered list using the 2-way insertion sort algorithm {3,1,7,5,2,4,9,6}is as follows:

  • Add record 3 to array d:


 

  • Then insert 1 into the array d, as shown in the following figure :


 

  • Insert record 7 into array d, as shown in the following figure:


 

  • Insert record 5 into array d. Since it is smaller than 7 but larger than 3, the position of 7 needs to be moved, and then 5 is inserted, as shown in the following figure:


 

  • Insert record 2 into array d. Since it is larger than 1 and smaller than 3, it is necessary to move the positions of 3, 7, and 5, and then insert 2, as shown in the following figure:


 

  • To insert record 4 into array d, the positions of 5 and 7 need to be moved, as shown in the figure below:


 

  • Insert record 9 into array d, as shown in the following figure:


 

  • Insert record 6 into array d, as shown in the following figure:


 

When finally stored in the original array, it will be stored sequentially starting from d[7]

Two, the code

#include <stdio.h>
#include <stdlib.h>
void insert(int arr[], int temp[], int n)
{
    int i,first,final,k;
    first = final = 0;//分别记录temp数组中最大值和最小值的位置
    temp[0] = arr[0];
    for (i = 1; i < n; i ++){
        // 待插入元素比最小的元素小
        if (arr[i] < temp[first]){
            first = (first - 1 + n) % n;
            temp[first] = arr[i];
        }
        // 待插入元素比最大元素大
        else if (arr[i] > temp[final]){
            final = (final + 1 + n) % n;
            temp[final] = arr[i];
        }
        // 插入元素比最小大,比最大小
        else {
            k = (final + 1 + n) % n;
            //当插入值比当前值小时,需要移动当前值的位置
            while (temp[((k - 1) + n) % n] > arr[i]) {
                temp[(k + n) % n] =temp[(k - 1 + n) % n];
                k = (k - 1 + n) % n;
            }
            //插入该值
            temp[(k + n) % n] = arr[i];
            //因为最大值的位置改变,所以需要实时更新final的位置
            final = (final + 1 + n) % n;
        }
    }
    // 将排序记录复制到原来的顺序表里
    for (k = 0; k < n; k ++) {
        arr[k] = temp[(first + k) % n];
    }
}

int main()
{
    int a[8] = {3,1,7,5,2,4,9,6};
    int temp[8];
    insert(a,temp,8);
    for (int i = 0; i < 8; i ++){
        printf("%d ", a[i]);
    }
    return 0;
}

​​​​​​​


Summarize

Guess you like

Origin blog.csdn.net/weixin_43537097/article/details/128021633