• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
乐碎碎
程序媛想的事儿
博客园    首页    新随笔    联系   管理    订阅  订阅
插入排序
 1 // 插入排序.cpp : 定义控制台应用程序的入口点。
 2 
 3 #include "stdafx.h"
 4 
 5 void insertsort(int a[],int n)
 6 {
 7     int i,key;
 8     for(int j=1;j<n;j++)
 9     {
10         key=a[j];
11         i=j-1;
12         while((i>=0)&&(a[i]>key)) //交换a[i]和a[i+1]
13         {
14             int temp;
15             temp=a[i];
16             a[i]=a[i+1];
17             a[i+1]=temp;
18             i--;
19         }
20         a[i+1]=key;
21     }
22 }
23 
24 void main()
25 {
26     int arr[]={5,2,4,6,1,3,24,12,57,7};
27     int len=sizeof(arr)/sizeof(arr[0]);
28     for(int i=0;i<len;i++)
29         printf("%4d",arr[i]);
30     printf("\n");
31     insertsort(arr,len);
32     for(int i=0;i<len;i++)
33         printf("%4d",arr[i]);
34 }

 

 

posted on 2012-10-04 18:59  xingle0917  阅读(145)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3