Sort five simple Select sort

Source: Internet
Author: User

key points Simple selection sorting is a Select Sort

Select sort : Each trip selects the smallest record of the keyword from the records to be sorted, placing the order at the end of the sorted sequence of records until the sorting is complete.

Simple Sort processing Flow:

(1) from the sequence to be sorted, find the smallest element of the keyword;
(2) If the minimum element is not the first element of the sequence to be sorted, it is swapped with the first element;
(3) from the remaining N-1 elements, find the element with the smallest keyword, repeat (1), (2), until the sort is finished.

, in each order, place the element of the current sub-I on position I .

Core code

PublicvoidSelectionsort (int[] list) {
//the number of times you need to traverse to get minimum values
//It is important to note that when the number of N is to be sorted, it is already an ordered sequence after N-1 traversal.
for(inti = 0; i < list.length-1; i++) {
inttemp = 0;
intindex = i;//used to save the least worth index

//looking for the small number of the first I
for(intj = i + 1; J < List.length; J + +) {
if(List[index] > List[j]) {
index = j;
}
}

//Place the small number I found in the first position.
temp = List[index];
List[index] = List[i];
List[i] = temp;
}
}

Algorithm Analysis simple selection of the performance of sorting algorithms
 Sort category Sorting methods Complexity of Time Complexity of space Stability Complexity Average situation Worst case scenario Best case Select sort Simple selection sorting O (N2) O (N2) O (N2) O (1) Not stable Simple

Complexity of Time

The comparison number of simple selection sorts is independent of the initial ordering of the sequence. Assuming that the sequence to be sorted has n elements, the number of comparisons is always n (N-1)/2.

The number of moves is related to the initial ordering of the sequence. When the sequence is positive, the minimum number of moves is 0.

When the sequence is reversed, the maximum number of moves is 3N (N-1)/2.

Therefore, the time complexity of simple sorting is O (N2), combined above.

Complexity of Space

Simple selection sorting requires a temporary space to be used when exchanging values.

Full Reference Code Java version
Code Implementation1ImportJava.util.Random;
2
3 Public classSelectionsort {
4
5 PublicvoidSelectionsort (int[] list) {
6//the number of times you need to traverse to get minimum values
7        //It is important to note that when the number of N is to be sorted, it is already an ordered sequence after N-1 traversal.
8 for(inti = 0; i < list.length-1; i++) {
9inttemp = 0;
Ten intindex = i;//used to save the least worth index
One
A            //looking for the small number of the first I
- for(intj = i + 1; J < List.length; J + +) {
-if(List[index] > List[j]) {
theindex = j;
-}
-}
-
+//Place the small number I found in the first position.
-temp = List[index];
+List[index] = List[i];
AList[i] = temp;
at
-System.out.format ("%d trip: \ t", i + 1);
-Printall (list);
-}
-}
-
in//Print Complete Sequence
- PublicvoidPrintall (int[] list) {
to for(intValue:list) {
+System.out.print (value + "\ T");
-}
theSystem.out.println ();
*}
\$
Panax Notoginseng PublicStaticvoidMain (string[] args) {
-//initialize a random sequence
theFinalintMax_size = 10;
+int[] Array =Newint[Max_size];
ARandom random =NewRandom ();
the for(inti = 0; i < max_size; i++) {
+Array[i] = Random.nextint (max_size);
-}
\$
\$//call the bubbling Sort method
-Selectionsort selection =NewSelectionsort ();
-System.out.print ("before sorting: \ t");
theSelection.printall (array);
-Selection.selectionsort (array);
WuyiSystem.out.print ("After sorting: \ t");
theSelection.printall (array);
-}
Wu
-}Java implementation of simple selection sorting

Run results

Before sorting: 5 6 1 6 1 1 5 5 4 2
1th Trip: 1 6 5 6 1 1 5 5 4 2
2nd trip: 1 1 5 6 6 1 5 5 4 2
3rd trip: 1 1 1 6 6 5 5 5 4 2
4th trip: 1 1 1 2 6 5 5 5 4 6
5th trip: 1 1 1 2 4 5 5 5 6 6
6th trip: 1 1 1 2 4 5 5 5 6 6
7th trip: 1 1 1 2 4 5 5 5 6 6
8th trip: 1 1 1 2 4 5 5 5 6 6
9th trip: 1 1 1 2 4 5 5 5 6 6
After sorting: 1 1 1 2 4 5 5 5 6 6

References "Data structure exercises and analysis" (B-Level 3rd edition)

Sort five simple Select sort

Related Keywords:

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.