常用排序算法--Insertion Sort(插入排序)

本文详细介绍了一种简单的排序算法——插入排序。通过逐步迭代的方式将数组中的每个元素放置到正确的位置,实现整个数组的有序排列。文章提供了插入排序的C语言实现代码,并解释了其工作原理。

Insertion Sort VIsualization

/* Logic : Suppose if the array is sorted till index i then we can sort the arry till i+1 by inserting      
i+1 th element in the correct position from 0 to i+1. The position at which (i+1)th element has    
to be inserted has to be found by iterating from 0 to i. As any array is sorted till 0th postion          
(Single element is always sorted) and we know how to expand, we can sort the whole array */ 
void InsertionSort(int *array , int number_of_elements){    
    int iter,jter;        
    for(iter=1;iter<number_of_elements;iter++)  
    {
        int current_element = array[iter];    
        jter = iter-1;        
        while(jter>=0 && array[jter] > current_element)        
        {                  
            array[jter+1] = array[jter];            
            jter--;           
        }         
        array[jter+1] = current_element;      
    }
}

转载于:https://www.cnblogs.com/gigbit/archive/2013/03/21/2974171.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值