-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
62 lines (52 loc) · 1.96 KB
/
SelectionSort.java
File metadata and controls
62 lines (52 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package Algorithms.Sorting;
import java.util.Arrays;
/**
* @author Srinvas Vadige, srinivas.vadige@gmail.com
* @since 21 Sept 2024
@TimeComplexity O(n²) worst case and O(n²) best case - consistency
@SpaceComplexity O(1)
*/
public class SelectionSort { // Select from left to right; j=i+1
public static void main(String[] args) {
System.out.println(Arrays.toString(selectionSort1(new int[]{3, 2, 4, -1, 1000, 100, 3, 1})));
}
/**
* @TimeComplexity O(n²) worst case and O(n²) best case - consistency
* @SpaceComplexity O(1)
Move from left to right and sort is also from left to right and IN-PLACE
APPROACH:
FIND THE SMALLEST jItem FOR i POSITION
-> choose the smallest item from all the right side items and place it in i-th position
see {@link #selectionSort2} for easy understanding
So it's looks like the combination of {@link Algorithms.Sorting.BubbleSort#bubbleSortUsingClassicApproach} and {@link Algorithms.Sorting.InsertionSort#insertionSortUsingSwap}
*/
public static int[] selectionSort1(int[] items){
for(int i=0; i<items.length; i++){
//
for(int j=i+1; j<items.length; j++){
int iItem = items[i];
int jItem = items[j];
if(iItem > jItem){
int temp = items[i];
items[i] = items[j];
items[j] = temp;
}
}
}
return items;
}
public static int[] selectionSort2(int[] items){
for (int i=0; i<items.length; i++) {
int minNumIndex = i; // smallest item index
for (int j=i+1; j<items.length; j++){
if (items[minNumIndex] > items[j]){
minNumIndex = j;
}
}
int temp = items[i];
items[i] = items[minNumIndex];
items[minNumIndex] = temp;
}
return items;
}
}