-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3ad1c3
commit e028851
Showing
8 changed files
with
74 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,15 @@ | ||
function binarySearch(arr, elem) { | ||
var start = 0; | ||
var end = arr.length - 1; | ||
var middle = Math.floor((start + end) / 2); | ||
while(arr[middle] !== elem && start <= end) { | ||
if(elem < arr[middle]){ | ||
end = middle - 1; | ||
} else { | ||
start = middle + 1; | ||
} | ||
middle = Math.floor((start + end) / 2); | ||
} | ||
if(arr[middle] === elem){ | ||
return middle; | ||
function binary (arr, n) { | ||
let left = 0, | ||
right = arr.length - 1, mid; | ||
|
||
while (left <= right) { | ||
mid = Math.floor((left + right)/2); | ||
if (arr[mid] === n) return mid; | ||
else if (arr[mid] < n) left = mid+1; | ||
else right = mid-1; | ||
} | ||
return undefined; | ||
|
||
return -1; | ||
} | ||
|
||
console.log (binarySearch ([1, 2, 3, 4, 5, 8, 9], 8)); // 5 | ||
console.log (binarySearch ([1, 2, 3, 4, 5, 8, 9], 7)); // undefined | ||
console.log (binary ([1, 2, 3, 4, 5, 6, 7, 8, 9], 5)); // 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
function insertionSort(arr){ | ||
var val; | ||
for(var i = 1; i < arr.length; i++){ | ||
val = arr[i]; | ||
for(var j = i - 1; j >= 0 && arr[j] > val; j--) { | ||
arr[j+1] = arr[j] | ||
} | ||
arr[j+1] = val; | ||
} | ||
return arr; | ||
} | ||
|
||
console.log ( insertionSort ([1, 5, 2, 7, 3, 4, 8, 9, 6])); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
![cover](./cover.png) | ||
|
||
# Day 32 - Search and Sort Algorithms Part F: Insertion Sort | ||
|
||
## Question | ||
|
||
Given an unsorted list of elements, write a program to sort the given list using insertion sort. | ||
|
||
**Example** | ||
|
||
``` | ||
input: [1, 5, 2, 7, 3, 4, 8, 9, 6] | ||
output: [1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
``` | ||
|
||
![ques](./ques.png) | ||
|
||
## Solution | ||
|
||
### [JavaScript Implementation](./JavaScript/insertionsort.js) | ||
|
||
```js | ||
function insertionSort(arr){ | ||
var val; | ||
for(var i = 1; i < arr.length; i++){ | ||
val = arr[i]; | ||
for(var j = i - 1; j >= 0 && arr[j] > val; j--) { | ||
arr[j+1] = arr[j] | ||
} | ||
arr[j+1] = val; | ||
} | ||
return arr; | ||
} | ||
|
||
console.log ( insertionSort ([1, 5, 2, 7, 3, 4, 8, 9, 6])); | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.