A recent project used sorting algorithms, so we looked at eight commonly used sorting algorithms. Because it is used in 8-bit SCM, so the memory and time requirements are relatively high, it is best not to take up extra space, and the time is short. So the commonly used 8 algorithm time to do a test. Through the LED on and off to indicate the beginning and completion of the algorithm, and then use oscilloscope test LED pin level to judge the algorithm time.

The 8 algorithm codes are as follows:

#include "sort.h"
#include "math.h"

#define EXCHANGE(num1, num2)  { num1 = num1 ^ num2; num2 = num1 ^ num2; num1 = num1 ^ num2;}



void swap( int* a, int* b )
{
    int t;
    t = *a;
    *a = *b;
    *b = t;
}


/*
一.冒泡排序
冒泡排序原理很容易理解,就是重复地走访过要排序的元素列,依次比较两个相邻的元素,顺序不对就交换,直至没有相邻元素需要交换,也就是排序完成。
这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端(升序或降序排列),就如同碳酸饮料中二氧化碳的气泡最终会上浮到顶端一样,故名“冒泡排序”。

冒泡排序是一种稳定排序算法。
时间复杂度:最好情况(初始情况就是正序)下是o(n),平均情况是o(n2)
*/
void bubbleSort( int num[], int count )
{
    for( int i = 0; i < count - 1; i++ )
    {
        for( int j = 0; j < count - i - 1; j++ )
        {
            if( num[j] > num[j + 1] )
            {
                //EXCHANGE( num[j], num[j + 1] );               //通过宏定义交换两个值
                swap( &num[j], &num[j + 1] );                   //通过函数交换两个值
            }
        }
    }
}

/*
设置一标志性变量pos,用于记录每趟排序中最后一次进行交换的位置。
由于pos位置之后的记录均已交换到位,故在进行下一趟排序时只要扫描到pos位置即可。
*/

void bubbleSort_1( int num[], int count )
{
    int i = count - 1; //初始时,最后位置保持不变
    while( i > 0 )
    {
        int pos = 0; //每趟开始时,无记录交换
        for( int j = 0; j < i; j++ )
        {
            if( num[j] > num[j + 1] )
            {
                pos = j; //记录交换的位置
                EXCHANGE( num[j], num[j + 1] );
            }
        }
        i = pos; //为下一趟排序作准备
    }
}
/*
传统冒泡排序中每一趟排序操作只能找到一个最大值或最小值,
我们考虑利用在每趟排序中进行正向和反向两遍冒泡的方法一次可以得到两个最终值(最大者和最小者) , 从而使排序趟数几乎减少了一半。
*/

void bubbleSort_2( int num[], int count )
{
    int low = 0;
    int high = count - 1; //设置变量的初始值
    int  j;
    while( low < high )
    {
        for( j = low; j < high; ++j ) //正向冒泡,找到最大者
        {
            if( num[j] > num[j + 1] )
            {
                EXCHANGE( num[j], num[j + 1] );
            }
        }
        --high;  //修改high值, 前移一位
        for( j = high; j > low; --j ) //反向冒泡,找到最小者
        {
            if( num[j] < num[j - 1] )
            {
                EXCHANGE( num[j], num[j - 1] );
            }
        }
        ++low;  //修改low值,后移一位
    }
}

/*
二、选择排序
选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,
存放在序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到全部待排序的数据元素排完。
选择排序的交换操作介于 0 和 (n - 1) 次之间。选择排序的比较操作为 n (n - 1) / 2 次之间。选择排序的赋值操作介于 0 和 3 (n - 1) 次之间。
比较次数O(n2),比较次数与关键字的初始状态无关,总的比较次数N=(n-1)+(n-2)+...+1=n*(n-1)/2。交换次数O(n),最好情况是,已经有序,交换0次;
最坏情况交换n-1次,逆序交换n/2次。交换次数比冒泡排序少多了,由于交换所需CPU时间比比较所需的CPU时间多,n值较小时,选择排序比冒泡排序快

选择排序是不稳定的排序方法。
时间复杂度:最好和平均情况下都是O(n2)
*/
void selectSort( int num[], int count )
{
    for( int i = 0; i < count; i++ )
    {
        int min = i;
        for( int j = i; j < count; j++ )
        {
            if( num[j] < num[min] )
            {
                min = j;
            }
        }
        if( i != min )
        {
            EXCHANGE( num[i], num[min] );    //可以看出,最多交换count - 1次
        }
    }
}

/*
三、二元选择排序

简单选择排序,每趟循环只能确定一个元素排序后的定位。
我们可以考虑改进为每趟循环确定两个元素(当前趟最大和最小记录)的位置,
从而减少排序所需的循环次数。改进后对n个数据进行排序,最多只需进行[n/2]趟循环即可.
*/

void selectSortBinary( int num[], int count )
{
    int i, j, max, min;                 //下标min max 指向最小和最大元素
    int maxtmp = 0, mintmp = 0;
    for( i = 0; i < count / 2; i++ )    //i跑 n/2趟排序就会排序完成
    {
        min = max = i;                  //先将max和min都指向待排序的第一个元素
        for( j = i; j < count - i; j++ )
        {
            if( num[j] < num[min] )     //用j找出最大值和最小值,分别让max 和min指上来
            {
                min = j;
                continue;
            }
            if( num[j] > num[max] )
            {
                max = j;
            }
        }
        maxtmp = num[max];              //备份当前最大值和最小值
        mintmp = num[min];
        num[min] = num[i];              // 队首元素放在最小值下标处
        num[max] = num[count - i - 1];  //队尾元素放在最大值下标处
        num[i] = mintmp;                //最小值放到队首
        num[count - i - 1] =  maxtmp;   //最大值放到队尾
    }
}






/*
四、直接插入排序
插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的、个数加一的有序数据,算法适用于少量数据的排序,
插入排序的基本思想是:每步将一个待排序的记录,按其关键码值的大小插入前面已经排序的文件中适当位置上,直到全部插入完为止

直接插入排序是稳定的排序算法。
时间复杂度:最好情况(初始情况就是正序)下是o(n),平均情况是o(n2)
---------------------

*/
void insertSort( int num[], int count )
{
    int i, j;
    for( i = 1; i < count; i++ )
    {
        if( num[i] < num[i - 1] )
        {
            int temp = num[i];
            for( j = i; j > 0; j-- )    //num[i] 前面的每一个数字和num[i]比较
            {
                if( num[j - 1] > temp ) //如果num[i]前面的值比num[i]大
                {
                    num[j] = num[j - 1];//将大的值向后挪一位
                }
                else
                {
                    break;
                }
            }
            num[j] = temp;              //将num[i]位置的值放到最后一次挪位置的地方
        }                               //也就是将比num[i]大的值依次向后挪一位,将num[i]直接放到前面
    }
}

/*
五、二分插入排序

由于在插入排序过程中,待插入数据左边的序列总是有序的,针对有序序列,就可以用二分法去插入数据了,也就是二分插入排序法。适用于数据量比较大的情况。
二分插入排序的算法思想:
算法的基本过程:
(1)计算 0 ~ i-1 的中间点,用 i 索引处的元素与中间值进行比较,如果 i 索引处的元素大,说明要插入的这个元素应该在中间值和刚加入i索引之间,
反之,就是在刚开始的位置 到中间值的位置,这样很简单的完成了折半;
(2)在相应的半个范围里面找插入的位置时,不断的用(1)步骤缩小范围,不停的折半,范围依次缩小为 1/2 1/4 1/8 .......快速的确定出第 i 个元素要插在什么地方;
(3)确定位置之后,将整个序列后移,并将元素插入到相应位置。

二分插入排序是稳定的排序算法。
时间复杂度:最好情况(刚好插入位置为二分位置)下是o(nlogn),平均情况和最坏情况是o(n2)
*/

void insertSortBinary( int num[], int count )
{
    int i, j;
    for( i = 1; i < count; i++ )
    {
        if( num[i] < num[i - 1] )
        {
            int temp = num[i];
            int left = 0, right = i - 1;
            while( left <= right )
            {
                int mid = ( left + right ) / 2;
                if( num[mid] < temp )
                {
                    left = mid + 1;
                }
                else
                {
                    right = mid - 1;
                }
            }
            //只是比较次数变少了,交换次数还是一样的
            for( j = i; j > left; j-- )
            {
                num[j] = num[j - 1];
            }
            num[left] = temp;
        }
    }
}

/*
六、希尔(插入)排序
希尔排序(Shell's Sort)是插入排序的一种又称“缩小增量排序”(Diminishing Increment Sort),是直接插入排序算法的一种更高效的改进版本。
希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,排序完成。

希尔排序是非稳定排序算法。
时间复杂度:O(n^(1.3—2))

*/
void shellSort( int num[], int count )
{
    int gap, i, j;
    for( gap = count / 2; gap >0; gap = gap / 2 ) //拆分整个序列,元素间距为gap(也就是增量)
    {
        for( i = gap ; i < count; i++ ) //对子序列进行直接插入排序
        {
            for( j = i - gap; j >= 0 && num[j] > num[j + gap]; j = j - gap )
            {
                EXCHANGE( num[j], num[j + gap] );
            }
        }
    }
}



/*
七、快速排序
快速排序(Quicksort)是对冒泡排序的一种改进。

它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,
然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。

快速排序是非稳定的排序算法
时间复杂度:最差为O(logn2),平均为O(nlogn),最好为O(nlogn)

*/
void quickSort( int num[], int count, int left, int right )
{
    if( left >= right )
    {
        return ;
    }
    int key = num[left];
    int lp = left;                                //左下标
    int rp = right;                               //右下标
    while( lp < rp )
    {
        if( num[rp] < key )
        {
            int temp = num[rp];
            for( int i = rp - 1; i >= lp; i-- )
            {
                num[i + 1] = num[i];
            }
            num[lp] = temp;
            lp ++;
            rp ++;
        }
        rp --;
    }
    quickSort( num, count, left, lp - 1 );        //对左半部分排序
    quickSort( num, count, rp + 1, right );       //对右半部分排序
}

/*
八、堆排序
是指利用堆这种数据结构所设计的一种排序算法。堆是一个近似完全二叉树的结构,并同时满足堆积的性质:
即子结点的键值或索引总是小于(或者大于)它的父节点
在堆的数据结构中,堆中的最大值总是位于根节点(在优先队列中使用堆的话堆中的最小值位于根节点)。
堆中定义以下几种操作:
最大堆调整(Max Heapify):将堆的末端子节点作调整,使得子节点永远小于父节点
创建最大堆(Build Max Heap):将堆中的所有数据重新排序
堆排序(HeapSort):移除位在第一个数据的根节点,并做最大堆调整的递归运算

堆排序是一个非稳定的排序算法。
时间复杂度:O(nlogn)

*/

void maxHeapify( int num[], int start, int end )
{
    //建立父节点指标和子节点指标
    int dad = start;
    int son = dad * 2 + 1;
    while( son <= end )  //若子节点指标在范围内才做比较
    {
        if( son + 1 <= end && num[son] < num[son + 1] ) //先比较两个子节点大小,选择最大的
        {
            son++;
        }
        if( num[dad] > num[son] ) //如果父节点大於子节点代表调整完毕,直接跳出函数
        {
            return;
        }
        else   //否则交换父子内容再继续子节点和孙节点比较
        {
            EXCHANGE( num[dad], num[son] )
            dad = son;
            son = dad * 2 + 1;
        }
    }
}

void heapSort( int num[], int count )
{
    int i;
    //初始化,i从最後一个父节点开始调整
    for( i = count / 2 - 1; i >= 0; i-- )
    {
        maxHeapify( num, i, count - 1 );
    }
    //先将第一个元素和已排好元素前一位做交换,再重新调整,直到排序完毕
    for( i = count - 1; i > 0; i-- )
    {
        EXCHANGE( num[0], num[i] )
        maxHeapify( num, 0, i - 1 );
    }
}
Copy the code

The test method is to randomly generate 100 integers between 0 and 9999 in the array, and then use the sorting algorithm to sort.

The test code is as follows:

// Generate a random number group void randNum(int* num, int count) {int I = 0; while( i < count ) { num[i++] = rand() % 9999; } } void bubbleSortTest( int num[], int count ) { randNum( num, count ); LED = 1; bubbleSort( num, count ); LED = 0; } void bubbleSort_1Test( int num[], int count ) { randNum( num, count ); LED = 1; bubbleSort_1( num, count ); LED = 0; } void bubbleSort_2Test( int num[], int count ) { randNum( num, count ); LED = 1; bubbleSort_2( num, count ); LED = 0; } void selectSortTest( int num[], int count ) { randNum( num, count ); LED = 1; selectSort( num, count ); LED = 0; } void selectSortBinaryTest( int num[], int count ) { randNum( num, count ); LED = 1; selectSortBinary( num, count ); LED = 0; } void insertSortTest( int num[], int count ) { randNum( num, count ); LED = 1; insertSort( num, count ); LED = 0; } void insertSortBinaryTest( int num[], int count ) { randNum( num, count ); LED = 1; insertSortBinary( num, count ); LED = 0; } void shellSortTest( int num[], int count ) { randNum( num, count ); LED = 1; shellSort( num, count ); LED = 0; } void quickSortTest( int num[], int count ) { randNum( num, count ); LED = 1; quickSort( num, count, 1, count - 1 ); LED = 0; } void heapSortTest( int num[], int count ) { randNum( num, count ); LED = 1; heapSort( num, count ); LED = 0; }Copy the code

The high level time of LED lamp was observed by oscilloscope to determine the time consuming of each algorithm. The test results are as follows:

        LED = 1;                                                   //136ns   

LED = 0;                                                   //240ns

randNum( num, 100 );                              //4ms      



bubbleSortTest( num, 100 );                     //20–22ms

bubbleSort_1Test( num, 100 );                 //30–35ms

bubbleSort_2Test( num, 100 );                 //31–35ms

selectSortTest( num, 100 );                      //12–14ms

selectSortBinaryTest( num, 100 );            //9–10ms

insertSortTest( num, 100 );                       //8–10ms

insertSortBinaryTest( num, 100 );             //7–9ms

shellSortTest( num, 100 );                         //5–8ms

quickSortTest( num, 100 );                        //13–30ms

heapSortTest( num, 100 );                        //8–10ms

Because the random numbers generated in the array are different each time, the sorting time is not exactly the same, the above time is the time range of the shortest and longest time.

It can be seen from the above test that Hill sort takes the shortest time and has the highest efficiency. At the same time, there is no extra space to open up, saving SCM resources. Therefore, hill ranking method is used in the project.