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
Binary file added BinarySearch.class
Binary file not shown.
Binary file added Exercise_1.class
Binary file not shown.
61 changes: 41 additions & 20 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
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
}

// Driver method to test above
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr, 0, n - 1, x);
if (result == -1)
System.out.println("Element not present");
// Time Complexity : O(logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this : No, Have implemented this before so it was easy

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) {
// Using the ittrative approch instead of recursive
int result = -1;
if (arr.length == 0) {
return -1;
}
while (l < r) {
int mid = (l + r) / 2;
if (arr[mid] == x) {
result = mid;
break;
}
if (arr[mid] > x) {
r = mid - 1;
}
if (arr[mid] < x) {
l = mid + 1;
}
}
return result;
}

// Driver method to test above
public static void main(String args[]) {
BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr, 0, n - 1, x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index " + result);
}
}
System.out.println("Element found at index " + result);
}
}
119 changes: 73 additions & 46 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,75 @@
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

// Time Complexity :O( n log n)
// Space Complexity :O(log n)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this : I had trouble warpping my head around partition and recursion but after watch a few videos and reading a few article I was able to write it but i am still not 100% confident on this one

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) {
int temp;
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,
high --> Ending index */
void sort(int arr[], int low, int high)
{
// Recursively sort elements before
// partition and after partition
}


int partition(int arr[], int low, int high) {
int pivot = arr[low];
int i = low - 1;
int j = high + 1;
while (i < j) {
do {
i++;
} while (arr[i] < pivot);
do {
j--;
} while (arr[j] > pivot);
if (i < j) {
swap(arr, i, j);
}
}
swap(arr, low, j);
return j;
}

/*
* 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 pivot = partition(arr, low, high);
sort(arr, low, pivot);
sort(arr, (pivot + 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);
}
}
107 changes: 56 additions & 51 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,58 @@
class LinkedList
{
Node head; // head of linked list

// Time Complexity :O(n)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this : i had never implemented fast and slow pointers, once i searched it online it was pretty easy to understand and implement

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()
{
//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;
}

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();
}
}
}
// Complete this function
void printMiddle() {
Node fast = head;
Node slow = head;
while (fast != null && fast.next != null) {

slow = slow.next;
fast = fast.next.next;
}
System.out.println("------------------------------------");
System.out.println(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 static void main(String[] args) {
LinkedList llist = new LinkedList();
for (int i = 15; i > 0; --i) {
llist.push(i);
llist.printList();
llist.printMiddle();
}
}
}
Loading