Select sorting c implementation in Linux

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.