Created
August 11, 2017 04:18
-
-
Save bytao7mao/ffb9a5224ea84864595631f32f6056e0 to your computer and use it in GitHub Desktop.
pseudocod ascending order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Selection Sort | |
Suppose A is an array of N values. We want to sort A in ascending order. That is, A[0] should be the smallest and A[N-1] should be the largest. | |
The idea of Selection Sort is that we repeatedly find the smallest element in the unsorted part of the array and swap it with the first element in the unsorted part of the array. | |
For I = 0 to N-1 do: | |
Smallsub = I | |
For J = I + 1 to N-1 do: | |
If A(J) < A(Smallsub) | |
Smallsub = J | |
End-If | |
End-For | |
Temp = A(I) | |
A(I) = A(Smallsub) | |
A(Smallsub) = Temp | |
End-For | |
A refinement of the above pseudocode would be to avoid swapping an element with itself. | |
An alternate way to sort in ascending order is to find the largest value and swap with the last element in the unsorted part of the array. | |
Selection Sort does roughly N**2 / 2 comparisons and does N swaps. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very useful :
https://www.cs.cmu.edu/~adamchik/15-121/lectures/Sorting%20Algorithms/sorting.html