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
17 changes: 16 additions & 1 deletion Exercise_1.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
//Time Complexity : O(logn)
// Space Complexity : O(logn)
#include <stdio.h>

// A recursive binary search function. It returns
// location of x in given array arr[l..r] is present,
// otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
//Your Code here
if(l > r){
return -1;
}
int mid = l + (r-1) / 2;

if (arr[mid] == x){
return mid;
}

if(arr[mid] > x){
return binarySearch(arr,l,mid-1,x);
}

return binarySearch(arr,mid+1,r,x);
}

int main(void)
Expand Down
24 changes: 21 additions & 3 deletions Exercise_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ using namespace std;
// A utility function to swap two elements
void swap(int* a, int* b)
{
//Your Code here
int temp = *a;
*a = *b;
*b = temp;
}

/* This function takes last element as pivot, places
Expand All @@ -14,7 +16,18 @@ to left of pivot and all greater elements to right
of pivot */
int partition (int arr[], int low, int high)
{
//Your Code here
int pivot = arr[high];
int i = low - 1;

for(int j = low;j <= high -1 ; j++){
if (arr[j] < pivot){

i++;
swap(&arr[i], &arr[j]);
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
}

/* The main function that implements QuickSort
Expand All @@ -23,7 +36,12 @@ low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
//Your Code here
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

/* Function to print an array */
Expand Down
15 changes: 13 additions & 2 deletions Exercise_3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@ struct Node
/* Function to get the middle of the linked list*/
void printMiddle(struct Node *head)
{
//YourCode here
//Use fast and slow pointer technique
if (head == NULL)
return;

struct Node* slow = head;
struct Node* fast = head;

while (fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = fast->next->next;
}

printf("Middle element is [%d]\n", slow->data);
}

// Function to add a new node
Expand Down
36 changes: 34 additions & 2 deletions Exercise_4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,46 @@
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
//Your code here
int n1 = m - l + 1;
int n2 = r - m;

int L[n1], R[n2];

for (int i = 0; i < n1; i++)
L[i] = arr[l + i];
for (int j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

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

while (i < n1 && j < n2)
{
if (L[i] <= R[j])
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}

while (i < n1)
arr[k++] = L[i++];

while (j < n2)
arr[k++] = R[j++];
}

/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
//Your code here
if (l < r)
{
int m = l + (r - l) / 2;

mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);

merge(arr, l, m, r);
}
}

/* UTILITY FUNCTIONS */
Expand Down
48 changes: 46 additions & 2 deletions Exercise_5.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//Time Complexity : O(n logn)
// Space Complexity : O(logn)

#include <bits/stdc++.h>
using namespace std;

Expand All @@ -12,15 +15,56 @@ void swap(int* a, int* b)
/* This function is same in both iterative and recursive*/
int partition(int arr[], int l, int h)
{
//Do the comparison and swapping here
int pivot = arr[h];
int i = l - 1;

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

swap(&arr[i + 1], &arr[h]);
return i + 1;
}

/* A[] --> Array to be sorted,
l --> Starting index,
h --> Ending index */
void quickSortIterative(int arr[], int l, int h)
{
//Try to think that how you can use stack here to remove recursion.
// Create an explicit stack
stack<int> st;

// Push initial values
st.push(l);
st.push(h);

// Pop until stack is empty
while (!st.empty())
{
h = st.top(); st.pop();
l = st.top(); st.pop();

int p = partition(arr, l, h);

// If elements on left side of pivot
if (p - 1 > l)
{
st.push(l);
st.push(p - 1);
}

// If elements on right side of pivot
if (p + 1 < h)
{
st.push(p + 1);
st.push(h);
}
}
}

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