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
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path=""/>
<classpathentry kind="output" path=""/>
</classpath>
23 changes: 23 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>PreCourse-2</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>
7 changes: 7 additions & 0 deletions .settings/.jsdtscope
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
<classpathentry kind="src" path=""/>
<classpathentry kind="output" path=""/>
</classpath>
1 change: 1 addition & 0 deletions .settings/org.eclipse.wst.jsdt.ui.superType.container
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.eclipse.wst.jsdt.launching.JRE_CONTAINER
1 change: 1 addition & 0 deletions .settings/org.eclipse.wst.jsdt.ui.superType.name
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Global
Binary file added BinarySearch.class
Binary file not shown.
17 changes: 17 additions & 0 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
class BinarySearch {
// Time Complexity : O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :
// 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) / 2;

if(arr[mid] == x) {
return mid;
} else if(arr[mid] < x) {
l = mid + 1;
} else if(arr[mid] > x) {
r = mid - 1;
}
}

return -1;
}

// Driver method to test above
Expand Down
44 changes: 41 additions & 3 deletions Exercise_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,60 @@ class QuickSort
smaller (smaller than pivot) to left of
pivot and all greater elements to right
of pivot */
// Time Complexity :
// Space Complexity :
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this : difficult to understand its partitions, not sure about how to get time and space complexity
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
// choose the pivot
int pivot = arr[high];

// index of smaller element and indicates
// the right position of pivot found so far
int i = low - 1;

// traverse arr[low to high] and move all smaller
// elements to the left side. Elements from low to
// i are smaller after every iteration
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(arr, i, j);
}
}

// Move pivot after smaller elements and
// return its position
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)
{
// Recursively sort elements before
// partition and after partition
// Recursively sort elements before
// partition and after partition
if (low < high) {

// pi is the partition return index of pivot
int pi = partition(arr, low, high);

// recursion calls for smaller elements
// and greater or equals elements
sort(arr, low, pi - 1);
sort(arr, pi + 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,5 +1,9 @@
class LinkedList
{
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :
Node head; // head of linked list

/* Linked list node */
Expand All @@ -20,6 +24,18 @@ void printMiddle()
{
//Write your code here
//Implement using Fast and slow pointers
Node slowPtr = head;
Node fastPtr = head;

if (head != null)
{
while (fastPtr != null && fastPtr.next != null)
{
fastPtr = fastPtr.next.next;
slowPtr = slowPtr.next;
}
System.out.println("The middle element is [" + slowPtr.data + "]");
}
}

public void push(int new_data)
Expand Down
62 changes: 62 additions & 0 deletions Exercise_4.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,59 @@ class MergeSort
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
// Time Complexity : O(n log n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :
void merge(int arr[], int l, int m, int r)
{
//Your code here
// Find sizes of two sub arrays to be merged
int n1 = m - l + 1;
int n2 = r - m;

// Create temp arrays
int L[] = new int[n1];
int R[] = new int[n2];

// Copy data to temp arrays
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];

// Merge the temp arrays

// Initial indices of first and second subarrays
int i = 0, j = 0;

// Initial index of merged subarray array
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}

// Copy remaining elements of L[] if any
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

// Copy remaining elements of R[] if any
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}

// Main function that sorts arr[l..r] using
Expand All @@ -14,6 +64,18 @@ void sort(int arr[], int l, int r)
{
//Write your code here
//Call mergeSort from here
if (l < r) {

// Find the middle point
int m = l + (r - l) / 2;

// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);

// Merge the sorted halves
merge(arr, l, m, r);
}
}

/* A utility function to print array of size n */
Expand Down
58 changes: 58 additions & 0 deletions Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class IterativeQuickSort {
// Time Complexity : O(n log n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this : difficult to understand through stacks
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
Expand All @@ -9,12 +13,66 @@ void swap(int arr[], int i, int j)
int partition(int arr[], int l, int h)
{
//Compare elements and swap.
int pivot = arr[h];
int i = (l - 1); // index of smaller element
for (int j = l; j <= h - 1; j++) {
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot) {
i++;

// swap
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

// swap arr[i+1] and arr[h] (or pivot)
int temp = arr[i + 1];
arr[i + 1] = arr[h];
arr[h] = temp;

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.
// Create an auxiliary stack
int[] stack = new int[h - l + 1];

int top = -1;

// push initial values of l and h to stack
stack[++top] = l;
stack[++top] = h;

// Keep popping from stack while is not empty
while (top >= 0) {
// Pop h and l
h = stack[top--];
l = stack[top--];

// Set pivot element at its correct position
// in sorted array
int p = partition(arr, l, h);

// If there are elements on left side of pivot,
// then push left side to stack
if (p - 1 > l) {
stack[++top] = l;
stack[++top] = p - 1;
}

// If there are elements on right side of pivot,
// then push right side to stack
if (p + 1 < h) {
stack[++top] = p + 1;
stack[++top] = h;
}
}
}

// A utility function to print contents of arr
Expand Down
Binary file added IterativeQuickSort.class
Binary file not shown.
Binary file added LinkedList$Node.class
Binary file not shown.
Binary file added LinkedList.class
Binary file not shown.
Binary file added MergeSort.class
Binary file not shown.
Binary file added QuickSort.class
Binary file not shown.