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
24 changes: 23 additions & 1 deletion Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
// Time Complexity : o(log n)
// Space Complexity :o(1)
// Did this code successfully run on Leetcode : N/A it is not a leetcode problem
// Any problem you faced while coding this :no


// Your code here along with comments explaining your approach
//1. consider left pointer at 0th index and right pointer at last index and middle of both.
//2. iterate until left and right not crossing
//3. check if target is matching with mid element, if yes return mid index.
//4. Else if target is greater than mid element then check the target on right side of of mid, else go towards left side.

class BinarySearch {
// Returns index of x if it is present in arr[l.. r], else return -1
int binarySearch(int arr[], int l, int r, int x)
{
//Write your code here
while(l<=r){
int mid = l+(r-l)/2;
if(arr[mid]== x){
return mid;
}else if(arr[mid]<x){
l = mid+1;
}else{
r = mid-1;
}
}
return -1;
}

// Driver method to test above
Expand Down
37 changes: 34 additions & 3 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
// Time Complexity :o(nlogn)
// Space Complexity : o(n) recursive stack space, n is no of elements in array.
// Did this code successfully run on Leetcode : no
// Any problem you faced while coding this :n/a


// Your code here along with comments explaining your approach
// step 1 decide pivot as last element in array
// and partition the element.
// recursively call sort on left side of pivot and on right side of pivot.Elements less than pivot are towards left and greater than pivot toward right

class QuickSort
{
/* This function takes last element as pivot,
Expand All @@ -8,20 +19,40 @@ class QuickSort
of pivot */
void swap(int arr[],int i,int j){
//Your code here
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;

}

int partition(int arr[], int low, int high)
{
//Write code here for Partition and Swap
{
int pivot = arr[high];
int i = low-1;
for(int j= low;j<=high-1;j++){
if(arr[j]<=pivot){
i++;
swap(arr, i, j);
}
}
swap(arr,i+1,high);
return i+1;
}

/* The main function that implements QuickSort()
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void sort(int arr[], int low, int high)
{
// Recursively sort elements before
// Recursively sort elements before
// partition and after partition
if(low<high){
int pivot = partition(arr, low, high);
sort(arr, low, pivot-1);
sort(arr, pivot+1, high);

}
}

/* A utility function to print array of size n */
Expand Down
32 changes: 32 additions & 0 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
// Time Complexity :o(n)
// Space Complexity :o(1)
// Did this code successfully run on Leetcode :n/a
// Any problem you faced while coding this :n.a


// Your code here along with comments explaining your approach
//Iterate over linklist and find length of linkedList
//iterate till half of linkedlist and inittialize temp node to head and move temparary node foward.
//once temparary node reached at half of linkedlist, return temparary node
class LinkedList
{
Node head; // head of linked list
Expand All @@ -13,13 +23,35 @@ class Node
next = null;
}
}

public int getLen(){
int length =0;
Node temp = head;
while(temp!=null){
length++;
temp = temp.next;
}
return length;
}

/* Function to print middle of linked list */
//Complete this function
void printMiddle()
{
//Write your code here
//Implement using Fast and slow pointers
if(head!=null){
int length = getLen();
Node temp = head;
int middleLength = length/2;
while(middleLength!=0){
temp = temp.next;
middleLength--;
}
System.out.println("The middle element is [" + temp.data + "]");
System.out.println("\n\n");
}

}

public void push(int new_data)
Expand Down
50 changes: 49 additions & 1 deletion Exercise_4.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,53 @@
// Time Complexity : O(nlogn)
// Space Complexity :O(n)
// Did this code successfully run on Leetcode : N/A
// Any problem you faced while coding this :N/A

// Your code here along with comments explaining your approach
//Divide the array into halves, sort each half recursively, then merge them.
class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
void merge(int arr[], int left, int mid, int right)
{
//Your code here
int n1 = mid-left+1;
int n2 = right - mid;
int[] L = new int[n1];
int[] R = new int[n2];
for(int i =0; i<n1;i++){
L[i] = arr[left+i];
}
for(int j =0; j<n2;j++){
R[j] = arr[mid+1+j];
}
int i = 0;
int j = 0;
int k = left;
while(i<n1 && j<n2){
if(L[i]<=R[j]){
arr[k] = L[i];
i++;
}else{
arr[k] = R[j];
j++;
}
k++;
}
while(i<n1){
arr[k] = L[i];
i++;
k++;
}
while(j<n2)
{
arr[k] = R[j];
j++;
k++;
}

}

// Main function that sorts arr[l..r] using
Expand All @@ -14,6 +56,12 @@ void sort(int arr[], int l, int r)
{
//Write your code here
//Call mergeSort from here
if(l<r){
int mid = l+(r-l)/2;
sort(arr, l,mid);
sort(arr, mid+1, r);
merge(arr,l, mid, r);
}
}

/* A utility function to print array of size n */
Expand Down
60 changes: 59 additions & 1 deletion Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
import java.util.Stack;
import java.util.Arrays;
// Time Complexity : o(logn)
// Space Complexity :o(logn)
// Did this code successfully run on Leetcode :no
// Any problem you faced while coding this : no


// Your code here along with comments explaining your approach
// //Create an empty stack and push first and last element.

// interate over stack and while iterating perform below action:
// 1. Find pivot
// 2. push left subarray from first element till pivot
// 3. push right subarray from next element of pivot till last element.



class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
Expand All @@ -7,14 +25,54 @@ void swap(int arr[], int i, int j)
/* This function is same in both iterative and
recursive*/
int partition(int arr[], int l, int h)
{
{
int pivot = arr[h];
int i = l-1;
for(int j = l; j<h;j++){

if(arr[j]<=pivot){
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

int temp = arr[i+1];
arr[i+1] = arr[h];
arr[h] = temp;
return i+1;
//Compare elements and swap.
}

// Sorts arr[l..h] using iterative QuickSort
void QuickSort(int arr[], int l, int h)
{
//Try using Stack Data Structure to remove recursion.
//create stack to store lower and upper bound of subarrays
Stack<Integer> stack = new Stack();

//push initial bound to the stack
stack.push(l);
stack.push(h);

//keep popping from stack while it is not empty
while(!stack.isEmpty()){
h = stack.pop();
l = stack.pop();
if(l < h){
int pivotIndex = partition(arr, l, h);
//If there are elements on left side of the pivot , push their indices to stack
if(pivotIndex-1>l){
stack.push(l);
stack.push(pivotIndex-1);
}
if(pivotIndex + 1 < h){
stack.push(pivotIndex+1);
stack.push(h);
}
}
}
}

// A utility function to print contents of arr
Expand Down