当前位置:网站首页>选择排序
选择排序
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
边栏推荐
- AI landing manufacturing: intelligent robots should have these four abilities
- DDoS threat situation gets worse
- 记录生产的一次OOM异常
- widget使用setImageViewBitmap方法设置bug分析
- [protection mode] segment descriptor
- scp远程拷贝命令记录
- Understand AQS principle (flow chart and synchronous queue diagram)
- Looking for thesaurus data [closed]
- Illustration Google V8 19: asynchronous programming (II): how does V8 implement async/await?
- DDoS threat situation gets worse
猜你喜欢

DMX configuration
![[MySQL 06] backup and restore MySQL database in Linux + docker container environment](/img/4e/8662d15ff84b2436d02948019540d3.png)
[MySQL 06] backup and restore MySQL database in Linux + docker container environment

012_ switch

Implementation of a simple camera based on pyqt5

005_ button

(4) Blender source code analysis flash window display process

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

图解 Google V8 # 19 :异步编程(二):V8 是如何实现 async/await 的?

一种跳板机的实现思路
![[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
随机推荐
DHU programming exercise
AI landing manufacturing: intelligent robots should have these four abilities
Thinking carefully and fearfully: a software can be transmitted online to monitor whether employees want to "run away"
018_ rate
当大学毕业感到迷茫怎么办?
dhu编程练习
Share the source code of the website of graduation student record
If you want to install a set of monitoring, what is the process? How much is it?
9 — 正则校验集合
Copy entire directory to output folder maintain folder structure- Copy entire directory to output folder maintaining the folder structure?
图解 Google V8 # 19 :异步编程(二):V8 是如何实现 async/await 的?
Comprendre le principe AQS (organigramme et schéma de file d'attente synchrone)
如何制作CSR(Certificate Signing Request)文件?
UE5的蓝图节点拷贝到UE4后连线和属性值全部丢失了
scp远程拷贝命令记录
Share the source code of the website of graduation student record
Tools and life services
【自然语言处理】【多模态】OFA:通过简单的sequence-to-sequence学习框架统一架构、任务和模态
What are the payment and distribution systems?
DHU programming exercise