insertion sort
1.insertion sort
#include <stdio.h>
#include <time.h>
#include <stdlib.h> #define N 20 void main()
{
int i,j,temp, a[N]; srand((unsigned)time(0));
puts("this is the 20 numbers,");
for( i = 0; i < N; i++)
{
a[i] = rand()%100;
printf("%4d\t",a[i]); }
puts("this is the end"); for( i = 1; i < N; i++)
{ temp = a[i];
for( j = i-1; j >= 0; j--)
{
if(temp < a[j])
{
a[j+1] = a[j];
}
else break;
}
//这里为什么没有放在break上面,是因为当temp要放在第一位的时候,这一句不一定被执行
a[j+1]=temp; } for(i = 0; i < N; i++)
printf("%4d\t",a[i]);
}
insertion sort的更多相关文章
- [LeetCode] Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...
- 经典排序算法 – 插入排序Insertion sort
经典排序算法 – 插入排序Insertion sort 插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕. 插入排序方法分直接插入排序和折半插入排序两种, ...
- leetcode Insertion Sort List
题目:Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct Li ...
- 【leetcode】Insertion Sort List (middle)
Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
- 9. Sort List && Insertion Sort List (链表排序总结)
Sort List Sort a linked list in O(n log n) time using constant space complexity. H ...
- LeetCode OJ 147. Insertion Sort List
Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...
- Java for LeetCode 147 Insertion Sort List
Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...
- 【LeetCode OJ】Insertion Sort List
Problem: Sort a linked list using insertion sort. The node of the linked list is defined as: /** * D ...
- 147. Insertion Sort List
Sort a linked list using insertion sort. 代码如下: /** * Definition for singly-linked list. * public cla ...
- leetcode 147. Insertion Sort List ----- java
Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...
随机推荐
- metadata lock
1 锁等待的例子 session 1: 执行查询但不提交 mysql> begin; mysql> select * from test where c2 = '1'; session 2 ...
- excel 经验总结
1.2007版excel表格中怎么将使用字母+数字下拉排序 比如:A201110300001怎么在excel表格中往下拉的时候变成A201110300002.A201110300003…… 方法: 因 ...
- TestNG传参的几种方式
1. 通过parameter传参 java代码部分: import org.testng.annotations.Parameters; import org.testng.annotations.T ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 get_selected_list_values(self, locator)
def get_selected_list_values(self, locator): """Returns the values of selected elemen ...
- bzoj 1408 [Noi2002]Robot(欧拉函数)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1408 [题意] 求m的所有约数中,满足可以分解成(奇数个不同素数/偶数个不同素数/其 ...
- [Hive - Tutorial] Querying and Inserting Data 查询和插入数据
Querying and Inserting Data Simple Query Partition Based Query Joins Aggregations Multi Table/File I ...
- spring-boot系列:初试spring-boot
部署父工程 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http: ...
- Codeforces 364
A 第一题明显统计,注意0和long long(我WA,RE好几次) /* * Problem: A. Matrix * Author: Shun Yao */ #include <string ...
- centos6.4 安装erlang
erlang官网: http://www.erlang.org 下载程序去年:
- 第二百九十七天 how can I 坚持
算是在家宅了一天吧,下午睡了会觉,晚上一起做了个饭,中午一起吃的炒菜和徐斌他同学. 还是那么冷啊. 整天都是东扯西扯的. 睡觉. 忘了件重要的事,就是今天第一次喝鸡尾酒,还有,常人之所以是常人,不是因 ...