Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented the codes in C++ #1612

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
10 changes: 9 additions & 1 deletion Exercise_1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
// A recursive binary search function. It returns
// location of x in given array arr[l..r] is present,
// otherwise -1
using namespace std;
int binarySearch(int arr[], int l, int r, int x)
{
//Your Code here
int mid = l+(r-l)/2;
if(l==r){
if(arr[l]==x) return l;
else return -1;
}
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
18 changes: 18 additions & 0 deletions Exercise_2.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include <bits/stdc++.h>
#include <iostream>
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 @@ -15,6 +19,16 @@ of pivot */
int partition (int arr[], int low, int high)
{
//Your Code here
int i = low, j = low;
while(j<=high-1){
if(arr[j]<=arr[high]){
swap(&arr[i],&arr[j]);
i++;
}
j++;
}
swap(&arr[i],&arr[high]);
return i;
}

/* The main function that implements QuickSort
Expand All @@ -24,6 +38,10 @@ high --> Ending index */
void quickSort(int arr[], int low, int high)
{
//Your Code here
if(low>=high) return;
int low_part = partition(arr,low,high);
quickSort(arr,low,low_part-1);
quickSort(arr,low_part+1,high);
}

/* Function to print an array */
Expand Down
13 changes: 12 additions & 1 deletion Exercise_3.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include<bits/stdc++.h>
#include<bits/stdc++.h>
#include <iostream>
using namespace std;

// Struct
Expand All @@ -13,6 +14,16 @@ void printMiddle(struct Node *head)
{
//YourCode here
//Use fast and slow pointer technique
Node* fast_ptr = head, *slow_ptr = head;
if(!head){
cout<<"List is empty";
return;
}
while(fast_ptr&&fast_ptr->next){
fast_ptr = fast_ptr->next->next;
slow_ptr = slow_ptr->next;
}
cout<<"Middle element is: "<<slow_ptr->data<<endl;
}

// Function to add a new node
Expand Down
32 changes: 32 additions & 0 deletions Exercise_4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,45 @@
void merge(int arr[], int l, int m, int r)
{
//Your code here
int n_final = r-l+1;
int temp[n_final];
int p1 = l, p2 = m+1,temp_p = 0;
while(p1<=m&&p2<=r){
if(arr[p1]<=arr[p2]){
temp[temp_p] = arr[p1];
p1++;
}
else{
temp[temp_p] = arr[p2];
p2++;
}
temp_p++;
}
while(p1<=m){
temp[temp_p] = arr[p1];
p1++;
temp_p++;
}
while(p2<=r){
temp[temp_p] = arr[p2];
p2++;
temp_p++;
}
for(int i=0;i<n_final;i++){
arr[l+i] = temp[i];
}
}

/* 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) return;
int mid = l+(r-l)/2;
mergeSort(arr,l,mid);
mergeSort(arr,mid+1,r);
merge(arr,l,mid,r);
}

/* UTILITY FUNCTIONS */
Expand Down
36 changes: 35 additions & 1 deletion Exercise_5.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <bits/stdc++.h>
#include <iostream>
#include <stack>
#include <utility>
using namespace std;

// A utility function to swap two elements
Expand All @@ -10,9 +13,20 @@ void swap(int* a, int* b)
}

/* This function is same in both iterative and recursive*/
int partition(int arr[], int l, int h)
int partition(int arr[], int low, int high)
{
//Do the comparison and swapping here
//Your Code here
int i = low, j = low;
while(j<=high-1){
if(arr[j]<=arr[high]){
swap(&arr[i],&arr[j]);
i++;
}
j++;
}
swap(&arr[i],&arr[high]);
return i;
}

/* A[] --> Array to be sorted,
Expand All @@ -21,6 +35,26 @@ h --> Ending index */
void quickSortIterative(int arr[], int l, int h)
{
//Try to think that how you can use stack here to remove recursion.
stack<pair<int,int> >params;
pair<int,int>curr;
curr.first = l;
curr.second = h;
params.push(curr);
while(!params.empty()){
pair<int,int>curr = params.top();
params.pop();
int low = curr.first, high = curr.second;
if(low>=high) continue;
int low_part = partition(arr,low,high);
pair<int,int>left_curr;
left_curr.first = low;
left_curr.second = low_part-1;
pair<int,int> right_curr;
right_curr.first = low_part+1;
right_curr.second = high;
params.push(left_curr);
params.push(right_curr);
}
}

// A utility function to print contents of arr
Expand Down
Binary file added test_program_1
Binary file not shown.
Binary file added test_program_2
Binary file not shown.
Binary file added test_program_3
Binary file not shown.
Binary file added test_program_4
Binary file not shown.
Binary file added test_program_5
Binary file not shown.