Get the App
SLTechnology News&Howtos  ›  Network Security  › 

C speech selection sorting algorithm and code

Shulou Source: shulou.com Published: 2022-06-01 00:41:21 10月01日 Update

Selective sorting is one of the sorting algorithms. Here we take sorting from small to large as an example to stop the explanation.

Fundamental ideas and examples

The basic idea of choosing order (from small to big) is, first, choose the smallest number and put it in the first place; then, choose the second smallest number and put it in the second position; and so on, until everything is sorted from small to large.

In terms of completion, we usually confirm the status of the smallest number of locations, and then stop communicating with the number I.

Next, the sorting process is illustrated by sorting the stop selection of 3-2-4-1, and the status of the smallest number of locations in the future is recorded using min_index.

The first round of sorting process (searching for the status of the first small number of locations)

3 2 4 1 (finally, min_index=1)

3 2 4 1 (3 > 2, so min_index=2)

3 2 4 1 (2

< 4, 所以 min_index=2) 3 2 4 1(2 >

1, so min_index=4, this time affirms that the first small number is in the position 4)

1 2 4 3 (first round consequence, communication between 3 and 1, that is, communication between status 1 and status 4)

The second round of sorting process (looking for the status of the second smallest number of locations)

1 2 4 3 (first round consequence, min_index=2, only need to start from position 2)

1 2 4 3 (4 > 2, so min_index=2)

1 2 4 3 (3 > 2, so min_index=2)

1 2 4 3 (second round consequence, as min_index happens to be in the second position, no communication is required)

The third round of sorting process (looking for the status of the third smallest number of locations)

1 2 4 3 (second round consequence, min_index=3, only need to start with status 2)

1 2 4 3 (4 > 3, so min_index=4)

1 2 3 4 (the third round of consequences, 3 and 4 exchanges, that is, status 4 and status 3 exchanges)

At this point, the ranking is over.

Summary and completion

Select sort stops sorting on the unordered array R [N] of size N, and stops the NMUI 1-round selection process. The I round pulls out the I lowest number and puts it in the I position. When the N-1 is completed, the number of the N-th (that is, the largest) is naturally in the initial position.

The C language completion of the selection sort is given below.

# include # include # define N 8 void select_sort (int a [], int n); / / Select the number of elements that finish sorting void select_sort (int a [], int n) / / n is the number of elements of the array a {/ / stop the first round of selection for (int item0; I)

Tags: Sorting status selection communication location consequences process from small to large small algorithm that is minimum number just sequence start number array fundamental order Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Microsoft Huawei Shulou Technology OPPO Reno