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
21 changes: 21 additions & 0 deletions Exercise_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@ class BinarySearch {
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
34 changes: 31 additions & 3 deletions Exercise_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,50 @@ class QuickSort
smaller (smaller than pivot) to left of
pivot and all greater elements to right
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
//Write code here for Partition and Swap
int i = low-1;

for(int j = low; j < high; j++)
{
if(arr[j] < arr[high])
{
i += 1;
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
// partition and after partition
// 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
11 changes: 10 additions & 1 deletion Exercise_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ class Node
void printMiddle()
{
//Write your code here
//Implement using Fast and slow pointers
//Implement using Fast and slow pointers
Node slow = head, fast = head;

while(fast!= null && fast.next!=null)
{
slow = slow.next;
fast = fast.next.next;
}

System.out.println(slow.data);
}

public void push(int new_data)
Expand Down
65 changes: 65 additions & 0 deletions Exercise_4.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,64 @@ class MergeSort
void merge(int arr[], int l, int m, int r)
{
//Your code here
int n1 = m - l + 1;
int n2 = r - m;

int[] temp1 = new int[n1];
int[] temp2 = new int[n2];

int temp = 0;
for(int i = l; i <= m; i++)
{
temp1[temp] = arr[i];
}

temp = 0;
for(int i = m+1; i <= r; i++)
{
temp2[temp] = arr[i];
}

int i = 0, j = 0;
temp = l;

while(i<n1 && j<n2)
{
if(temp1[i] < temp2[j])
{
arr[temp] = temp1[i];
i++;
}

else
{
arr[temp] = temp2[j];
j++;
}

temp+=1;
}

if(i<n1)
{
while(i<n1)
{
arr[temp] = temp1[i];
i++;
temp += 1;
}
}

if(j<n2)
{
while(j<n2)
{
arr[temp] = temp2[j];
j++;
temp += 1;
}
}

}

// Main function that sorts arr[l..r] using
Expand All @@ -14,6 +72,13 @@ 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
40 changes: 39 additions & 1 deletion Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,58 @@
class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
//Try swapping without extra variable
if(i==j)
{
return;
}

arr[i] = arr[i] + arr[j];
arr[j] = arr[i] - arr[j];
arr[i] = arr[i] - arr[j];

}

/* This function is same in both iterative and
recursive*/
int partition(int arr[], int l, int h)
{
//Compare elements and swap.
int i = l-1;

for(int j = l; j < h; j++)
{
if(arr[j] < arr[h])
{
i += 1;
swap(arr, i, j);
}
}

swap(arr, i+1, h);

return i+1;
}

// Sorts arr[l..h] using iterative QuickSort
void QuickSort(int arr[], int l, int h)
{
//Try using Stack Data Structure to remove recursion.
Stack<int[]> st = new Stack<int[]>();
st.push(new int[] {l,h});

while(!st.isEmpty())
{
int[] boundary = st.pop();
int low = boundary[0], high = boundary[1];

if(low<high)
{
int pivot = partition(arr, low, high);
st.push(new int[] {low, pivot - 1});
st.push(new int[] {pivot + 1, high});
}
}
}

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