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 PreCourse2 #1671

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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 23 additions & 6 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
class BinarySearch {
// Time Complexity : O(log n) where n is the size of the array
// Space Complexity : O(1)
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)
{
int binarySearch(int arr[], int l, int r, int x)
{
//Write your code here
}
int mid = 0;
while(r >= l) {
//calculate middle index
mid = l+(r-l)/2;
// found the element return the index
if(arr[mid] == x) return mid;
// if target element is bigger than the middle element, search on the right half
if(arr[mid] < x)
l = mid+1;
// else search on the left half
else
r = mid-1;
}

return -1; //Element not present
}

// Driver method to test above
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int x = 10;
int result = ob.binarySearch(arr, 0, n - 1, x);
if (result == -1)
System.out.println("Element not present");
Expand Down
118 changes: 75 additions & 43 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,80 @@
class QuickSort
{
/* This function takes last element as pivot,
places the pivot element at its correct
position in sorted array, and places all
smaller (smaller than pivot) to left of
pivot and all greater elements to right
// Time Complexity :
// Best/Average Case: O(n log n) where n is the size of the array
// Worst Case: O(n^2) , when pivot is always the smallest or largest element)

// Space Complexity: O(log n) , due to recursion call
class QuickSort
{
/* This function takes last element as pivot,
places the pivot element at its correct
position in sorted array, and places all
smaller (smaller than pivot) to left of
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
}
/* The main function that implements QuickSort()
arr[] --> Array to be sorted,
low --> Starting index,

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 = i+1 ;j < high; j++) {
//if element is smaller or equal to pivot, swap
if(arr[j] <= pivot) {
swap(arr, ++i, j);
}
}
// final swap with the pivot
swap(arr, i+1, high);

// returns partition index
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
}


void sort(int arr[], int low, int high)
{
// Recursively sort elements before
// partition and after partition
if(low < high){
int partIndex = partition(arr, low, high);

sort(arr, low , partIndex-1);
sort(arr, partIndex+1 ,high );
}

}


/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
// Driver program
public static void main(String args[])
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = arr.length;
QuickSort ob = new QuickSort();
ob.sort(arr, 0, n-1);
System.out.println("sorted array");
printArray(arr);
}
}
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}

// Driver program
public static void main(String args[])
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = arr.length;

QuickSort ob = new QuickSort();
ob.sort(arr, 0, n-1);

System.out.println("sorted array");
printArray(arr);
}
}
104 changes: 57 additions & 47 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,63 @@
class LinkedList
{
Node head; // head of linked list

// Time Complexity : O(n) , where n is the size of the linked list
// Space Complexity: O(1)
class LinkedList
{
Node head; // head of linked list

/* Linked list node */
class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}

/* Function to print middle of linked list */
//Complete this function
void printMiddle()
{
void printMiddle()
{
//Write your code here
//Implement using Fast and slow pointers
}

public void push(int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
Node slow = head;
Node fast = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
System.out.println("Middle Node: " + slow.data);
}


public void push(int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}

public void printList()
{
Node tnode = head;
while (tnode != null)
{
System.out.print(tnode.data+"->");
tnode = tnode.next;
}
System.out.println("NULL");
}

public void printList()
{
Node tnode = head;
while (tnode != null)
{
System.out.print(tnode.data+"->");
tnode = tnode.next;
}
System.out.println("NULL");
}

public static void main(String [] args)
{
LinkedList llist = new LinkedList();
for (int i=15; i>0; --i)
{
llist.push(i);
llist.printList();
llist.printMiddle();
}
}
}
public static void main(String [] args)
{
LinkedList llist = new LinkedList();
for (int i=15; i>0; --i)
{
llist.push(i);
llist.printList();
llist.printMiddle();
}
}
}
Loading