Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 41 additions & 37 deletions day29/C++/binary_search.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,59 @@
/*
* @author : imkaka
* @date : 31/1/2019
*/

#include<iostream>
#include<cstdio>
/**
* @author: Karthick < karthikn2099@gmail.com >
* @github: https://github.com/karthikn2098
* @date: 31/01/2018
*/

#include <iostream>
using namespace std;

int binary_search_itr(int arr[], int size, int val){
int l = 0, r = size-1;
int mid = (l + r) / 2;
/** @desc: splits the array into two sections at each step, then checks the element in the desired one.
* @param: array , key (to be found), size of the array.
* @return: index of key if present , -1 otherwise.
* @TimeComplexity: O( logn ) < n = size of the array >
*/
int Binary_search(int arr[] , int key, int N) {

int low = 0 , high = N-1 , mid;

while(low <= high) {

while(arr[mid] != val && l <= r){
if(val < arr[mid]){
r = mid - 1;
mid = low + (high - low) / 2; //like (low + high)/2 but efficient.

if(key == arr[mid]) {
return mid;
}
else{
l = mid +1;
else if( key > arr[mid] ) {
low = mid + 1;
}
else {
high = mid - 1;
}

mid = (l + r) / 2;
}

if(arr[mid] == val)
return mid;

return -1;
}

int binary_search_rec(int arr[], int left, int right, int val){
if(right >= left){
int main(void) {

int mid = left + (right - left) / 2;
int arr[] = { 1, 3, 5, 7, 8, 9, 100, 234, 788, 899 };

if(arr[mid] == val) return mid;
int sizeOfArray = sizeof(arr) / sizeof(arr[0]);

if(arr[mid] > val)
return binary_search_rec(arr, left, mid-1, val);
return binary_search_rec(arr, mid+1, right, val);
}
cout << "\nBinary_search(arr , 100) = " << Binary_search(arr, 100, sizeOfArray ) << endl;

return -1;
}

int main(){
cout << "\nBinary_search(arr , 1000) = " << Binary_search(arr, 1000, sizeOfArray ) << endl;

int arr[] = {1, 2, 3, 6, 9, 15, 16, 14};
int val = 9;
int size = sizeof(arr)/sizeof(arr[0]);
cout << "Iterative: " << binary_search_itr(arr, size, val) << endl;
cout << "Recursive: " << binary_search_rec(arr, 0, size-1, val) << endl;
return 0;
}

/*
---------------
Sample Output
---------------

Binary_search(arr , 100) = 6

Binary_search(arr , 1000) = -1

*/
84 changes: 84 additions & 0 deletions day48/Python/DLL.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""
@author: Abhishek Reddy
@date: 2/25/2019
"""

import gc


class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None


class DLL:
def __init__(self):
self.head = None

def push(self, data):
new_node = Node(data)
new_node.next = self.head
new_node.prev = None
if self.head is not None:
self.head.prev = new_node
self.head = new_node

def pushAfter(self, prevnode, data):
if prevnode is None:
return
new_node = Node(data)
new_node.next = prevnode.next
prevnode.next = new_node
new_node.prev = prevnode
if new_node.next is not None:
new_node.next.prev = new_node

def append(self, data):
new_node = Node(data)
if self.head is None:
new_node.prev = None
new_node.next = None
self.head = new_node
return
else:
tmp = self.head
while tmp.next is not None:
tmp = tmp.next
tmp.next = new_node
new_node.prev = tmp
new_node.next = None

def popAtPos(self, prevnode):
if self.head is None or prevnode is None:
return
if self.head == prevnode:
self.head = prevnode.next
if prevnode.next is not None:
prevnode.next.prev = prevnode.prev
if prevnode.prev is not None:
prevnode.prev.next = prevnode.next
gc.collect()

def popFirst(self):
tmp = self.head
self.head = self.head.next
self.head.prev = None
tmp.next = None
def popEnd(self):
tmp = self.head
while tmp.next is not None:
tmp = tmp.next
tmp.prev.next = None
tmp.prev = None

def printList(self):
temp = self.head
while temp is not None:
print(temp.data)
temp = temp.next


d = DLL()