CLRS:Insert sort in in c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#define ARRAY_SIZE 1000
int buf[ARRAY_SIZE];
int main()
{
int i,j,n;
srand((unsigned int)time(0));
memset(buf,0,sizeof(buf[0]));
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)buf[i]=rand()%100;//creat th data storage in buf
for(i=1;i<=n;i++)printf("%d ",buf[i]);//before sort
printf("\nAffter sort:\n");
for(j=2;j<=n;j++)
{
buf[0]=buf[j];
for(i=j-1;i>0&&buf[i]>buf[0];i--)buf[i+1]=buf[i];
buf[i+1]=buf[0];
}
for(i=1;i<=n;i++)printf("%d ",buf[i]);//after sort
printf("\n");
}
}
CLRS:Insert sort in in c的更多相关文章
- 计数排序(Count Sort )与插入排序(Insert Sort)
计数排序法:计数数组适用于当前数组密集的情况.例如(2,3,5,4,2,3,3,2,5,4) 方法:先找出最大值最小值,之后统计每个数出现的次数,根据次数从小到大往数组里添加 计数排序法是一种不需要比 ...
- c++算法联系,冒泡排序,bubble sort,插入排序,insert sort,
#include <iostream.h> #define MAX 100 void dispaly(int a[],int n) { for(int i=0;i<n;i+ ...
- Insert Sort Singly List
对单链表插入排序,给出个单链表的head节点:返回排完序的head节点: 首先数据结构中习惯了以数组为参数排序,瞬间想到是遍历单链表存入arraylist中,再进行insert sort,(O(n** ...
- insert sort
插入排序将数据分为前面有序部分和后面无序部分,取无序部分的第一个元素插入到有序序列中. 注意与选择排序的区别. // insert sortvoid insertionSort(int arr[], ...
- leetcode:Insert Sort List
问题描写叙述 对一个单链表进行插入排序,head指向第一个结点. 代码 /** * Definition for singly-linked list. * struct ListNode { * i ...
- 插入排序(1)——直接插入排序(insert sort)
假设有一组无序序列 R0, R1, ... , RN-1. (1) 我们先将这个序列中下标为 0 的元素视为元素个数为 1 的有序序列. (2) 然后,我们要依次把 R1, R2, ... , RN- ...
- Java经典算法之插入排序(Insert Sort)
插入排序在局部有序的情况下比冒泡排序快一倍,比选择排序快一点. 那什么是插入排序,就是将局部有序的数据向右移动,将未排序的数据插到他的前面 下面我们来解析代码: 这里外层循环out变量从1开始向右移动 ...
- Sort Methods
heyheyhey ~~ It has been a long time since i come here again...whatever today i will summerize some ...
- CLRS:sorting in linear time O(n)
//intput array A,output array result. count array count . //all the elements is in te range of 0~k ...
随机推荐
- Java事务处理全解析(三)——丑陋的案例
在本系列的上一篇文章中,我们看到了一个典型的事务处理失败的案例,其主要原因在于,service层和各个DAO所使用的Connection是不一样的,而JDBC中事务处理的作用对象正是Connectio ...
- Spring配置xml文件详解
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- CLR via C#笔记
第一部分 CLR基础 CLR和JIT “运行时”如何解析类型引用 第二部分 设计类型 使用C#的is和as操作符来转型 运行时的相互联系 基元类型.引用类型和值类型 值类型的装箱和拆箱 哈希码 dyn ...
- iis7 500错误日志报 LOG_FILE_MAX_SIZE_TRUNCATE
参考:http://blogs.iis.net/robert_mcmurray/freb-log-file-max-size-truncate cd /d "%windir%\system3 ...
- mysql常用脚本
1.执行拼接字符串sql语句(可传参数) set @dbname='trickraft_14f.'; set @sql=CONCAT('SELECT * FROM ',@dbname,'Armforc ...
- sed 使用 删除匹配行
“p” command prints the buffer (remember to use -n option with “p”) “d” command is just opposite, its ...
- Bootstrap学习的点点滴滴
1)网站:http://www.bootcss.com/http://v3.bootcss.com/getting-started/#downloadhttps://github.com/twbs/b ...
- java小程序 示例
乘法表: package com.test; import org.junit.Test; public class TestSwitch { @Test public void test() { f ...
- Java 线程间通讯(共享变量方式)
Java线程间通讯,最常用的方式便是共享变量方式,多个线程共享一个静态变量就可以实现在线程间通讯,但是这需要注意的就是线程同步问题. 一.没考虑线程同步: package com.wyf; publi ...
- 在点击HOME键时, 在点击icon回到原来的应用。
参考资料: http://www.linuxidc.com/Linux/2012-01/51332.htm