当前位置:网站首页>选择排序
选择排序
2022-06-30 02:13:00 【生活需要深度】
概要
本章介绍排序算法中的选择排序。
目录
1. 选择排序介绍
2. 选择排序图文说明
3. 选择排序的时间复杂度和稳定性
4. 选择排序实现
4.1 选择排序C实现
4.2 选择排序C++实现
4.3 选择排序Java实现
转载请注明出处:选择排序 - 如果天空不死 - 博客园
更多内容:数据结构与算法系列 目录
选择排序介绍
选择排序(Selection sort)是一种简单直观的排序算法。
它的基本思想是:首先在未排序的数列中找到最小(or最大)元素,然后将其存放到数列的起始位置;接着,再从剩余未排序的元素中继续寻找最小(or最大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。
选择排序图文说明
选择排序代码
/*
* 选择排序
*
* 参数说明:
* a -- 待排序的数组
* n -- 数组的长度
*/
void select_sort(int a[], int n)
{
int i; // 有序区的末尾位置
int j; // 无序区的起始位置
int min; // 无序区中最小元素位置
for(i=0; i<n; i++)
{
min=i;
// 找出"a[i+1] ... a[n]"之间的最小元素,并赋值给min。
for(j=i+1; j<n; j++)
{
if(a[j] < a[min])
min=j;
}
// 若min!=i,则交换 a[i] 和 a[min]。
// 交换之后,保证了a[0] ... a[i] 之间的元素是有序的。
if(min != i)
swap(a[i], a[min]);
}
}下面以数列{20,40,30,10,60,50}为例,演示它的选择排序过程(如下图)。

排序流程
第1趟:i=0。找出a[1...5]中的最小值a[3]=10,然后将a[0]和a[3]互换。 数列变化:20,40,30,10,60,50 -- > 10,40,30,20,60,50
第2趟:i=1。找出a[2...5]中的最小值a[3]=20,然后将a[1]和a[3]互换。 数列变化:10,40,30,20,60,50 -- > 10,20,30,40,60,50
第3趟:i=2。找出a[3...5]中的最小值,由于该最小值大于a[2],该趟不做任何处理。
第4趟:i=3。找出a[4...5]中的最小值,由于该最小值大于a[3],该趟不做任何处理。
第5趟:i=4。交换a[4]和a[5]的数据。 数列变化:10,20,30,40,60,50 -- > 10,20,30,40,50,60
选择排序的时间复杂度和稳定性
选择排序时间复杂度
选择排序的时间复杂度是O(N2)。
假设被排序的数列中有N个数。遍历一趟的时间复杂度是O(N),需要遍历多少次呢?N-1!因此,选择排序的时间复杂度是O(N2)。
选择排序稳定性
选择排序是稳定的算法,它满足稳定算法的定义。
算法稳定性 -- 假设在数列中存在a[i]=a[j],若在排序之前,a[i]在a[j]前面;并且排序之后,a[i]仍然在a[j]前面。则这个排序算法是稳定的!
选择排序实现
/**
* 选择排序:C 语言
*
* @author skywang
* @date 2014/03/11
*/
#include <stdio.h>
// 数组长度
#define LENGTH(array) ( (sizeof(array)) / (sizeof(array[0])) )
#define swap(a,b) (a^=b,b^=a,a^=b)
/*
* 选择排序
*
* 参数说明:
* a -- 待排序的数组
* n -- 数组的长度
*/
void select_sort(int a[], int n)
{
int i; // 有序区的末尾位置
int j; // 无序区的起始位置
int min; // 无序区中最小元素位置
for(i=0; i<n; i++)
{
min=i;
// 找出"a[i+1] ... a[n]"之间的最小元素,并赋值给min。
for(j=i+1; j<n; j++)
{
if(a[j] < a[min])
min=j;
}
// 若min!=i,则交换 a[i] 和 a[min]。
// 交换之后,保证了a[0] ... a[i] 之间的元素是有序的。
if(min != i)
swap(a[i], a[min]);
}
}
void main()
{
int i;
int a[] = {20,40,30,10,60,50};
int ilen = LENGTH(a);
printf("before sort:");
for (i=0; i<ilen; i++)
printf("%d ", a[i]);
printf("\n");
select_sort(a, ilen);
printf("after sort:");
for (i=0; i<ilen; i++)
printf("%d ", a[i]);
printf("\n");
}选择排序C++实现
实现代码(SelectSort.cpp)
/**
* 选择排序:C++
*
* @author skywang
* @date 2014/03/11
*/
#include <iostream>
using namespace std;
/*
* 选择排序
*
* 参数说明:
* a -- 待排序的数组
* n -- 数组的长度
*/
void selectSort(int* a, int n)
{
int i; // 有序区的末尾位置
int j; // 无序区的起始位置
int min; // 无序区中最小元素位置
for(i=0; i<n; i++)
{
min=i;
// 找出"a[i+1] ... a[n]"之间的最小元素,并赋值给min。
for(j=i+1; j<n; j++)
{
if(a[j] < a[min])
min=j;
}
// 若min!=i,则交换 a[i] 和 a[min]。
// 交换之后,保证了a[0] ... a[i] 之间的元素是有序的。
if(min != i)
{
int tmp = a[i];
a[i] = a[min];
a[min] = tmp;
}
}
}
int main()
{
int i;
int a[] = {20,40,30,10,60,50};
int ilen = (sizeof(a)) / (sizeof(a[0]));
cout << "before sort:";
for (i=0; i<ilen; i++)
cout << a[i] << " ";
cout << endl;
selectSort(a, ilen);
cout << "after sort:";
for (i=0; i<ilen; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}选择排序Java实现
实现代码(SelectSort.java)
/**
* 选择排序:Java
*
* @author skywang
* @date 2014/03/11
*/
public class SelectSort {
/*
* 选择排序
*
* 参数说明:
* a -- 待排序的数组
* n -- 数组的长度
*/
public static void selectSort(int[] a, int n) {
int i; // 有序区的末尾位置
int j; // 无序区的起始位置
int min; // 无序区中最小元素位置
for(i=0; i<n; i++) {
min=i;
// 找出"a[i+1] ... a[n]"之间的最小元素,并赋值给min。
for(j=i+1; j<n; j++) {
if(a[j] < a[min])
min=j;
}
// 若min!=i,则交换 a[i] 和 a[min]。
// 交换之后,保证了a[0] ... a[i] 之间的元素是有序的。
if(min != i) {
int tmp = a[i];
a[i] = a[min];
a[min] = tmp;
}
}
}
public static void main(String[] args) {
int i;
int[] a = {20,40,30,10,60,50};
System.out.printf("before sort:");
for (i=0; i<a.length; i++)
System.out.printf("%d ", a[i]);
System.out.printf("\n");
selectSort(a, a.length);
System.out.printf("after sort:");
for (i=0; i<a.length; i++)
System.out.printf("%d ", a[i]);
System.out.printf("\n");
}
}上面3种实现的原理和输出结果都是一样的。下面是它们的输出结果:
before sort:20 40 30 10 60 50 after sort:10 20 30 40 50 60
边栏推荐
- 006_ radio
- 007_ checkbox
- Is the processor the main factor in buying a mobile phone?
- SCP remote copy command record
- DTW学习(dynamic time warping)——思想、代码实现
- DDoS threat situation gets worse
- [MySQL 06] backup and restore MySQL database in Linux + docker container environment
- Global communication infrastructure faces apt, robotics and DDoS; The weakest mobile network
- AI落地制造业:智能机器人应具备这4种能力
- 搞透AQS原理(流程圖及同步隊列圖解)
猜你喜欢
![[MySQL 04] sauvegarde et restauration de la base de données MySQL sous Linux en utilisant MySQL Workbench 8.0 ce](/img/e7/fc2925a10ac5fb370dd221c3f4a46a.png)
[MySQL 04] sauvegarde et restauration de la base de données MySQL sous Linux en utilisant MySQL Workbench 8.0 ce

widget使用setImageViewBitmap方法设置bug分析
![[graph neural network] summary of graph classification study [3]: evaluation of graph classification methods and future research directions](/img/b1/2afa73a14b2f41b7a65c4c2d261e6a.png)
[graph neural network] summary of graph classification study [3]: evaluation of graph classification methods and future research directions

想转行,但不知道自己要做什么工作比较好?

Mobaihe cm201-2-ch-hi3798mv300-300h-emmc and NAND_ Infrared Bluetooth voice_ Brush firmware package

Scala基础【入门及安装】

Thinking carefully and fearfully: a software can be transmitted online to monitor whether employees want to "run away"

Share the source code of the website of graduation student record

33Mysql

8 — router
随机推荐
假离婚变成真离婚,财产怎么办
如何制作CSR(Certificate Signing Request)文件?
Widget uses setimageviewbitmap method to set bug analysis
DDoS attacks and destructive ripple effects against online gamers
云存储架构能解决 DevOps 的什么问题?
005_ button
004_ icon
CTF入门学习(Web方向)
DHU programming exercise
搞透AQS原理(流程圖及同步隊列圖解)
Implement vs to run only one source file at a time
dhu编程练习
Knowledge payment cannot escape the essence of "anxiety"
Add a second network card (network interface NIC) for the virtual machine in azure portal in 2 minutes
DDoS "fire drill" service urges companies to prepare
Copy entire directory to output folder maintain folder structure- Copy entire directory to output folder maintaining the folder structure?
006_ radio
7 — filter
Restore a 35k-55k Tencent Android Senior Engineer Interview
【自然语言处理】【多模态】OFA:通过简单的sequence-to-sequence学习框架统一架构、任务和模态