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

Completed Pre-course 2 #1626

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
8 changes: 8 additions & 0 deletions Exercise_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ class BinarySearch {
int binarySearch(int arr[], int l, int r, int x)
{
//Write your code here
int index = -1;
for (int i = l; i<=r; i++){
if(x == arr[i]){
index = i;
break;
}
}
return index;
}

// Driver method to test above
Expand Down
24 changes: 21 additions & 3 deletions Exercise_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@ class QuickSort
pivot and all greater elements to right
of pivot */
void swap(int arr[],int i,int j){
//Your code here
//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
//Write code here for Partition and Swap
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;
}
/* The main function that implements QuickSort()
arr[] --> Array to be sorted,
Expand All @@ -21,7 +34,12 @@ 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
9 changes: 8 additions & 1 deletion Exercise_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ void printMiddle()
{
//Write your code here
//Implement using Fast and slow pointers
Node slow = head;
Node fast = head.next;
while(fast!=null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
System.out.println(slow.data);
}

public void push(int new_data)
Expand Down Expand Up @@ -50,4 +57,4 @@ public static void main(String [] args)
llist.printMiddle();
}
}
}
}
48 changes: 45 additions & 3 deletions Exercise_4.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,57 @@ 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 p1 = 0;
int p2 = 0;
int n1 = m-l+1;
int n2 = r-m;
int[] temp1 = new int[n1];
int[] temp2 = new int[n2];

for(int i=0; i<n1;i++){
temp1[i] = arr[l+i];
}
for(int i=0; i<n2; i++){
temp2[i] = arr[m+1+i];
}
int i = l;
while(p1<n1 && p2<n2) {
if(temp1[p1]< temp2[p2]){
arr[i] = temp1[p1];
i++;
p1++;
} else {
arr[i] = temp2[p2];
i++;
p2++;
}
}
while(p1<n1){
arr[i] = temp1[p1];
i++;
p1++;
}
while(p2<n2){
arr[i] = temp2[p2];
i++;
p2++;
}
}

// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
//Write your code here
//Call mergeSort from here
//Call mergeSort from here
if(l >= r) {
return;
}
int middle = l + (r-l)/2;
sort(arr, l, middle);
sort(arr, middle+1, r);
merge(arr, l, middle, r);
}

/* A utility function to print array of size n */
Expand All @@ -39,4 +81,4 @@ public static void main(String args[])
System.out.println("\nSorted array");
printArray(arr);
}
}
}
41 changes: 39 additions & 2 deletions Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,57 @@
class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
//Try swapping without extra variable
if(arr[i] == arr[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.
int[] stack = new int[h-l+1];

stack[0] = l;
stack[1] = h;
int top = 1;
while(top>=0) {
h = stack[top--];
l = stack[top--];

int p = partition(arr, l, h);
if(p-1>l){
stack[++top] = l;
stack[++top] = p-1;
}
if(p+1<h){
stack[++top] = p+1;
stack[++top] =h;
}

}

}

// A utility function to print contents of arr
Expand All @@ -33,4 +70,4 @@ public static void main(String args[])
ob.QuickSort(arr, 0, arr.length - 1);
ob.printArr(arr, arr.length);
}
}
}