what is Selection Sort algorithm
Selection Sort Algorithm
Definition
one way of sorting algorithms by using brute force.
check every possible ways to sort the list.
Algorithm steps
- find smallest number in the list
- swap the smallest number and first place
- the first element is sorted
- find smallest number in the list except the sorted element.
- swap the smallest number and second place
- Repeat 1~5 until all of them sorted.



Avg. time complexity
Worst time complexity
space complexity
stability
no
Java code
public static int[] selectionSort(int[] input) {
for (int i = 0; i < input.length - 1; ++i) {
for (int j = input.length - 1; j > i; --j) {
if (input[j] < input[i]) {
int temp = input[i];
input[i] = input[j];
input[j] = temp;
}
}
}
return input;
}
How to calculate
numbers of loop
The most visited place of the list
(the last place)
The least visited place of the list
(the first place)
Avg. visited numbers of the list
(The most visited place of the list+The least visited place of the list)/2
Polynomial Time
numbers of loop*Avg. visited numbers of the list
Time Complexity
0 개의 댓글:
댓글 쓰기