Skip to content

Commit a1053ab

Browse files
committed
Add selection sort
1 parent 50446f2 commit a1053ab

File tree

5 files changed

+75
-4
lines changed

5 files changed

+75
-4
lines changed

src/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Main {
77
public static void main(String[] args) {
88
int[] arr = {5, 3, 4, 2, 1};
99

10-
SortingService.sort(arr, SortingAlgorithms.BUBBLE_SORT);
10+
SortingService.sort(arr, SortingAlgorithms.SELECTION_SORT);
1111

1212
System.out.println(Arrays.toString(arr));
1313
}

src/sort/SortingService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sort;
22

33
import sort.bubble.BubbleSort;
4+
import sort.selection.SelectionSort;
45

56
public class SortingService {
67
public static void sort(int[] arr) {
@@ -12,6 +13,9 @@ public static void sort(int[] arr, SortingAlgorithms sortAlgorithms) {
1213
case BUBBLE_SORT:
1314
BubbleSort.sort(arr);
1415
break;
16+
case SELECTION_SORT:
17+
SelectionSort.sort(arr);
18+
break;
1519
}
1620
}
1721
}

src/sort/bubble/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# Bubble Sort
2+
*Largest element will be bubbled to the last in every pass*
23

34
### Time Complexity
45

56
---
6-
1. Worst Case : O(n<sup>2</sup>). The worst case occurs when we want to sort a list in ascending order, but it is arranged in descending order.
7-
2. Best Case : O(n). The average case is when the list is arranged in a jumbled order.
8-
3. Average Case : O(n). The best case occurs when the list is already arranged in the desired order.
7+
1. Worst Case: O(n<sup>2</sup>). The worst case occurs when we want to sort a list in ascending order, but it is arranged in descending order.
8+
2. Best Case: O(n). The average case is when the list is arranged in a jumbled order.
9+
3. Average Case: O(n). The best case occurs when the list is already arranged in the desired order.
910

1011
### Space Complexity
1112

src/sort/selection/README.MD

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Selection Sort
2+
*Select the minimum element and move it to its correct place in every pass*
3+
4+
### Time Complexity
5+
6+
---
7+
1. Worst Case Complexity: O(n<sup>2</sup>)
8+
If we want to sort in ascending order and the array is in descending order then, the worst case occurs.
9+
2. Best Case Complexity: O(n<sup>2</sup>)
10+
It occurs when the array is already sorted
11+
3. Average Case Complexity: O(n<sup>2</sup>)
12+
It occurs when the elements of the array are in jumbled order (neither ascending nor descending).
13+
14+
15+
### Space Complexity
16+
17+
---
18+
1. Space complexity is O(1) because an extra variable is used for swapping.
19+
20+
### Properties
21+
1. Comparison Based
22+
2. Not Stable
23+
3. Inplace
24+
4. Basic idea for Heap Sort
25+
5. Less memory writes compared to Bubble Sort
26+
27+
### Selection Sort Applications
28+
1. It is used when a small list is to be sorted
29+
2. If cost of swapping does not matter
30+
3. If checking of all the elements is compulsory
31+
4. If cost of writing to a memory matters like in flash memory (number of writes/swaps is O(n) as compared to O(n<sup>2</sup>) of bubble sort)
32+
33+
### Advantages
34+
1. Simple and easy to understand.
35+
2. Works well with small datasets.
36+
37+
### Disadvantages
38+
1. Selection sort has a time complexity of O(n<sup>2</sup>) in the worst and average case.
39+
2. Does not work well on large datasets.
40+
3. Does not preserve the relative order of items with equal keys which means it is not stable.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package sort.selection;
2+
3+
public class SelectionSort {
4+
public static void sort(int[] arr) {
5+
int n = arr.length;
6+
7+
// loop for (n-1) passes
8+
for (int i = 0; i < n; i++) {
9+
// assume this is the index of min element
10+
int min_index = i;
11+
12+
// loop to find min element in remaining elements
13+
for (int j = i + 1; j < n; j++) {
14+
// compare and find index of min element < assumed arr[min_index]
15+
if (arr[j] < arr[min_index]) {
16+
min_index = j;
17+
}
18+
}
19+
20+
// swap elements to put min element to its correct position
21+
int temp = arr[i];
22+
arr[i] = arr[min_index];
23+
arr[min_index] = temp;
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)