/*****************************************************
*
* 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", ¬Sort[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;
}
插入排序
最新推荐文章于 2023-09-23 17:47:53 发布