Skip to content
Open

done #1873

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
11 changes: 10 additions & 1 deletion Exercise_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ 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
int mid=(l+r)/2;
if(arr[mid]==x) return mid;
else if(arr[mid]>x)
{
return binarySearch(arr, l, mid, x);
}

else{
return binarySearch(arr, mid,r, x);}
//Write your code here
}

// Driver method to test above
Expand Down
26 changes: 25 additions & 1 deletion Exercise_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,29 @@ 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)
{
int pivot=arr[high];
int i=low-1;

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


}

swap(arr, i+1, high);
return i+1;
//Write code here for Partition and Swap
}
/* The main function that implements QuickSort()
Expand All @@ -20,7 +39,12 @@ int partition(int arr[], int low, int high)
high --> Ending index */
void sort(int arr[], int low, int high)
{
// Recursively sort elements before
if(low<high)
{
int p=partition(arr, low, high);
sort(arr, low, p-1);
sort(arr, p+1, high);
} // Recursively sort elements before
// partition and after partition
}

Expand Down
13 changes: 11 additions & 2 deletions Exercise_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@ class Node
/* Function to print middle of linked list */
//Complete this function
void printMiddle()
{
//Write your code here
{ if(head==null) return;
Node s=head;
Node f=head;
while(f!=null && f.next!=null)
{
s=s.next;
f=f.next.next;

}

System.out.println(s.data);//Write your code here
//Implement using Fast and slow pointers
}

Expand Down
44 changes: 44 additions & 0 deletions Exercise_4.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,57 @@ class MergeSort
void merge(int arr[], int l, int m, int r)
{
//Your code here
int n1=m-l+1;
int n2=r-m;

int []a=new int[n1];
int []b=new int [n2];
for (int i = 0; i < n1; i++)
a[i] = arr[l + i];

for (int j = 0; j < n2; j++)
b[j] = arr[m + 1 + j];
int i=0,j=0, k=l;
while(i<n1 && j<n2)
{
if(a[i]<=b[j])
{
arr[k]=a[i];
i++;
}
else{
arr[k]=b[j];
j++;
}

k++;
}

while(i<n1)
{
arr[k]=a[i];
i++;k++;
}
while(j<n2)
{
arr[k]=b[j];
j++;k++;
}
}

// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
//Write your code here
if(l<r)
{
int m=l+(r-l)/2;

sort(arr, l, m);
sort(arr,m+1,r);
merge(arr, l, m , r);
}
//Call mergeSort from here
}

Expand Down
40 changes: 39 additions & 1 deletion Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,58 @@
import java.util.Stack;
class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
//Try swapping without extra variable
if (i == j) return;
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-1;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.
if(l>=h) return ;
Stack <int[]> st =new Stack<>();
st.push(new int []{l,h});

while(!st.isEmpty())
{
int [] range=st.pop();
int start=range[0],end=range[1];

if(start<end)
{
int p=partition(arr, start, end);
if(p-1>start){st.push(new int[]{start, p-1});}
if(p+1<end)
{
st.push(new int [] {p+1, end});
}
}
}

}

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