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

Done PreCourse-2 #1647

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
20 changes: 18 additions & 2 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
// Time Complexity : O(logn)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

//prune the left side if x>arr[mid] else prune the right 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(x>arr[mid]) {
l=mid+1;
}else {
r=mid-1;
}
}
return -1;
}

// Driver method to test above
Expand All @@ -11,7 +27,7 @@ public static void main(String args[])
BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int x = 40;
int result = ob.binarySearch(arr, 0, n - 1, x);
if (result == -1)
System.out.println("Element not present");
Expand Down
37 changes: 34 additions & 3 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Time Complexity : O(nlogn)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


class QuickSort
{
/* This function takes last element as pivot,
Expand All @@ -7,12 +13,31 @@ class QuickSort
pivot and all greater elements to right
of pivot */
void swap(int arr[],int i,int j){
//Your code here
int t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}

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

}
swap(arr,low,j);
return j;

}
/* The main function that implements QuickSort()
arr[] --> Array to be sorted,
Expand All @@ -21,7 +46,13 @@ int partition(int arr[], int low, int high)
void sort(int arr[], int low, int high)
{
// Recursively sort elements before
// partition and after partition
// partition and after partition
if(low<high) {
int p=partition(arr,low,high);
sort(arr,low,p-1);
sort(arr,p+1,high);
}

}

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

class LinkedList
{
Node head; // head of linked list
Expand All @@ -20,6 +25,17 @@ void printMiddle()
{
//Write your code here
//Implement using Fast and slow pointers
if(head==null) {
System.out.println("middle element does not exist");
return;
}
Node p=head;
Node q=head.next;
while(q!=null && q.next!=null) {
p=p.next;
q=q.next.next;
}
System.out.println("middle element is "+p.data);
}

public void push(int new_data)
Expand Down
44 changes: 42 additions & 2 deletions Exercise_4.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
// Time Complexity : O(nlogn)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

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)
{
//Your code here
int n=r-l+1;
int[] newarr=new int[n];
int p=l;
int q=m+1;
int i=0;
while(i<n && p<=m && q<=r) {
if(arr[p]<=arr[q]) {
newarr[i]=arr[p];
p++;
}else {
newarr[i]=arr[q];
q++;
}
i++;
}
while(p<=m) {
newarr[i]=arr[p];
i++;
p++;
}
while(q<=r) {
newarr[i]=arr[q];
i++;
q++;
}
int start=l;
for(int j=0;j<n;j++) {
arr[start]=newarr[j];
start++;
}
}

// Main function that sorts arr[l..r] using
Expand All @@ -14,6 +48,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 All @@ -28,7 +68,7 @@ static void printArray(int arr[])
// Driver method
public static void main(String args[])
{
int arr[] = {12, 11, 13, 5, 6, 7};
int arr[] = {12, 11, 14, 13, 2, 14};

System.out.println("Given Array");
printArray(arr);
Expand Down
57 changes: 53 additions & 4 deletions Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,69 @@
import java.util.ArrayList;
import java.util.List;

// Time Complexity : O(nlogn)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
int t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}

/* This function is same in both iterative and
recursive*/
int partition(int arr[], int l, int h)
{
//Compare elements and swap.
int pivot=arr[l];
int i=l+1;
int j=h;
while(i<j) {
while(i<=h && arr[i]<=pivot) {
i++;
}
while(j>=l && arr[j]>=pivot) {
j--;
}
if(i<j) {
swap(arr,i,j);
}

}
swap(arr,l,j);
return j;
}

// Sorts arr[l..h] using iterative QuickSort
void QuickSort(int arr[], int l, int h)
void QuickSort(int arr[], int low, int high)
{
//Try using Stack Data Structure to remove recursion.
Stack<List<Integer>> stk=new Stack();
List<Integer> p=new ArrayList<>();
p.add(low);
p.add(high);
stk.push(p);
while(!stk.isEmpty()) {
int x=stk.peek().get(0);
int y=stk.peek().get(1);
stk.pop();
int pivot=partition(arr,x,y);
if(pivot-1>x) {
List<Integer> l1=new ArrayList<>();
l1.add(x);
l1.add(pivot-1);
stk.push(l1);
}
if(pivot+1<y) {
List<Integer> l2=new ArrayList<>();
l2.add(pivot+1);
l2.add(y);
stk.push(l2);
}
}

}

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