Select sorting method

Source: Internet
Author: User

 

// Select sort by file. cpp

// The basic idea of sorting:
// Assume that N pieces of data are placed in array A. Sort the N numbers in ascending order.
// First, within the range from a [0] to a [n-1], the small value element is selected to exchange with a [0;
// Then, within the range from a [1] to a [n-1], the element with the smallest value is selected for exchange with a [1;
// Perform the following operations in sequence. The sorting can be completed after n-1 selections.

# Include "iostream. H"

Void xuanze_sort (int A [], int Len)
{
Cout <"/n is selecting sorting:/N ";

For (INT I = 0; I <len-1; I ++) {// len-1 selection is required for both the best and worst cases
 
Int K = I; // prepare to select the element at position I. K is initially set to I

For (Int J = I + 1; j <Len; j ++)
If (A [k]> A [J])

K = J; // K indicates the position of the element with the current minimum value,
// After the for ends, K indicates the subscript from a [I] to a [len-1] minimum element
 
If (K! = I) {// you need to exchange a [I] And a [k]; maybe K = I, meaning: the original A [I]
Int temp = A [I]; // It is the minimum value between a [I] to a [len-1], K is not modified,
// Keep its initialization value I
A [I] = A [k];
A [k] = temp;
}

// Output the sorting result after this selection. Use the "|" sign as the separator between the sorted element and the unsorted sorted element.

For (INT m = 0; m <Len; m ++ ){
If (M = I)
Cout <A [m] <"| ";
Else
Cout <A [m] <"";
}

Cout <Endl;

} // End of

 
}

Void main (void)
{
Int B [] = {9, 8,-, 5,-, 0 };
Int lenofb = sizeof (B)/sizeof (INT );
 
Cout <"before sort:/N ";

For (INT I = 0; I <lenofb; I ++)
Cout <B [I] <"";

Xuanze_sort (B, lenofb );
 
Cout <"/nafter sort:/N ";
For (I = 0; I <lenofb; I ++)
Cout <B [I] <"";

Cout <Endl;

}

 

 

 

// File select the sorting deformation 1.cpp

// The basic idea of sorting:
// Assume that N pieces of data are placed in array A. Sort the N numbers in ascending order.
// First, within the range from a [0] to a [n-1], the small value element is selected to exchange with a [0;
// Then, within the range from a [1] to a [n-1], the element with the smallest value is selected for exchange with a [1;
// Perform the following operations in sequence. The sorting can be completed after n-1 selections.

# Include "iostream. H"
 
Void main (void)
{
Int B [] = {9, 8,-, 5,-, 0 };
Int Len = sizeof (B)/sizeof (INT );
 
Cout <"before sort:/N ";

For (INT I = 0; I <Len; I ++)
Cout <B [I] <"";

Cout <"/n is selecting sorting:/N ";

For (I = 0; I <len-1; I ++) {// len-1 selection is required for both the best and worst cases
 
Int K = I; // prepare to select the element at position I. K is initially set to I

For (Int J = I + 1; j <Len; j ++)
If (B [k]> B [J])

K = J; // K indicates the position of the element with the current minimum value,
// After the for ends, K indicates the subscript from a [I] to a [len-1] minimum element
 
If (K! = I) {// you need to exchange a [I] And a [k]; maybe K = I, meaning: the original A [I]
Int temp = B [I]; // It is the minimum value between a [I] to a [len-1], K is not modified,
// Keep its initialization value I
B [I] = B [k];
B [k] = temp;
}

// Output the sorting result after this selection. Use the "|" sign as the separator between the sorted element and the unsorted sorted element.

For (INT m = 0; m <Len; m ++ ){
If (M = I)
Cout <B [m] <"| ";
Else
Cout <B [m] <"";
}

Cout <Endl;

} // End of outer
 
Cout <"/nafter sort:/N ";

For (I = 0; I <Len; I ++)
Cout <B [I] <"";

Cout <Endl;

}

 

 

 

 

 

// File: select the sorting deformation 2.cpp.

// The basic idea of sorting:
// Assume that N pieces of data are placed in array A. Sort the N numbers in ascending order.
// First, within the range from a [0] to a [n-1], the small value element is selected to exchange with a [0;
// Then, within the range from a [1] to a [n-1], the element with the smallest value is selected for exchange with a [1;
// Perform the following operations in sequence. The sorting can be completed after n-1 selections.

# Include "iostream. H"
 
Void main (void)
{
Int B [] = {9, 8,-, 5,-, 0 };
Int Len = sizeof (B)/sizeof (INT );
 
Cout <"before sort:/N ";

For (INT I = 0; I <Len; I ++)
Cout <B [I] <"";

Cout <"/n is selecting sorting:/N ";

For (I = 0; I <len-1; I ++) {// len-1 selection is required for both the best and worst cases
 
For (Int J = I + 1; j <Len; j ++)
If (B [I]> B [J]) {
Int K = B [I]; B [I] = B [J]; B [J] = K;
}
 

// Output the sorting result after this selection. Use the "|" sign as the separator between the sorted element and the unsorted sorted element.

For (INT m = 0; m <Len; m ++ ){
If (M = I)
Cout <B [m] <"| ";
Else
Cout <B [m] <"";
}

Cout <Endl;

} // End of outer
 
Cout <"/nafter sort:/N ";

For (I = 0; I <Len; I ++)
Cout <B [I] <"";

Cout <Endl;

}

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.