Skip to content

Commit 7ba9b69

Browse files
authored
Merge pull request vJechsmayr#101 from sarahsakhri/bubblesort/sarahsakhri
worked on bubble sorting algorithm
2 parents 31d08ab + 712fe4e commit 7ba9b69

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//Language: Javascript
2+
//Author: Sarah Sakhri
3+
//Github: https://github.com/sarahsakhri
4+
5+
let x = [22, 35, 40, 5, 777, 8, 9, 9, 223, 46, 34];
6+
7+
console.log("---- Original array -----");
8+
console.log(x);
9+
10+
function bubbleSort(array) {
11+
12+
for (let i = 0; i < array.length; i++) {
13+
14+
for (let j = 0; j < (array.length - i - 1); j++) {
15+
16+
if (array[j] > array[j + 1]) {
17+
18+
let temp = array[j];
19+
array[j] = array[j + 1];
20+
array[j + 1] = temp;
21+
}
22+
}
23+
}
24+
}
25+
26+
bubbleSort(x);
27+
28+
console.log("---- Bubble sorted array -----");
29+
console.log(x);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//Language: Javascript
2+
//Author: Sarah Sakhri
3+
//Github: https://github.com/sarahsakhri
4+
5+
let x = [66, 0, 19, -45, 26, 56, 15, 21, -98];
6+
console.log("---- Original array -----");
7+
console.log(x);
8+
9+
10+
function selectionSort(arr) {
11+
for (i = 0; i < arr.length; i++) {
12+
let min = i;
13+
for (let j = i + 1; j < arr.length; j++) {
14+
if (arr[j] < arr[min]) {
15+
min = j;
16+
}
17+
}
18+
19+
if (i !== min) {
20+
let temp = arr[i];
21+
arr[i] = arr[min];
22+
arr[min] = temp;
23+
}
24+
}
25+
26+
return arr;
27+
}
28+
29+
selectionSort(x);
30+
31+
console.log("---- Selection sorted array -----");
32+
console.log(x);
33+

0 commit comments

Comments
 (0)