Skip to content

Commit 0ac53fb

Browse files
authored
Merge pull request vJechsmayr#98 from Lanhbao/master
Bubble Sort Algorithm
2 parents 5614404 + 3976243 commit 0ac53fb

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎SearchAlgorithms/BinarySearch.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//Language: Javascript
2+
//Author: Tran Lan Anh
3+
//Github: https://github.com/Lanhbaobao
4+
5+
var binarySearch = function(array, value) {
6+
var guess,
7+
min = 0,
8+
max = array.length - 1;
9+
10+
while(min <= max){
11+
guess = Math.floor((min + max) /2);
12+
if(array[guess] === value)
13+
return guess;
14+
else if(array[guess] < value)
15+
min = guess + 1;
16+
else
17+
max = guess - 1;
18+
}
19+
20+
return -1;
21+
}
22+
23+
var arr = [2,6,4,1,7,9];
24+
25+
binarySearch(arr, 4);

‎SortingAlgorithms/BubbleSort.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//Language: Javascript
2+
//Author: Tran Lan Anh
3+
//Github: https://github.com/Lanhbao
4+
5+
function bubbleSort(arr){
6+
for(let i = arr.length-1; i > 0; i--){
7+
for(let j = 0; j < i; j++){
8+
if(arr[j] >= arr[j+1]){
9+
let temp = arr[j];
10+
arr[j]=arr[j+1];
11+
arr[j+1]=temp;
12+
}
13+
}
14+
}
15+
return arr;
16+
}
17+
//test
18+
var arr = [5,2,3,9,0];
19+
bubleSort(arr);
20+
21+
22+

0 commit comments

Comments
 (0)