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

Precourse-2 complete #1634

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
17 changes: 17 additions & 0 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
//Time Complexity: O(logn)
//Space Complexity: O(1)
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
if(r>=l){
int mid= l+(r-l)/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);

}
return -1;


}

// Driver method to test above
Expand Down
25 changes: 25 additions & 0 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//Time Complexity: O(nlogn)
//Space Complexity: O(logn)

class QuickSort
{
/* This function takes last element as pivot,
Expand All @@ -8,11 +11,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 pivot=high;
int s=low;
for(int i =low;i<=high;i++){
if(arr[i]<arr[pivot]){
swap(arr, s, i);
s++;
}

}
swap(arr, s, pivot);
return s;

}
/* The main function that implements QuickSort()
arr[] --> Array to be sorted,
Expand All @@ -22,6 +40,13 @@ void sort(int arr[], int low, int high)
{
// Recursively sort elements before
// partition and after partition
//Base Case
if(low<high){
int pa=partition(arr, low, high);
sort(arr, low, pa-1); // Left Partition
sort(arr, pa+1, high);// Right Partition
}

}

/* A utility function to print array of size n */
Expand Down
14 changes: 14 additions & 0 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//Time Complexity: O(n)
//Spaxxe Complexity: O(1)
class LinkedList
{
Node head; // head of linked list
Expand All @@ -18,8 +20,20 @@ class Node
//Complete this function
void printMiddle()
{
if(head==null){
System.out.println("The List is empty");
return;
}
//Write your code here
//Implement using Fast and slow pointers
Node fast=head; //fast pointer
Node slow=head; //slow pointer

while (fast!=null && fast.next!=null) {
slow=slow.next;
fast=fast.next.next;
}
System.out.println("The mid element is :" + slow.data);
}

public void push(int new_data)
Expand Down
58 changes: 58 additions & 0 deletions Exercise_4.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//Time Complexity: O(nlong)
//Space Complexity: O(n)
class MergeSort
{
// Merges two subarrays of arr[].
Expand All @@ -6,6 +8,54 @@ class MergeSort
void merge(int arr[], int l, int m, int r)
{
//Your code here
int p=m-l+1;
int q=r-m;

//Temp Arrays:
int left[]=new int[p];
int right[]=new int[q];
// copy data in temp arrays
for(int i=0;i<p;i++)
{ left[i]=arr[l+i];}

for(int j=0;j<q;j++)
{ right[j]=arr[m+1+j];}

//Merge the temp arrays back to array

int i=0,j=0;
int k=l;//initial index of merged subarray
while (i<p && j<q) {
if(left[i]<=right[j]){
arr[k]=left[i];
i++;
}else{
arr[k]=right[j];
j++;
}
k++;

}
//copy remainig elements of left array
while (i<p) {
arr[k]=left[i];
i++;
k++;

}
//copy remaining elements of right array
while (j<q) {
arr[k]=right[j];
j++;
k++;

}






}

// Main function that sorts arr[l..r] using
Expand All @@ -14,6 +64,14 @@ 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 sorted halves
merge(arr, l, mid, r);
}
}

/* A utility function to print array of size n */
Expand Down
39 changes: 38 additions & 1 deletion Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,58 @@
import java.util.Stack;

class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
if(i!=j){
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];
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> st=new Stack<>();
st.push(l);
st.push(h);
while (!st.isEmpty()) {
h=st.pop();
l=st.pop();
//set pivot element to its correct position

int p= partition(arr, l, h);
//if elementes on left side of pivot, push left side to stack
if(p-1>l){
st.push(l);
st.push(p-1);
}
//if elements on right side, push right side to stack
if(p+1<h){
st.push(p+1);
st.push(h);
}
}
}

// A utility function to print contents of arr
void printArr(int arr[], int n)
Expand Down