亘古微博
咨询邮箱:nicholas@anycle.com
您的位置:网站首页 > 新闻动态 > 算法专题 >
[C] Insertion sort
来源:未知 作者:admin 发表于:2015-03-28 11:17  点击
This is the insertion sort function by nicholas.

#define OQ_LEN 10
#define OQ_VALUE 1,2,3,4,5,6,8,7,9,0

unsigned int original_queue[OQ_LEN] = {OQ_VALUE};


void insertion_sort(){
        int queue_len = OQ_LEN;
        int tmp = 0;
        int j = 0;
        printf("\n******************* insertion_sort testing begin ****************\n");
        for(int i=1; i<queue_len; i++){
                tmp = original_queue[i];
                for(j=0; j<i; j++){
                        if(tmp < original_queue[j]){
                                for(int k=i; k>j; k--){
                                        original_queue[k] = original_queue[k-1];
                                }
                                break;
                        }
                }
                original_queue[j] = tmp;
        }       
        for(int i=0; i<queue_len; i++){
                printf(" %d", original_queue[i]);
        }
        printf("\n");
}


Result:
******************* insertion_sort testing begin ****************
 0 1 2 3 4 5 6 7 8 9


相关文章推荐
[C] Insertion sort

This is the insertion sort function by nicholas....

[C] Binary sort

Just a simple example of binary sort....

[C] Bubble sort

Bubble sort should be avoided in the case of large collections. It will not be ef...

[JS] How boys and girls stand balance

How boys and girls stand balance...