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

// Driver method to test above
Expand Down
23 changes: 21 additions & 2 deletions Exercise_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,38 @@ class QuickSort
pivot and all greater elements to right
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 = arr[high];
int i = low - 1;

for(int j = low; j<high;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,
low --> Starting index,
high --> Ending index */
void sort(int arr[], int low, int high)
{
if(low<high){
int pi = partition(arr, low, high);
sort(arr, low, pi-1);
sort(arr, pi+1, high);

}
// Recursively sort elements before
// partition and after partition
}
Expand Down
11 changes: 10 additions & 1 deletion Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class LinkedList
{
Node head; // head of linked list

/* Linked list node */
class Node
{
Expand All @@ -19,7 +19,16 @@ class Node
void printMiddle()
{
//Write your code here
Node slow = head;
Node fast = head;
//Implement using Fast and slow pointers
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
if(slow != null){
System.out.println("Middle elemnt is " + slow.data);
}
}

public void push(int new_data)
Expand Down
55 changes: 54 additions & 1 deletion Exercise_4.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,51 @@ 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 - l +1; //size of left part
int n2 = r - m; //size of right part

int left[] = new int[n1];
int right[] = new int [n2];

//copy left half int left[]
for(int i = 0; i<n1; i++)
left[i] = arr[ l + i ];
//copy right hlaf into right[]
for(int j = 0;j< n2;j++)
right[j] = arr[m+1+j];

int i =0; //pointer for left[]
int j =0; //pointer for right[]
int k =l; //pointer for original arr[]

//compare both arrays and put smaller elemnt first
while(i<n1 && j< n2){
//left =[11,12,13]
//right = [5,6,7]
//comapre 11 and 5
//5 is smaller so put 5 first
if(left[i] <= right[j]){
arr[k] = left[i];
i++;
}else{
arr[k] = right[j];
j++;
}
k++;
}
//if anything is still left in left[], copy it
while(i<n1){
arr[k] = left[i];
i++;
k++;
}
//if anything is still left in riight [], copy it
while(j<n2){
arr[k] = right[j];
j++;
k++;
}
}

// Main function that sorts arr[l..r] using
Expand All @@ -14,6 +58,15 @@ void sort(int arr[], int l, int r)
{
//Write your code here
//Call mergeSort from here
if(l<r){
int m = l+(r-l)/2;
//srt left half
sort(arr, l, m);
//srt right half
sort(arr,m+1, r);
//merge both sorted halves
merge(arr, l, m, r);
}
}

/* A utility function to print array of size n */
Expand Down
58 changes: 57 additions & 1 deletion Exercise_5.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,77 @@ class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
//ex: arr[i] = 4, arr[j] = 3;
//after swap: arr[i] =3, arr[j] =4
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.
//pick last elemnt as pivot
int pivot = arr[h];

//i shows correct position for smaller elemnts
int i = l-1;

for(int j = l;j <= h-1; j++){
if(arr[j] <= pivot){
i++;
swap(arr, i, j);
}
}
//put pivot in its correct position
swap (arr, i+1, h);

//return pivot index
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.
//sort(left side)
//sort right side)

// but here we use stack instead of recurtion.

// think of stack like a notebook
//we write which part of array still needs sorting
int stack[] = new int [h-l+1];
int top = -1;

//push starting and ending index
stack[++top] = l;
stack[++top] = h;

while(top >= 0){
//pop high and low
h = stack[top--];
l = stack[top--];

//put pivot at corect place
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
void printArr(int arr[], int n)
{
Expand Down