选择排序

来源:互联网 发布:mac win10 鼠标滚轮 编辑:程序博客网 时间:2024/06/04 18:08
/***************************************************** 
*
*Main: 选择排序
*Code by: Xiangjie.Huang
*Date: 2014/08/18
*Blog: http://blog.163.com/surgy_han
*
*<----------------Details: start------------>
*
*(1) 首先在未排序序列中找到最小( 大 )元素,
*存放到排序序列的起始位置
*
*
*(2) 再从剩余未排序元素中继续寻找最小( 大 )
*元素, 然后放到已排序序列的末尾
*
*(3) 重复第二步, 直到所有元素均排序完毕
*
*<----------------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];
int Sorted[N];
bool isUsed[N];

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

void Output(int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", Sorted[i]);
}
printf("\n");
}

void Sort_of_Selection(int n)
{
InputData(n);
memset(isUsed, 0, sizeof(isUsed));

for (int i = 0; i < n; i++)
{
int _min = oo;
int mark;
for (int j = 0; j < n; j++)
{
if (isUsed[j] == true) continue;
if (notSort[j] < _min)
{
_min = notSort[j];
mark = j;
}
}
Sorted[i] = _min;
isUsed[mark] = true;
}

Output(n);
}

int main()
{
int n;

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

return 0;
}


0 0
原创粉丝点击