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
#searchFirst is a flag not for finding the first/last occurence | |
def binarySearch(arr, l, r, x, searchFirst): | |
result = -1 | |
while l <= r: | |
mid = (l + r ) / 2 | |
# Check if x is present at mid | |
if arr[mid] == x: |
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
class Search: | |
def __init__(self): | |
self.array = [3, 4, 5, 1, 2] | |
def search(self, low, high, key): | |
if low > high: | |
return -1 | |
mid = (low + high) / 2 |
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
class Search: | |
def __init__(self): | |
# self.array = [3, 4, 5, 6, 7, 8, 1, 2] | |
# self.array = [3, 4, 5, 6, 7, 8,9, 10,11,12, 0,1,2] | |
self.array = [15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14] | |
def search(self, low, high): | |
# if the entire array is sorted then pivot point is 0 | |
if self.array[low] < self.array[high]: |
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
def insertionSort(alist): | |
for index in range(1, len(alist)): | |
currentvalue = alist[index] | |
position = index | |
while position > 0 and alist[position - 1] > currentvalue: | |
alist[position] = alist[position - 1] | |
position = position - 1 |
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
# http://www.geeksforgeeks.org/search-floor-and-ceil-in-a-sorted-array/ | |
def binarySearch(arr, l, r, x): | |
if x < arr[l]: | |
return arr[l] | |
if x > arr[r]: |
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
import java.io.*; | |
import java.util.*; | |
import java.lang.Math; | |
class Kadane | |
{ | |
public static void main (String[] args) | |
{ | |
int [] a = {-2, -3, 4, -1, -2, 1, 5, -3}; |
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
// https://github.com/careermonk/DataStructureAndAlgorithmsMadeEasyInJava/blob/master/src/chapter03linkedlists/MergeSortedListsRecursion.java | |
public class MergeSortedListsIterative { | |
public ListNode mergeTwoLists(ListNode head1, ListNode head2) { | |
ListNode head = new ListNode(0); | |
ListNode curr = head; | |
while(head1 != null && head2 != null){ | |
if(head1.data <= head2.data){ | |
curr.next = head1; | |
head1 = head1.next; | |
}else{ |
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
// Java program to sort an array of 0, 1 and 2 | |
import java.io.*; | |
class countzot { | |
// Sort the input array, the array is assumed to | |
// have values in {0, 1, 2} | |
static void sort012(int a[], int arr_size) | |
{ | |
int lo = 0; |
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
private Node getIntersectedLinkedList(Node first, Node second){ | |
Node ptr = first; | |
Node ptr1 = second; | |
Node previous = null; | |
Node firstIntersectedNode = null; | |
while (ptr != null) { | |
ptr1=second; | |
while (ptr1!= null && ptr.getValue() != ptr1.getValue() ) { | |
ptr1 = ptr1.getNext(); | |
} |
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
// N time and O(1)O(1) space | |
import java.util.Map; | |
import java.util.HashMap; | |
class Fibber { | |
Map<Integer, Integer> memo = new HashMap<Integer, Integer>(); | |
public int fib(int n) { |
OlderNewer