惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security Affairs
WordPress大学
WordPress大学
MongoDB | Blog
MongoDB | Blog
A
About on SuperTechFans
F
Fortinet All Blogs
Hacker News: Ask HN
Hacker News: Ask HN
酷 壳 – CoolShell
酷 壳 – CoolShell
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Tenable Blog
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
NISL@THU
NISL@THU
SecWiki News
SecWiki News
Cyberwarzone
Cyberwarzone
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
V2EX - 技术
V2EX - 技术
Simon Willison's Weblog
Simon Willison's Weblog
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
C
Check Point Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
S
Schneier on Security
博客园 - 【当耐特】
雷峰网
雷峰网
月光博客
月光博客
H
Help Net Security
人人都是产品经理
人人都是产品经理
博客园 - 三生石上(FineUI控件)
Google Online Security Blog
Google Online Security Blog
L
LINUX DO - 最新话题
Microsoft Security Blog
Microsoft Security Blog
Know Your Adversary
Know Your Adversary
The GitHub Blog
The GitHub Blog
H
Hacker News: Front Page
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Cisco Talos Blog
Cisco Talos Blog
G
Google Developers Blog
V
Vulnerabilities – Threatpost
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Blog of Author Tim Ferriss

博客园 - searchDM

Elasticsearch 集成 Elasticsearch 环境 Elasticsearch 优化 Elasticsearch入门 Elasticsearch概述 吴恩达自然语言处理第一课:分类与向量空间 Linux下C语言字符串操作之字符串转数值型 lucene 3.4 contrib/facet 切面搜索 在ubuntu上安装全文搜索中文分词Coreseek/sphinx及和Rails集成 solr3.4 高亮(highlight),拼写检查(spellCheck),匹配相似(moreLikeThis) 应用实践 堆与堆排序 Trie树|字典树的简介及实现 快速排序 归并排序的实现 Lucene.net搜索结果排序(单条件和多条件) 冒泡排序 直接插入排序的三种实现 希尔排序的实现 几乎所有食物的英文翻译
直接选择排序及交换二个数据的实现
searchDM · 2011-08-09 · via 博客园 - searchDM

直接选择排序和直接插入排序类似,都将数据分为有序区和无序区,所不同的是直接播放排序是将无序区的第一个元素直接插入到有序区以形成一个更大的有序区,而直接选择排序是从无序区选一个最小的元素直接放到有序区的最后。

设数组为a[0…n-1]

1.      初始时,数组全为无序区为a[0..n-1]。令i=0

2.      在无序区a[i…n-1]中选取一个最小的元素,将其与a[i]交换。交换之后a[0…i]就形成了一个有序区。

3.      i++并重复第二步直到i==n-1。排序完成。

直接选择排序无疑是最容易实现的,下面给出代码:

void Selectsort(int a[], int n)

{

       int i, j, nMinIndex;

       for (i = 0; i < n; i++)

       {

              nMinIndex = i; //找最小元素的位置

              for (j = i + 1; j < n; j++)

                     if (a[j] < a[nMinIndex])

                            nMinIndex = j;

              Swap(a[i], a[nMinIndex]); //将这个元素放到无序区的开头

       }

}

在这里,要特别提醒各位注意下Swap()的实现,建议用:

inline void Swap(int &a, int &b)

{

       int c = a;

       a = b;

       b = c;

}

笔试面试时考不用中间数据交换二个数,很多人给出了

inline void Swap1(int &a, int &b)

{

       a ^= b;

       b ^= a;

       a ^= b;

}

在网上搜索下,也可以找到许多这样的写法。不过这样写存在一个隐患,如果a, b指向的是同一个数,那么调用Swap1()函数会使这个数为0。如:

       int i = 6;

       Swap1(i, i);

       printf("%d\n", i);

当然谁都不会在程序中这样的写代码,但回到我们的Selectsort(),如果a[0]就是最小的数,那么在交换时,将会出现将a[0]0的情况,这种错误相信调试起来也很难发现吧,因此建议大家将交换二数的函数写成:

inline void Swap(int &a, int &b)

{

       int c = a;

       a = b;

       b = c;

}

或者在Swap1()中加个判断,如果二个数据相等就不用交换了:

inline void Swap1(int &a, int &b)

{

       if (a != b)

       {

              a ^= b;

              b ^= a;

              a ^= b;

       }

}