码迷,mamicode.com
首页 > 编程语言 > 详细

选择排序【代码】

时间:2017-10-28 14:34:49      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:ace   http   程序   engine   arch   form   多少   sig   交换   

思路参考:

http://www.cnblogs.com/kkun/archive/2011/11/23/2260281.html 

代码如下:

 

 1 // 171028选择排序.cpp: 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include <random>
 7 
 8 using namespace std;
 9 
10 void selection_sort(unsigned int a[],int len)//按升序排列
11 {
12     int k = 0;
13     int swap = 0;
14     for (int i = 0; i < len - 1; i++)//多少趟
15     {
16         k = i;
17         for (int j = i + 1; j < len; j++)//每次选出最小的数
18         {
19             k = (a[k] > a[j]) ? j : k;//如果按降序排列,这里改成a[k] < a[j]
20         }
21         //交换
22         swap = a[i];
23         a[i] = a[k];
24         a[k] = swap;
25     }
26     cout << "排序之后的数组序列: ";
27     for (int i = 0; i < len; i++)
28     {
29         cout << a[i] << ends;
30     }
31     cout << endl;
32 }
33 
34 int main()
35 {
36     /*default_random_engine e;
37     uniform_int_distribution<unsigned> u(0, 100);*/
38     unsigned int test[9] = { 1,4,6,9,7,2,8,5,3 };
39     /*for (int i = 0; i < 15; i++)
40     {
41         test[i] = u(e);
42     }*/
43     cout << "排序之前的数组序列: ";
44     for (auto c : test)
45         cout << c << ends;
46     cout << endl;
47     selection_sort(test, sizeof(test) / sizeof(test[0]));
48     return 0;
49 }

 

选择排序【代码】

标签:ace   http   程序   engine   arch   form   多少   sig   交换   

原文地址:http://www.cnblogs.com/journal-of-xjx/p/7746616.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!