Skip to content

Commit

Permalink
Add day 33
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhavBahl committed Feb 4, 2019
1 parent b3ad1c3 commit e028851
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 34 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Motivate yourself to code daily till 60 days, and see the magic! Coding will bec
| [Day 30](./day30) | [Naive Search](./day30) | [http://codetoexpress.tech/dc/day30/](http://codetoexpress.tech/dc/day30/) | **Intermediate** |
| [Day 31](./day31) | [Bubble Sort](./day31) | [http://codetoexpress.tech/dc/day31/](http://codetoexpress.tech/dc/day31/) | **Beginner** |
| [Day 32](./day32) | [Selection Sort](./day32) | [http://codetoexpress.tech/dc/day32/](http://codetoexpress.tech/dc/day32/) | **Intermediate** |
| [Day 33](./day32) | [Insertion Sort](./day33) | [http://codetoexpress.tech/dc/day33/](http://codetoexpress.tech/dc/day33/) | **Beginner** |

## [More Problems](./BONUS/README.md)

Expand Down
29 changes: 12 additions & 17 deletions day29/JavaScript/binary.js
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
29 changes: 12 additions & 17 deletions day29/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,19 @@ output: undefined
### [JavaScript Implementation](./JavaScript/binary.js)

```js
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);
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;
}
if(arr[middle] === elem){
return middle;
}
return undefined;

return -1;
}

console.log (binarySearch ([1, 2, 3, 4, 5, 8, 9], 8));
console.log (binarySearch ([1, 2, 3, 4, 5, 8, 9], 7));
console.log (binary ([1, 2, 3, 4, 5, 6, 7, 8, 9], 5)); // 4
```
13 changes: 13 additions & 0 deletions day32/JavaScript/insertionsort.js
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.
36 changes: 36 additions & 0 deletions day33/README.md
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]));
```
Binary file added day33/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added day33/ques.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e028851

Please sign in to comment.