插入排序

/*****************************************************
*
* Main: 插入排序
* Code by: Xiangjie.Huang
* Date: 2014/08/18
* Blog: http://blog.163.com/surgy_han
*
* <----------------Details: start------------>
*
* (1) 将第一待排序序列第一个元素看做一个有序
* 序列, 把第二个元素到最后一个元素当成是
* 未排序序列
*
* (2) 从头到尾依次扫描未排序序列, 将扫描到的
* 每个元素插入有序序列的适当位置. ( 如果
* 待插入的元素与有序序列中的某个元素相等
* , 则将待插入元素插入到相等元素的后面 )
*
* <----------------Details: end-------------->
*
*****************************************************/

//#pragma comment(linker, "/STACK:61400000,61400000")
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;

#define pi 3.1415926535897932385
#define LL64 __int64
#define LL long long
#define oo 2147483647
#define N 1010
#define M 210
#define INF 1e9

int notSort[N];

struct insert_sort
{
struct insert_sort *next;
int value;
};
struct insert_sort *node, *p, *q;

void InputData(int n)
{
for (int i = 0; i < n; i++)
{
scanf("%d", &notSort[i]);
}
}

void init()
{
node = (insert_sort *)malloc(sizeof(insert_sort));
node->next = NULL;
}

void initQ(int i)
{
q = (insert_sort *)malloc(sizeof(insert_sort));
q->next = NULL;
q->value = notSort[i];
}

void Output()
{
p = node->next;
while (p != NULL)
{
printf("%d ", p->value);
p = p->next;
}
printf("\n");
}

void Find_Location()
{
p = node;
while (p != NULL)
{
if (p->next == NULL)
{
p->next = q;
break;
}

if (p->next->value > q->value)
{
q->next = p->next;
p->next = q;
break;
}

p = p->next;
}
}

void Sort_of_Insert(int n)
{
InputData(n);
init();

for (int i = 0; i < n; i++)
{
initQ(i);
Find_Location();
}

Output();
}

int main()
{
int n;

//freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
while (~scanf("%d", &n))
{
Sort_of_Insert(n);
}

return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值