惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
C
Cybersecurity and Infrastructure Security Agency CISA
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Know Your Adversary
Know Your Adversary
S
Securelist
I
Intezer
AWS News Blog
AWS News Blog
L
LINUX DO - 热门话题
P
Privacy International News Feed
Recent Announcements
Recent Announcements
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - 聂微东
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
The GitHub Blog
The GitHub Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Schneier on Security
Schneier on Security
N
Netflix TechBlog - Medium
爱范儿
爱范儿
B
Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
Blog — PlanetScale
Blog — PlanetScale
WordPress大学
WordPress大学
S
Secure Thoughts
K
Kaspersky official blog
N
News | PayPal Newsroom
O
OpenAI News
Last Week in AI
Last Week in AI
C
Check Point Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cyberwarzone
Cyberwarzone
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tor Project blog
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News
D
Docker
Hugging Face - Blog
Hugging Face - Blog
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
博客园 - 司徒正美
Martin Fowler
Martin Fowler
人人都是产品经理
人人都是产品经理
P
Palo Alto Networks Blog

博客园 - LeeXiaoLiang

自己写的一个javascript下拉列表类 jquery1.5.1根据元素ID获取元素对象 jquery向.ashx文件post中文乱码问题的解决 【转】通过在RowDataBound事件中把行索引绑定到控件的CommandArgument,然后在RowCommand事件中取出 - LeeXiaoLiang - 博客园 【转】GridView的RowCommand事件中取得行索引 - LeeXiaoLiang - 博客园 【转】GridView 实现服务器端和客户端全选的两种方法 ASP.NET之GridView数据绑定 - LeeXiaoLiang - 博客园 希尔排序的C语言实现(2) 希尔排序的C语言实现(1) 堆排序的C语言实现 对于堆排序算法的理解 【转】sqlserver中分页方法集锦 【转】高效的MySQL分页 【摘】完全二叉树 【原创】连接字符串数组 【摘】用C实现将数组转换为字符串 【原创】用C实现Trim()函数 - LeeXiaoLiang - 博客园 【转】浅谈数据库设计技巧 [转]数据库设计范式的理解
简单插入排序的C语言实现
LeeXiaoLiang · 2010-08-17 · via 博客园 - LeeXiaoLiang

代码

#include <stdio.h>
#include 
<stdlib.h>void PrintHeap(const char* strMsg,int array[],int nLength);
void InsertionSort1(int *items, int count) 
void InsertionSort2(int a[],int size);
void PrintArray(const char* strMsg,int array[],int nLength);int main(int argc, char *argv[])
{
  
int data[13]={8,5,4,6,13,7,1,9,12,11,3,10,2};
  InsertionSort1(data,
13);
  PrintArray(
"Insertion Sort:",data,13);
  
  system(
"PAUSE");    
  
return 0;
}
/*
插入排序思路:
    将数组分成两个区域:已排序区域和未排序区域。首先假设数组的第一个元素处于已排序区域,
    第一个元素之后的所有元素都处于未排序区域。
    排序时用到两层循环,第一层循环用于从未排序区域中取出待排序元素,并逐步缩小未排序区域,
    第二层循环用于从已排序区域中寻找插入位置(即不断地从已排序区域中寻找比待排序元素大的元素,
    然后将较大的已排序区的元素后移,后移的最终结果是已排序区元素的最后一个元素占据
    待排序元素原来的位置,而已排序区中间空出一个位置),最后将待排序元素插入元素后移后留下的空位。 
    注:待排序元素所在位置与已排序元素的最后一个元素是相邻的,因此进行移位循环时第一次后移时将已排序元素的最后一个元素直接移至待排序元素的位置即可。元素比较和元素移位共用一个循环,即边比较边移位。
*/
void InsertionSort1(int *items, int count)              
{                                                  
    
int x, y;                             
    
int c;                                        
                                                   
    
for ( x=1; x<count; ++x )                      
    {                                              
        c 
= items[x];                              
        
for ( y=x-1; (y>=0&& (c<items[y]); y-- ) 
            items[y
+1= items[y];                 
                                                   
        items[y
+1= c;                            
    }                                              
}
void InsertionSort2(int a[],int size)
{
     
int i,j,v;
     
//initially,the first item is considered to be sorted
     
//i divides a into a sorted region,x<i,and unsorted one,x>=i
     for(i=1;i<size;i++)
     {
        
//select the item at the beginning of the as yet unsorted section
        v=a[i];
        
//work backwards through the array,finding where v should go
        j=i;
        
//if this element is greater than v,move it up one
        while(a[j-1]>v)
        {
           a[j]
=a[j-1];
           j
--;
           
if(j<=0break;
        }
        
//stopped when a[j-1]<=v,put v at position
        a[j]=v;
     } 
}
void PrintArray(const char* strMsg,int array[],int nLength)
{
     
int i;
     printf(
"%s",strMsg);
     
for(i=0;i<nLength;i++)
     {
        printf(
"%d ",array[i]);
     }
     printf(
"\n");
}

参考资料:http://www.ahhf45.com/info/Data_Structures_and_Algorithms/algorithm/commonalg/sort/internal_sorting/insertion_sort/insertion_sort.htm http://baike.baidu.com/view/1193395.htm
http://www.javaeye.com/topic/547734