当前位置:网站首页>Select sort
Select sort
2022-06-30 02:17:00 【Life needs depth】
Summary
This chapter introduces the selective sorting in the sorting algorithm .
Catalog
1. Introduction to selection sorting
2. Select Sorting text description
3. Time complexity and stability of selective sorting
4. Choose Sort implementation
4.1 Selection sort C Realization
4.2 Selection sort C++ Realization
4.3 Selection sort Java Realization
Reprint please indicate the source : Selection sort - If the sky doesn't die - Blog Garden
More : Data structure and algorithm series Catalog
Introduction to selection sorting
Selection sort (Selection sort) It is a simple and intuitive sorting algorithm .
The basic idea is this : First, find the smallest... In the unordered sequence (or Maximum ) Elements , Then store it at the beginning of the sequence ; next , Then continue to find the smallest element from the remaining unordered elements (or Maximum ) Elements , And then at the end of the sorted sequence . And so on , Until all the elements are sorted .
Select Sorting text description
Select sort code
/*
* Selection sort
*
* Parameter description :
* a -- Array to sort
* n -- Length of array
*/
void select_sort(int a[], int n)
{
int i; // The end of the ordered area
int j; // The starting position of the disorder area
int min; // The position of the smallest element in the disordered region
for(i=0; i<n; i++)
{
min=i;
// find "a[i+1] ... a[n]" The smallest element between , And assign it to min.
for(j=i+1; j<n; j++)
{
if(a[j] < a[min])
min=j;
}
// if min!=i, The exchange a[i] and a[min].
// After the exchange , To ensure the a[0] ... a[i] The elements between are ordered .
if(min != i)
swap(a[i], a[min]);
}
}The following is a sequence of numbers {20,40,30,10,60,50} For example , Demonstrate its selection sorting process ( Here's the picture ).

Sorting process
The first 1 Trip to :i=0. find a[1...5] Minimum of a[3]=10, And then a[0] and a[3] swap . Sequence change :20,40,30,10,60,50 -- > 10,40,30,20,60,50
The first 2 Trip to :i=1. find a[2...5] Minimum of a[3]=20, And then a[1] and a[3] swap . Sequence change :10,40,30,20,60,50 -- > 10,20,30,40,60,50
The first 3 Trip to :i=2. find a[3...5] Minimum of , Because the minimum value is greater than a[2], This trip doesn't do anything .
The first 4 Trip to :i=3. find a[4...5] Minimum of , Because the minimum value is greater than a[3], This trip doesn't do anything .
The first 5 Trip to :i=4. In exchange for a[4] and a[5] The data of . Sequence change :10,20,30,40,60,50 -- > 10,20,30,40,50,60
Time complexity and stability of selective sorting
Select sorting time complexity
The time complexity of sorting is O(N2).
Let's assume that the sequence being sorted has N Number . The time complexity of traversing a trip is O(N), How many times does it take to traverse ?N-1! therefore , The time complexity of sorting is O(N2).
Select sort stability
Selective sorting is a stable algorithm , It satisfies the definition of stable algorithm .
Algorithm stability -- Let's assume that there exists in a sequence a[i]=a[j], If before sorting ,a[i] stay a[j] front ; And after sorting ,a[i] Still in a[j] front . Then the sorting algorithm is stable !
Choose Sort implementation
Selection sort C Realization
Implementation code (select_sort.c)
/**
* Selection sort :C Language
*
* @author skywang
* @date 2014/03/11
*/
#include <stdio.h>
// The length of the array
#define LENGTH(array) ( (sizeof(array)) / (sizeof(array[0])) )
#define swap(a,b) (a^=b,b^=a,a^=b)
/*
* Selection sort
*
* Parameter description :
* a -- Array to sort
* n -- Length of array
*/
void select_sort(int a[], int n)
{
int i; // The end of the ordered area
int j; // The starting position of the disorder area
int min; // The position of the smallest element in the disordered region
for(i=0; i<n; i++)
{
min=i;
// find "a[i+1] ... a[n]" The smallest element between , And assign it to min.
for(j=i+1; j<n; j++)
{
if(a[j] < a[min])
min=j;
}
// if min!=i, The exchange a[i] and a[min].
// After the exchange , To ensure the a[0] ... a[i] The elements between are ordered .
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");
} Selection sort C++ Realization
Implementation code (SelectSort.cpp)
/**
* Selection sort :C++
*
* @author skywang
* @date 2014/03/11
*/
#include <iostream>
using namespace std;
/*
* Selection sort
*
* Parameter description :
* a -- Array to sort
* n -- Length of array
*/
void selectSort(int* a, int n)
{
int i; // The end of the ordered area
int j; // The starting position of the disorder area
int min; // The position of the smallest element in the disordered region
for(i=0; i<n; i++)
{
min=i;
// find "a[i+1] ... a[n]" The smallest element between , And assign it to min.
for(j=i+1; j<n; j++)
{
if(a[j] < a[min])
min=j;
}
// if min!=i, The exchange a[i] and a[min].
// After the exchange , To ensure the a[0] ... a[i] The elements between are ordered .
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;
} Selection sort Java Realization
Implementation code (SelectSort.java)
/**
* Selection sort :Java
*
* @author skywang
* @date 2014/03/11
*/
public class SelectSort {
/*
* Selection sort
*
* Parameter description :
* a -- Array to sort
* n -- Length of array
*/
public static void selectSort(int[] a, int n) {
int i; // The end of the ordered area
int j; // The starting position of the disorder area
int min; // The position of the smallest element in the disordered region
for(i=0; i<n; i++) {
min=i;
// find "a[i+1] ... a[n]" The smallest element between , And assign it to min.
for(j=i+1; j<n; j++) {
if(a[j] < a[min])
min=j;
}
// if min!=i, The exchange a[i] and a[min].
// After the exchange , To ensure the a[0] ... a[i] The elements between are ordered .
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");
}
}above 3 The principle and output of the two implementations are the same . Here is their output :
before sort:20 40 30 10 60 50 after sort:10 20 30 40 50 60
边栏推荐
- 图解 Google V8 # 19 :异步编程(二):V8 是如何实现 async/await 的?
- After the blueprint node of ue5 is copied to UE4, all connections and attribute values are lost
- What problems can cloud storage architecture solve for Devops?
- ROS Bridge 笔记(01)— apt 安装、源码编译安装、安装依赖、运行显示
- 網上炒股安全麼?炒股需要開戶嗎?
- Jenkins continuous integration environment build 8 (configure mailbox server to send build results)
- Method of converting songs from DTS to MP3
- How does payment splitting help B2B bulk commodity transactions?
- Vs realize quick replacement function
- Global communication infrastructure faces apt, robotics and DDoS; The weakest mobile network
猜你喜欢

Day_ 19 multithreading Basics
![[protection mode] segment descriptor](/img/23/19b12c496da437fbf03829b7b4e3b8.jpg)
[protection mode] segment descriptor

SQL injection -day17

CTF introductory learning (WEB direction)

DMX configuration

Using face_ Recognition library reports an error reason: cudnn_ STATUS_ NOT_ SUPPORTED
![[Galaxy Kirin V10] [desktop] Firefox browser settings home page does not take effect](/img/96/e3afb2b9683cce7645afd483cc0844.png)
[Galaxy Kirin V10] [desktop] Firefox browser settings home page does not take effect

选购通配符SSL证书注意事项

堆排序

004_ icon
随机推荐
013_ slider
Using grpcui to test asp Net core grpc service
Le Code autojs peut - il être chiffré? Oui, présentation des techniques de chiffrement autojs
DDoS threat situation gets worse
7 — filter
Using face_ Recognition library reports an error reason: cudnn_ STATUS_ NOT_ SUPPORTED
The birth of the cheapswap protocol
[naturallanguageprocessing] [multimodality] ofa: unified architecture, tasks and modes through a simple sequence to sequence learning framework
Bucket sort
AutoJS代碼能加密嗎?YES,AutoJS加密技巧展示
26. common interview questions of algorithm
DHU programming exercise
The largest DDoS attack ever peaked at 400 Gbps
【MySQL 06】linux + Docker容器环境中备份和还原MySQL数据库
Is online stock trading safe? Do you need to open an account for stock trading?
CTF入门学习(Web方向)
Knowledge payment cannot escape the essence of "anxiety"
封装一个完整版的uniapp图片和视频上传组件,拿来即用,可进行图片视频切换,可自定义上传按钮样式,删除按钮样式,可单独上传图片或者视频,可限制上传数量
Recommendations for agileplm database parameter optimization
Tencent released the first Office Photo 23 years ago. It's so chronological