Skip to content

Commit edef6b0

Browse files
author
Mohit Soni
committed
selection sort - JS % Python 3, BubbleSort - Python3
1 parent 75fb208 commit edef6b0

4 files changed

Lines changed: 55 additions & 0 deletions

File tree

JavaScript/SelectionSort.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const numbers = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];
2+
3+
function selectionSort(array) {
4+
const length = array.length;
5+
for(let i = 0; i < length; i++){
6+
// set current index as minimum
7+
let min = i;
8+
let temp = array[i];
9+
for(let j = i+1; j < length; j++){
10+
if (array[j] < array[min]){
11+
//update minimum if current is lower that what we had previously
12+
min = j;
13+
}
14+
}
15+
array[i] = array[min];
16+
array[min] = temp;
17+
}
18+
return array;
19+
}
20+
21+
selectionSort(numbers);
22+
console.log(numbers);

Python3/BubbleSort.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
numbers = [99, 44, 6, 2, 1, 5, 63, -5, 87, 283, 4, 0]
2+
3+
def bubbleSort(array):
4+
for i in range(0, len(array)):
5+
for j in range(0, len(array)-1):
6+
if array[j] > array[j+1]:
7+
temp = array[j+1]
8+
array[j+1] = array[j]
9+
array[j] = temp
10+
11+
bubbleSort(numbers)
12+
print(numbers)

Python3/SelectionSort.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
numbers = [99, 44, 6, 2, 1, 5, 63, -5, 87, 283, 460, 4, 0]
2+
3+
def selectionSort(array):
4+
length = len(array)
5+
for i in range(0, length):
6+
# set current index as minimum
7+
min = i
8+
temp = array[i]
9+
for j in range(i+1, length):
10+
if array[j] < array[min]:
11+
# update minimum if current is lower that what we had previously
12+
min = j
13+
array[i] = array[min]
14+
array[min] = temp
15+
return array
16+
17+
selectionSort(numbers)
18+
print(numbers)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ Points to be noted:
4747
* [BinarySearch](https://github.com/manishdangi98/algorithms/blob/master/Java/BinarySearch.java)
4848
* Python
4949
* Python3
50+
* [BubbleSort](https://github.com/mohitsoni-dev/algorithms/blob/master/Python3/BubbleSort.py)
51+
* [SelectionSort](https://github.com/mohitsoni-dev/algorithms/blob/master/Python3/SelectionSort.py)
5052
* Javascript
5153
* [BubbleSort](https://github.com/mohitsoni-dev/algorithms/blob/master/JavaScript/BubbleSort.js)
5254
* [InsertionSort](https://github.com/mohitsoni-dev/algorithms/blob/master/JavaScript/InsertionSort.js)
55+
* [SelectionSort](https://github.com/mohitsoni-dev/algorithms/blob/master/JavaScript/SelectionSort.js)
5356
* Go
5457
* Swift
5558
* C

0 commit comments

Comments
 (0)