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 code commit #1599

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
42 changes: 41 additions & 1 deletion Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
// Time Complexity : O(log n)
// Space Complexity :O(1) as we are not creating an extra array to store
// Did this code successfully run on Leetcode : I didn't find similar question on leetcode
// Any problem you faced while coding this : No




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
boolean isAsc = arr[l] < arr[r]; // It is checking if the array is sorted in ascending or Descending manner
if(arr[l] < arr[r]){
isAsc=true;
}
else{
isAsc=false;
}

while(l<=r){ //Base condition if l > r then the element will not be present
int mid=l+(r-l)/2;
if(arr[mid]==x){ //If the mid is the target element then return it and no further execution
return mid;
}
else{
if(isAsc){ //If array is sorted in ascending order
if(x < arr[mid]){
r=mid-1;
}
else{
l=mid+1;
}
}
else{ //If array is sorted in descending order
if(x < arr[mid]){
l=mid+1;
}
else{
r=mid-1;
}
}
}
}
return -1; //If the target element is not present
}

// Driver method to test above
public static void main(String args[])
{
Expand Down
39 changes: 34 additions & 5 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Time Complexity : O(n log n)
// Space Complexity : Not sure
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this : Yes, I was not sure how to implement it and I refereed few videos but still i was unable to do it on my own

class QuickSort
{
/* This function takes last element as pivot,
Expand All @@ -6,26 +11,50 @@ class QuickSort
smaller (smaller than pivot) to left of
pivot and all greater elements to right
of pivot */
void swap(int arr[],int i,int j){
void swap(int arr[],int i,int j){ //Swap logic to swap two elements using a temp variable
//Your code here
int temp = arr[i];
arr[i]= arr[j];
arr[j]=temp;
}

int partition(int arr[], int low, int high)
int partition(int arr[], int low, int high) //this is a partition function which compares every element with the pivot and places small elements than pivot at left and greater elements to the right of pivot and returns pivot
{

int pivot = arr[high];
int x=low-1; //x is the pointer which we are maintaining and it will return the correct position of the pivot

for(int i=low;i<high;i++){ //the loop will be low to high and then at the end we will swap the pivot
if(arr[i]<pivot){
x++;
swap(arr,x,i);
}
}
x++;
swap(arr,x,high);
return x;


//Write code here for Partition and Swap
}
/* 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)
{
void sort(int arr[], int low, int high) //This is the sort function which is recursively called based on the pivot.
{ //for example if there are 10 elements and pivot is the sixth element then sort function will be recursively called for 0 to 5 index and 7-10 element and so on.

if(low<high){
int pivotIndex = partition(arr,low,high); //pivotIndex will be the index of the pivot
sort(arr,low,pivotIndex-1);
sort(arr,pivotIndex+1,high);
}
// Recursively sort elements before
// partition and after partition
}

/* A utility function to print array of size n */
static void printArray(int arr[])
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
Expand Down
17 changes: 17 additions & 0 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Time Complexity : O(n) -> I think it should be o(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this : I faced issue while finding out the end condition when the loop should stop

class LinkedList
{
Node head; // head of linked list
Expand All @@ -18,8 +23,20 @@ class Node
//Complete this function
void printMiddle()
{
Node slow=head; //Two pointers one slow and one fast
Node fast=head;


while(fast!=null && fast.next!=null){ //Condition till when the loop should iterate considering both even and odd number of nodes
fast=fast.next.next; //The fast pointer will move twice than slow pointer
slow=slow.next;
}
System.out.println("Middle node is "+slow.data);


//Write your code here
//Implement using Fast and slow pointers

}

public void push(int new_data)
Expand Down
43 changes: 43 additions & 0 deletions Exercise_4.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Time Complexity : O(n log(n)) -> I think it should be o(n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this : I referred few videos to so this
class MergeSort
{
// Merges two subarrays of arr[].
Expand All @@ -6,12 +10,51 @@ class MergeSort
void merge(int arr[], int l, int m, int r)
{
//Your code here
int[] merged = new int[r-l+1];
int idx1=l; //pointer to first sub array
int idx2=m+1; //pointer to second sub array
int k=0;
while(idx1<=m && idx2 <=r){
if(arr[idx1] <= arr[idx2] ){
merged[k] = arr[idx1];
k++;
idx1++;
}
else{
merged[k] = arr[idx2];
k++;
idx2++;
}
}
while(idx1<=m){ //loop that will copy all the remaining any elements in the same order as they will be already sorted
merged[k] = arr[idx1];
k++;
idx1++;
}
while(idx2<=r){ //loop that will copy all the remaining any elements in the same order as they will be already sorted
merged[k] = arr[idx2];
k++;
idx2++;
}

for(int i=0, j=l;i<merged.length;i++,j++ ){ //copying the merged array into original array
arr[j]=merged[i];
}
}

// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
if(l>=r){
return;
}
int mid = l+(r-l)/2;


sort(arr,l,mid);
sort(arr,mid+1,r);
merge(arr,l,mid,r);
//Write your code here
//Call mergeSort from here
}
Expand Down
59 changes: 52 additions & 7 deletions Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,68 @@
// Time Complexity : Not Sure
// Space Complexity : Not sure
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this : Yes, I was not able to implement it on my own and understand it.
import java.util.*;
class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
if (i != j) { //it will not swap if i & j are equal
arr[i] = arr[i] + arr[j]; //swapping without using 3rd variable
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)
int partition(int arr[], int l, int h) //partitioning and returning the position of the pivot
{
//Compare elements and swap.
int pivot = arr[h];
int x=l-1; //x is the pointer which we are maintaining and it will return the correct position of the pivot

for(int i=l;i<h;i++){ //the loop will be low to high and then at the end we will swap the pivot
if(arr[i]<pivot){
x++;
swap(arr,x,i);
}
}
x++;
swap(arr,x,h);
return x;
}

// Sorts arr[l..h] using iterative QuickSort
void QuickSort(int arr[], int l, int h)

void QuickSort(int arr[], int l, int h) //I didn't completely understood this part but I understood it recursively. Here we are pushing the range of indexes of left and right of the pivot
{
//Try using Stack Data Structure to remove recursion.
Stack<Integer> stack = new Stack<>();
stack.push(l);
stack.push(h);

while (!stack.isEmpty()) {
int high = stack.pop();
int low = stack.pop();

if (low < high) {
int pi = partition(arr, low, high);

//left side
if (pi - 1 > low) {
stack.push(low);
stack.push(pi - 1);
}

//right side
if (pi + 1 < high) {
stack.push(pi + 1);
stack.push(high);
}
}
}


}

// A utility function to print contents of arr

void printArr(int arr[], int n)
{
int i;
Expand Down