Select "sort" to divide the sequence to be sorted into two types: sorted sequence and unsorted sequence. Select the smallest element from the sequence that has never been sorted, and store it at the end of the sequence until all elements are sorted. The key code is as follows:
1. Select the sorting header file: selectSort. h
[Cpp]
# Ifndef SELECTSORT_H
# Define SELECTSORT_H
Extern void selectSort (int * pArr, const int length );
# Endif
# Ifndef SELECTSORT_H
# Define SELECTSORT_H www.2cto.com
Extern void selectSort (int * pArr, const int length );
# Endif
2. Select the sorting source file: selectSort. c
[Cpp]
# Include "selectSort. h"
Void selectSort (int * pArr, const int length)
{
Int I, j, min, tmp;
For (I = 0; I <length-1; I ++)
{
Min = I;
For (j = I + 1; j <length; j ++)
{
If (* (pArr + min)> * (pArr + j ))
{
Min = j;
}
}
If (min! = I)
{
Tmp = * (pArr + I );
* (PArr + I) = * (pArr + min );
* (PArr + min) = tmp;
}
}
}
# Include "selectSort. h"
Void selectSort (int * pArr, const int length)
{
Int I, j, min, tmp;
For (I = 0; I <length-1; I ++)
{
Min = I;
For (j = I + 1; j <length; j ++)
{
If (* (pArr + min)> * (pArr + j ))
{
Min = j;
}
}
If (min! = I)
{
Tmp = * (pArr + I );
* (PArr + I) = * (pArr + min );
* (PArr + min) = tmp;
}
}
}
3. main header file: main. h
[Cpp]
# Ifndef MAIN_H
# Define MAIN_H
# Include <stdio. h>
# Include "selectSort. h"
Int main (void );
Void showArr (const int * pArr, const int length );
Void initRandomArr (int * pArr, const int length );
# Endif
# Ifndef MAIN_H
# Define MAIN_H
# Include <stdio. h>
# Include "selectSort. h"
Int main (void );
Void showArr (const int * pArr, const int length );
Void initRandomArr (int * pArr, const int length );
# Endif
4. main source file: main. c
[Cpp]
# Include "main. h"
Int main (void)
{
Printf ("Input array length: \ n ");
Int length;
Scanf ("% d", & length );
If (length <0)
{
Printf ("Array length must be larger 0 \ n ");
Return 1;
}
Int arr [length];
InitRandomArr (arr, length );
Printf ("Get random array: \ n ");
ShowArr (arr, length );
SelectSort (arr, length );
Printf ("select sort result: \ n ");
ShowArr (arr, length );
Return 0;
}
Void showArr (const int * pArr, const int length)
{
Int I;
For (I = 0; I <length; I ++)
{
Printf ("% d", * (pArr + I ));
}
Printf ("\ n ");
}
Void initRandomArr (int * pArr, const int length)
{
Int I;
Srand (time (NULL ));
For (I = 0; I <length; I ++)
{
* (PArr + I) = rand () % 1000;
}
}
# Include "main. h"
Int main (void)
{
Printf ("Input array length: \ n ");
Int length;
Scanf ("% d", & length );
If (length <0)
{
Printf ("Array length must be larger 0 \ n ");
Return 1;
}
Int arr [length];
InitRandomArr (arr, length );
Printf ("Get random array: \ n ");
ShowArr (arr, length );
SelectSort (arr, length );
Printf ("select sort result: \ n ");
ShowArr (arr, length );
Return 0;
}
Void showArr (const int * pArr, const int length)
{
Int I;
For (I = 0; I <length; I ++)
{
Printf ("% d", * (pArr + I ));
}
Printf ("\ n ");
}
Void initRandomArr (int * pArr, const int length)
{
Int I;
Srand (time (NULL ));
For (I = 0; I <length; I ++)
{
* (PArr + I) = rand () % 1000;
}
}
5. Compile the program:
[Cpp]
[Root @ localhost selectSort] $ gcc-c main. c
[Root @ localhost selectSort] $ gcc-c selectSort. c
[Root @ localhost selectSort] $ gcc-o main. o selectSort. o
[Root @ localhost selectSort] $ gcc-c main. c
[Root @ localhost selectSort] $ gcc-c selectSort. c
[Root @ localhost selectSort] $ gcc-o main. o selectSort. o
Execute the executable file main, which is roughly as follows:
[Cpp]
[Root @ localhost selectSort] $./main
Input array length:
8
Get random array:
906 968 161 208 757 885 370
Select sort result:
161 208 370 691 757 885 906
[Root @ localhost selectSort] $./main
Input array length:
8
Get random array:
906 968 161 208 757 885 370
Select sort result:
161 208 370 691 757 885 906
Select the sort time complexity (n²) and the number of exchanges (O (n). If the order is already ordered, 0 is exchanged. If the reverse order is the worst, N-1 is exchanged. The number of swapping times is much less than that of Bubble sorting. Because the CPU time required for switching is much longer than the CPU time required by the comparison, and the n value is small, the selection sorting is faster than Bubble sorting.