当前位置:网站首页>Select sort (illustration +c code)
Select sort (illustration +c code)
2022-07-07 22:38:00 【One meter eight six brother】
Algorithm principle :
Selection sort is a simple and intuitive sorting algorithm . It works on the principle of :
First, find the smallest in the sequence ( Big ) Elements , Put it at the beginning of the sequence as the sorted sequence ;
then , Continue to find the smallest from the remaining unsorted elements ( Big ) Elements , Put it at the end of the sorted sequence ;
Repeat the above steps , Until all elements are sorted .
One 、 Image simulation Selection sort The process
We choose ten numbers 0~9 As our sort number , And break it up . Then we will arrange them in ascending order . Here's the picture :
The process of sorting is to repeat the steps in the above principle in a cycle , Now let's simulate the image in a round by round cycle .
1、 The first cycle
① Initialize the minimum value
Before formal sorting , Let's initialize first , The task of initialization is to select a minimum value , Of course , It is impossible for the computer to know which is the minimum value before starting the comparison . So we have to assume a , First, use the minimum value of this assumption to compare with each value , Finally, the real minimum value is obtained .
Here we choose the first number 5 As the minimum value of initialization assumption .
② Find the minimum
Now let's take the hypothetical minimum 5 Compare with each value .
When the computer is held 5 Go to the 0 when , comparison ,0 smaller , At this time, the computer will 5 Put back , Take it 0 Then compare with each remaining value .
In fact, you can already see 0 It is the smallest of these ten numbers , But according to the principle of selective sorting , The computer is still required to compare with the rest one by one , That is to say 7 It can be determined that this number is the minimum value . You need to know that there is such a process , Here we don't use images to simulate one by one , Please fill in the picture by yourself .
The next? , Let's go straight to the minimum Put at the end of the sorted sequence . Of course, this is the first cycle , The sorted sequence has not yet been generated .0 It is the beginning number of the sorted sequence .
③ Location switching
When the minimum value is found , We are going to put the minimum value in front of the sequence . In fact, it is location Exchange . This round of position exchange is digital 5 And number 0 The exchange of positions .
After exchanging ,0 Is the sorted sequence , No further operations will be performed on the sorted sequence .
2、 The second cycle
① Initialize the minimum value
The second round of initialization begins , Let's continue to choose the assumed minimum , This time, , We still choose the first number as the assumed minimum , It should be noted that ,0 It is already a sorted sequence , We have to choose the first number from the unordered sequence , That is to say (5、1、8、6、2、3、4、9、7) Numbers in an unordered sequence 5.
② Find the minimum
When 5 Go to the 1 when , comparison 1 Than 5 Small establishment .
take 5 Put back , Take it 1.
When 1 Compare them bit by bit 7 when , This round of searching for the minimum value is completed , determine 1 That's the minimum .
③ Location switching
take 1 Move to sorted sequence (0) At the end of .
After exchanging ,(0,1) It's an ordered sequence ,(5、8、2、3、4、9、7) It's an unordered sequence
3、 The third cycle
① Initialize the minimum value
Select the first number of the unordered sequence 5 As the assumed minimum .
② Find the minimum
When 5 Compare them bit by bit 2 when , Less than established ,5 Put back .
Take it 2, Compare them bit by bit 7 when , determine 2 That's the minimum .
③ Swap places
4、 The fourth cycle
① Initialize the minimum value
② Find the minimum
8 Go to the 6 when , Less than established ,8 Put back .
Take it 6 Compare bit by bit , Go to the 5 Time is less than established ,6 Put back .
Take it 5 Compare bit by bit , Go to the 3 Time is less than established ,5 Put back .
Take it 3 Compare bit by bit , Compared to 7 When determining 3 That's the minimum .
③ Swap places
take 3 Move to sorted sequence (0、1、2) The end of the sequence .
After four cycles , We have sorted the smallest four numbers , Completing the final sorting requires ten rounds of such a cycle . The purpose of each cycle is to find the minimum value , And then at the end of the sorted sequence . There is no repetition of images . You can understand the idea . Next , We enter the code design phase .
Two 、 Program code design (C Language )
Code description :
1、selection_sort Select sorting function for , The formal parameter in its parameter list needs to receive two values :“ Array ” and “ The length of the array ”.
2、 There's a variable in the function min, Subscript used to record the minimum .
3、 There is an external for loop , Used to traverse each element of the array
, external for The loop is nested inside a layer for loop , The inner loop is used to find the minimum , Record minimum subscript .
4、 Exit the inner layer for There are three exchange statements after the loop , Used to move the minimum value to the end of the sorted sequence .
C The language code :
#include<stdio.h>
void selection_sort(int *arr,int len)
{
int min; // The subscript used to store the minimum value
for (int i = 0; i<=len; i++)
{ min = i; // Initialize the minimum value of the selection hypothesis , The first number of the unordered sequence is selected ,
// Here is the subscript
for (int j = i; j<=len; j++)
{
if (arr[j]<arr[min]) // If smaller values are encountered , Then record the minimum subscript
{
min = j;
}
}
// Inner loop exit , The minimum value is determined
// Move the minimum value to the end of the sorted sequence
int tmp = arr[i];
arr[i] = arr[min];
arr[min] = tmp;
}
}
int main()
{
int arr[] = {5,0,1,8,6,2,3,4,9,7};
int len = sizeof(arr) / sizeof(int);
printf(" Value to be sorted :");
for (int i = 0; i <= len - 1; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
printf(" Sorted values :");
selection_sort(arr,len-1);// Call the select sort function
for (int i = 0; i <= len - 1; i++)
{
printf("%d ", arr[i]);
}
}
Running results :
边栏推荐
- Get the week start time and week end time of the current date
- Unity development --- the mouse controls the camera to move, rotate and zoom
- Aspose. Word operation word document (I)
- Revit secondary development - intercept project error / warning pop-up
- 戴森官方直营店免费造型服务现已开放预约 先锋科技诠释护发造型理念,助力消费者解锁多元闪耀造型
- Revit secondary development - get the project file path
- Application practice | the efficiency of the data warehouse system has been comprehensively improved! Data warehouse construction based on Apache Doris in Tongcheng digital Department
- 反爬通杀神器
- Oracle advanced (VI) Oracle expdp/impdp details
- The free styling service of Dyson's official direct store is now open for appointment. Pioneer Technology interprets the styling concept of hair care and helps consumers unlock diversified and shiny s
猜你喜欢
C # realizes the communication between Modbus protocol and PLC
Vs custom template - take the custom class template as an example
如何选择合适的自动化测试工具?
Redis cluster installation
Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb
Robot autonomous exploration DSVP: code parsing
Customer case | China law network, through observing the cloud, greatly shortens the time of fault location
PKPM 2020 software installation package download and installation tutorial
Pdf document signature Guide
Remember aximp once Use of exe tool
随机推荐
Anti climbing killer
How pyGame rotates pictures
The strongest installation of the twin tower model, Google is playing "antique" again?
operator
Kaggle-Titanic
Main functions of OS, Sys and random Standard Libraries
[environment] pycharm sets the tool to convert QRC into py file
C development -- WPF simple animation
#DAYU200体验官#MPPT光伏发电项目 DAYU200、Hi3861、华为云IotDA
怎样写一个增广矩阵到txt文件中
使用 CustomPaint 绘制基本图形
【Azure微服务 Service Fabric 】在SF节点中开启Performance Monitor及设置抓取进程的方式
Micro service remote debug, nocalhost + rainbow micro service development second bullet
How to close eslint related rules
Application practice | the efficiency of the data warehouse system has been comprehensively improved! Data warehouse construction based on Apache Doris in Tongcheng digital Department
微服務遠程Debug,Nocalhost + Rainbond微服務開發第二彈
Unity FAQ (I) lack of references
Dbsync adds support for mongodb and ES
戴森官方直营店免费造型服务现已开放预约 先锋科技诠释护发造型理念,助力消费者解锁多元闪耀造型
UWA Q & a collection