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

Finished Precourse 2 #1614

Open
wants to merge 1 commit 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
13 changes: 13 additions & 0 deletions Exercise_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ 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; // getting the mid point
if(arr[mid]==x){
return mid;
}
else if(arr[mid]<x){ // target is greater than mid
l=mid+1;
}
else{
r=mid-1; // target is more than mid
}
}
return-1;
}

// Driver method to test above
Expand Down
20 changes: 20 additions & 0 deletions Exercise_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,26 @@ 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 i=low-1;
int pivot = arr[high]; // geeting the pivot
for(int j=low;j<=high;j++){
if(arr[j]<pivot){
i++;
swap(arr,i,j);
}
}
swap(arr,i+1,high); // swapping pivot
return (i+1);

}
/* The main function that implements QuickSort()
arr[] --> Array to be sorted,
Expand All @@ -22,6 +37,11 @@ void sort(int arr[], int low, int high)
{
// Recursively sort elements before
// partition and after partition
if(low<high){
int pivot = partition(arr, low, high);
sort(arr,low,pivot-1); // sorting left side of the pivot
sort(arr, pivot+1, high); // sprting right side of the pivot
}
}

/* A utility function to print array of size n */
Expand Down
8 changes: 8 additions & 0 deletions Exercise_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ void printMiddle()
{
//Write your code here
//Implement using Fast and slow pointers
Node slow=head;
Node fast=head;

while(fast!= null && fast.next!=null){
slow=slow.next; // slow reaches the mid
fast=fast.next.next; // fast reaches the end
}
System.out.println("Middle Node: " +slow.data );
}

public void push(int new_data)
Expand Down
39 changes: 38 additions & 1 deletion Exercise_4.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,37 @@ class MergeSort
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
//Your code here

//Your code here
int n1 = m+1-l; // left array length
int n2 = r-m; // right array length
int larr[] = new int[n1];
int rarr[] = new int[n2];

for(int x=0;x<n1;x++){
larr[x] = arr[l+x]; // copying left array
}
for(int x=0;x<n2;x++){
rarr[x] = arr[m+1+x]; // copying right array
}
int i=0,j=0,k=l;

while(i<n1 && j<n2){
if(larr[i]<=rarr[j]){ // comparing left and right array
arr[k++]=larr[i++]; // updating acutal array
}
else{
arr[k++]=rarr[j++];
}
}

while(i<n1){
arr[k++]=larr[i++]; // remainder is added
}
while(j<n2){
arr[k++]=rarr[j++];
}

}

// Main function that sorts arr[l..r] using
Expand All @@ -14,6 +44,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
34 changes: 34 additions & 0 deletions Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
import java.util.Stack;

class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
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 pivot=arr[h]; // same as normal quick sort
int i=l-1;
for(int j=l;j<h;j++){
if(arr[j]<pivot){
i++;
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<Integer> s = new Stack<>();
s.push(l);
s.push(h); // stack is used to mimic recursive call

while(!s.isEmpty()){
h=s.pop();
l=s.pop();
int pivot = partition(arr, l, h); // getting pivot and doing swapping

if(l<pivot-1){
s.push(l);
s.push(pivot-1);// upper bound
}
else if(h>pivot+1){
s.push(h);
s.push(pivot+1);
}
}
}

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