Skip to content

Commit 8308a41

Browse files
Merge pull request #2 from Sphesihlebjamile/sort/selection-sort
Selection Sort Algorithm
2 parents 6e238ad + 620851c commit 8308a41

5 files changed

Lines changed: 76 additions & 7 deletions

File tree

‎.idea/material_theme_project_new.xml‎

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/com/sketch/Main.java‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
package com.sketch;
22

3-
import com.sketch.search.binarysearch.BinarySearch;
3+
import com.sketch.sort.selectionsort.SelectionSort;
4+
5+
import java.util.Arrays;
6+
import java.util.Random;
47

58
public class Main {
69
public static void main(String[] args){
7-
int[] sortedArray = { 1, 2, 3, 5, 7, 9, 11, 14, 17, 26, 18, 37, 42, 43, 44, 47, 48, 49, 78, 227, 5556, 8976 };
8-
int valueToFind = 18;
10+
int[] unsortedArray = new int[20];
11+
Random random = new Random();
12+
for(int i = 0; i < unsortedArray.length; i++){
13+
unsortedArray[i] = random.nextInt(20);
14+
}
15+
16+
int[] unsortedArrayResult = new int[unsortedArray.length];
17+
System.arraycopy(unsortedArray, 0, unsortedArrayResult, 0, unsortedArray.length);
918

10-
int binarySearchIndex = BinarySearch.searchRecursive(sortedArray, valueToFind, 0, sortedArray.length - 1);
19+
unsortedArrayResult = SelectionSort.sort(unsortedArrayResult);
1120

12-
System.out.println("Expected Value Index: 20");
13-
System.out.println("Actual Value Index: " + binarySearchIndex);
21+
System.out.println("Actual Value Index: " + Arrays.toString(unsortedArray));
22+
System.out.println("Actual Value Index: " + Arrays.toString(unsortedArrayResult));
1423
}
1524
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.sketch.sort.selectionsort;
2+
3+
public class SelectionSort {
4+
public static int[] sort(int[] unsortedArray){
5+
if(unsortedArray.length == 1){
6+
return unsortedArray;
7+
}
8+
9+
int length = unsortedArray.length;
10+
11+
for (int i = 0; i < length - 1; i ++){
12+
int min = unsortedArray[i];
13+
int minIndex = i;
14+
for(int j = i + 1; j < unsortedArray.length; j++){
15+
if(unsortedArray[j] < min){
16+
min = unsortedArray[j];
17+
minIndex = j;
18+
}
19+
}
20+
swap(unsortedArray, i, minIndex);
21+
}
22+
23+
return unsortedArray;
24+
}
25+
26+
private static void swap(int[] array, int a, int b){
27+
int temp = array[a];
28+
array[a] = array[b];
29+
array[b] = temp;
30+
}
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Selection Sort Algorithm
2+
**Selection Sort** is a comparison-based sorting algorithm. It sorts by repeatedly selecting the **smallest (or largest)** element from the unsorted portion and swapping it with the first unsorted element.
3+
1. Find the smallest element and swap it with the first element. This way we get the smallest element at its correct position.
4+
2. Then find the smallest among remaining elements (or second smallest) and swap it with the second element.
5+
3. We keep doing this until we get all elements moved to correct position.
6+
7+
![img.png](img.png)
8+
9+
## Advantages of Selection Sort
10+
- Easy to understand and implement, making it ideal for teaching basic sorting concepts.
11+
- Requires only a constant O(1) extra memory space.
12+
- It requires less number of swaps (or memory writes) compared to many other standard algorithms. Only cycle sort beats it in terms of memory writes. Therefore it can be simple algorithm choice when memory writes are costly.
13+
14+
## Disadvantages of Selection Sort
15+
- Selection sort has a time complexity of **O(n^2)** makes it slower compared to algorithms like Quick Sort or Merge Sort.
16+
- Does not consistently maintain the relative order of equal elements which means it is not stable.
17+
18+
## Complexity Analysis of Selection Sorty
19+
- Time Complexity:
20+
- **O(n^2)**
21+
- Auxiliary Space:
22+
- **O(1)**
23+
24+
## Applications of Selection Sort
25+
- Perfect for teaching fundamental sorting mechanisms and algorithm design.
26+
- Suitable for small lists where the overhead of more complex algorithms isn't justified and memory writing is costly as it requires less memory writes compared to other standard sorting algorithms.
27+
- **Heap Sort** algorithm is based on Selection Sort.
92.3 KB
Loading

0 commit comments

Comments
 (0)