From 63531915053ae135fb5e6f9823b666be125c6cde Mon Sep 17 00:00:00 2001 From: franiachettiar <65803302+franiachettiar@users.noreply.github.com> Date: Tue, 27 May 2025 01:19:49 +0530 Subject: [PATCH] Done --- Exercise_1.cpp | 21 -------------------- Exercise_1.java | 21 -------------------- Exercise_1.js | 16 --------------- Exercise_1.py | 38 +++++++++++++++++++++++++---------- Exercise_2.cpp | 47 ------------------------------------------- Exercise_2.java | 48 -------------------------------------------- Exercise_2.js | 41 -------------------------------------- Exercise_2.py | 17 +++++++++++++++- Exercise_3.cpp | 50 ---------------------------------------------- Exercise_3.java | 53 ------------------------------------------------- Exercise_3.js | 43 --------------------------------------- Exercise_3.py | 29 ++++++++++++++++++++++++++- Exercise_4.cpp | 43 --------------------------------------- Exercise_4.java | 42 --------------------------------------- Exercise_4.js | 34 ------------------------------- Exercise_4.py | 43 +++++++++++++++++++++++++++++++++++++++ Exercise_5.cpp | 42 --------------------------------------- Exercise_5.java | 36 --------------------------------- Exercise_5.js | 36 --------------------------------- Exercise_5.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++ Sample.java | 7 ------- 21 files changed, 165 insertions(+), 592 deletions(-) delete mode 100644 Exercise_1.cpp delete mode 100644 Exercise_1.java delete mode 100644 Exercise_1.js delete mode 100644 Exercise_2.cpp delete mode 100644 Exercise_2.java delete mode 100644 Exercise_2.js delete mode 100644 Exercise_3.cpp delete mode 100644 Exercise_3.java delete mode 100644 Exercise_3.js delete mode 100644 Exercise_4.cpp delete mode 100644 Exercise_4.java delete mode 100644 Exercise_4.js delete mode 100644 Exercise_5.cpp delete mode 100644 Exercise_5.java delete mode 100644 Exercise_5.js delete mode 100644 Sample.java diff --git a/Exercise_1.cpp b/Exercise_1.cpp deleted file mode 100644 index a6dc14cc..00000000 --- a/Exercise_1.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include - -// A recursive binary search function. It returns -// location of x in given array arr[l..r] is present, -// otherwise -1 -int binarySearch(int arr[], int l, int r, int x) -{ - //Your Code here -} - -int main(void) -{ - int arr[] = { 2, 3, 4, 10, 40 }; - int n = sizeof(arr) / sizeof(arr[0]); - int x = 10; - int result = binarySearch(arr, 0, n - 1, x); - (result == -1) ? printf("Element is not present in array") - : printf("Element is present at index %d", - result); - return 0; -} \ No newline at end of file diff --git a/Exercise_1.java b/Exercise_1.java deleted file mode 100644 index c3ff1141..00000000 --- a/Exercise_1.java +++ /dev/null @@ -1,21 +0,0 @@ -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"); - else - System.out.println("Element found at index " + result); - } -} diff --git a/Exercise_1.js b/Exercise_1.js deleted file mode 100644 index 89d853ec..00000000 --- a/Exercise_1.js +++ /dev/null @@ -1,16 +0,0 @@ -class BinarySearch { - // Returns index of x if it is present in arr[l.. r], else return -1 - function binarySearch(arr, l, r, x) { -​ - } -} -// Driver method to test above -const ob = new BinarySearch(); -const arr = [2, 3, 4, 10, 40]; -const n = arr.length; -const x = 10; -const result = ob.binarySearch(arr, 0, n - 1, x); -if (result === -1) - console.log("Element not present"); -else - console.log("Element found at index " + result); diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..71518f62 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,22 +1,40 @@ +"""" +// Time Complexity :O( log n) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : + + +// Your code here along with comments explaining your approach + + +""" + + # Python code to implement iterative Binary # Search. # It returns location of x in given array arr # if present, else returns -1 def binarySearch(arr, l, r, x): - - #write your code here - - - + while l <= r: + mid = l + (r - l) // 2 # Avoids overflow compared to (l + r) // 2 + if arr[mid] == x: + return mid + elif arr[mid] < x: + l = mid + 1 + else: + r = mid - 1 + return -1 + # Test array -arr = [ 2, 3, 4, 10, 40 ] +arr = [2, 3, 4, 10, 40] x = 10 - + # Function call result = binarySearch(arr, 0, len(arr)-1, x) - + if result != -1: - print "Element is present at index % d" % result + print("Element is present at index %d" % result) else: - print "Element is not present in array" + print("Element is not present in array") \ No newline at end of file diff --git a/Exercise_2.cpp b/Exercise_2.cpp deleted file mode 100644 index c90e577e..00000000 --- a/Exercise_2.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include -using namespace std; - -// A utility function to swap two elements -void swap(int* a, int* b) -{ - //Your Code here -} - -/* 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 */ -int partition (int arr[], int low, int high) -{ - //Your Code here -} - -/* The main function that implements QuickSort -arr[] --> Array to be sorted, -low --> Starting index, -high --> Ending index */ -void quickSort(int arr[], int low, int high) -{ - //Your Code here -} - -/* Function to print an array */ -void printArray(int arr[], int size) -{ - int i; - for (i = 0; i < size; i++) - cout << arr[i] << " "; - cout << endl; -} - -// Driver Code -int main() -{ - int arr[] = {10, 7, 8, 9, 1, 5}; - int n = sizeof(arr) / sizeof(arr[0]); - quickSort(arr, 0, n - 1); - cout << "Sorted array: \n"; - printArray(arr, n); - return 0; -} \ No newline at end of file diff --git a/Exercise_2.java b/Exercise_2.java deleted file mode 100644 index d0b5fa5f..00000000 --- a/Exercise_2.java +++ /dev/null @@ -1,48 +0,0 @@ -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 - } - - 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 - } - - /* A utility function to print array of size n */ - static void printArray(int arr[]) - { - int n = arr.length; - for (int i=0; i Array to be sorted, - low --> Starting index, - high --> Ending index */ - function sort(arr, low, high) { - // Recursively sort elements before - // partition and after partition - } -​ - /* A utility function to print array of size n */ - function printArray(arr) { - let n = arr.length; - for (let i = 0; i < n; ++i) - console.log(arr[i] + " "); - console.log(); - } -} - // Driver program - let arr = [10, 7, 8, 9, 1, 5]; - let n = arr.length; - let ob = new QuickSort(); - ob.sort(arr, 0, n - 1); - console.log("sorted array"); - ob.printArray(arr); diff --git a/Exercise_2.py b/Exercise_2.py index 35abf0dd..b9651190 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,14 +1,29 @@ + # Python program for implementation of Quicksort Sort # give you explanation for the approach def partition(arr,low,high): + pivot = arr[high] # Last element as pivot + i = low - 1 # Index of smaller element + + for j in range(low, high): + if arr[j] <= pivot: + i += 1 + arr[i], arr[j] = arr[j], arr[i] # Swap + + arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in the middle + return i + 1 #write your code here # Function to do Quick sort -def quickSort(arr,low,high): +def quickSort(arr,low,high): + if low < high: + pi = partition(arr, low, high) + quickSort(arr, low, pi - 1) + quickSort(arr, pi + 1, high) #write your code here diff --git a/Exercise_3.cpp b/Exercise_3.cpp deleted file mode 100644 index 209ce0fe..00000000 --- a/Exercise_3.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include -using namespace std; - -// Struct -struct Node -{ - int data; - struct Node* next; -}; - -/* Function to get the middle of the linked list*/ -void printMiddle(struct Node *head) -{ - //YourCode here - //Use fast and slow pointer technique -} - -// Function to add a new node -void push(struct Node** head_ref, int new_data) -{ - struct Node* new_node = new Node; - new_node->data = new_data; - new_node->next = (*head_ref); - (*head_ref) = new_node; -} - -// A utility function to print a given linked list -void printList(struct Node *ptr) -{ - while (ptr != NULL) - { - printf("%d->", ptr->data); - ptr = ptr->next; - } - printf("NULL\n"); -} - -// Driver Code -int main() -{ - struct Node* head = NULL; - for (int i=15; i>0; i--) - { - push(&head, i); - printList(head); - printMiddle(head); - } - - return 0; -} \ No newline at end of file diff --git a/Exercise_3.java b/Exercise_3.java deleted file mode 100644 index 1f9b752a..00000000 --- a/Exercise_3.java +++ /dev/null @@ -1,53 +0,0 @@ -class LinkedList -{ - Node head; // head of linked list - - /* Linked list node */ - 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(); - } - } -} \ No newline at end of file diff --git a/Exercise_3.js b/Exercise_3.js deleted file mode 100644 index 4c1eabdd..00000000 --- a/Exercise_3.js +++ /dev/null @@ -1,43 +0,0 @@ -class LinkedList { - constructor() { - this.head = null; // head of linked list - } -​ - /* Linked list node */ - static Node = class { - constructor(d) { - //Constructor here - this.data = d; - this.next = null; - } - } -​ - /* Function to print middle of linked list */ - //Complete this function - function printMiddle() { - //Write your code here - //Implement using Fast and slow pointers - } -​ - function push(new_data) { - let new_node = new this.Node(new_data); - new_node.next = this.head; - this.head = new_node; - } -​ - function printList() { - let tnode = this.head; - while (tnode != null) { - console.log(tnode.data + "->"); - tnode = tnode.next; - } - console.log("NULL"); - } -} -​ -let llist = new LinkedList(); -for (let i = 15; i > 0; --i) { - llist.push(i); - llist.printList(); - llist.printMiddle(); -} diff --git a/Exercise_3.py b/Exercise_3.py index a26a69b8..0fabbe6e 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,20 +1,47 @@ + +"""" +// Time Complexity :O(n) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : + + +// Your code here along with comments explaining your approach + + +""" # Node class class Node: # Function to initialise the node object - def __init__(self, data): + def __init__(self, data): + self.data = data + self.next = None class LinkedList: def __init__(self): + self.head = None def push(self, new_data): + new_node = Node(new_data) + new_node.next = self.head + self.head = new_node # Function to get the middle of # the linked list def printMiddle(self): + slow = self.head + fast = self.head + + while fast and fast.next: + slow = slow.next + fast = fast.next.next + + if slow: + print("The middle element is:", slow.data) # Driver code list1 = LinkedList() diff --git a/Exercise_4.cpp b/Exercise_4.cpp deleted file mode 100644 index 1a528ee6..00000000 --- a/Exercise_4.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include -#include - -// Merges two subarrays of arr[]. -// First subarray is arr[l..m] -// Second subarray is arr[m+1..r] -void merge(int arr[], int l, int m, int r) -{ - //Your code here -} - -/* l is for left index and r is right index of the - sub-array of arr to be sorted */ -void mergeSort(int arr[], int l, int r) -{ - //Your code here -} - -/* UTILITY FUNCTIONS */ -/* Function to print an array */ -void printArray(int A[], int size) -{ - int i; - for (i=0; i < size; i++) - printf("%d ", A[i]); - printf("\n"); -} - -/* Driver program to test above functions */ -int main() -{ - int arr[] = {12, 11, 13, 5, 6, 7}; - int arr_size = sizeof(arr)/sizeof(arr[0]); - - printf("Given array is \n"); - printArray(arr, arr_size); - - mergeSort(arr, 0, arr_size - 1); - - printf("\nSorted array is \n"); - printArray(arr, arr_size); - return 0; -} \ No newline at end of file diff --git a/Exercise_4.java b/Exercise_4.java deleted file mode 100644 index 81afd3c2..00000000 --- a/Exercise_4.java +++ /dev/null @@ -1,42 +0,0 @@ -class MergeSort -{ - // Merges two subarrays of arr[]. - // First subarray is arr[l..m] - // Second subarray is arr[m+1..r] - void merge(int arr[], int l, int m, int r) - { - //Your code here - } - - // Main function that sorts arr[l..r] using - // merge() - void sort(int arr[], int l, int r) - { - //Write your code here - //Call mergeSort from here - } - - /* A utility function to print array of size n */ - static void printArray(int arr[]) - { - int n = arr.length; - for (int i=0; i 1: + mid = len(arr) // 2 + L = arr[:mid] + R = arr[mid:] + + mergeSort(L) + mergeSort(R) + + i = j = k = 0 + + while i < len(L) and j < len(R): + if L[i] < R[j]: + arr[k] = L[i] + i += 1 + else: + arr[k] = R[j] + j += 1 + k += 1 + + while i < len(L): + arr[k] = L[i] + i += 1 + k += 1 + + while j < len(R): + arr[k] = R[j] + j += 1 + k += 1 #write your code here # Code to print the list def printList(arr): + for i in arr: + print(i, end=" ") + print() #write your code here diff --git a/Exercise_5.cpp b/Exercise_5.cpp deleted file mode 100644 index a07c2bf6..00000000 --- a/Exercise_5.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include -using namespace std; - -// A utility function to swap two elements -void swap(int* a, int* b) -{ - int t = *a; - *a = *b; - *b = t; -} - -/* This function is same in both iterative and recursive*/ -int partition(int arr[], int l, int h) -{ - //Do the comparison and swapping here -} - -/* A[] --> Array to be sorted, -l --> Starting index, -h --> Ending index */ -void quickSortIterative(int arr[], int l, int h) -{ - //Try to think that how you can use stack here to remove recursion. -} - -// A utility function to print contents of arr -void printArr(int arr[], int n) -{ - int i; - for (i = 0; i < n; ++i) - cout << arr[i] << " "; -} - -// Driver code -int main() -{ - int arr[] = { 4, 3, 5, 2, 1, 3, 2, 3 }; - int n = sizeof(arr) / sizeof(*arr); - quickSortIterative(arr, 0, n - 1); - printArr(arr, n); - return 0; -} \ No newline at end of file diff --git a/Exercise_5.java b/Exercise_5.java deleted file mode 100644 index 30e82675..00000000 --- a/Exercise_5.java +++ /dev/null @@ -1,36 +0,0 @@ -class IterativeQuickSort { - void swap(int arr[], int i, int j) - { - //Try swapping without extra variable - } - - /* This function is same in both iterative and - recursive*/ - int partition(int arr[], int l, int h) - { - //Compare elements and swap. - } - - // Sorts arr[l..h] using iterative QuickSort - void QuickSort(int arr[], int l, int h) - { - //Try using Stack Data Structure to remove recursion. - } - - // A utility function to print contents of arr - void printArr(int arr[], int n) - { - int i; - for (i = 0; i < n; ++i) - System.out.print(arr[i] + " "); - } - - // Driver code to test above - public static void main(String args[]) - { - IterativeQuickSort ob = new IterativeQuickSort(); - int arr[] = { 4, 3, 5, 2, 1, 3, 2, 3 }; - ob.QuickSort(arr, 0, arr.length - 1); - ob.printArr(arr, arr.length); - } -} \ No newline at end of file diff --git a/Exercise_5.js b/Exercise_5.js deleted file mode 100644 index 01fb9e63..00000000 --- a/Exercise_5.js +++ /dev/null @@ -1,36 +0,0 @@ -class IterativeQuickSort { -​ - function swap(arr, i, j) { -​ - //Try swapping without extra variable -​ - } -​ - /* This function is same in both iterative and - recursive*/ - function partition(arr, l, h) { -​ - //Compare elements and swap. -​ - } -​ - // Sorts arr[l..h] using iterative QuickSort - function QuickSort(arr, l, h) { -​ - //Try using Stack Data Structure to remove recursion. -​ - } -​ - // A utility function to print contents of arr - function printArr(arr, n) { - let i; - for (i = 0; i < n; ++i) - console.log(arr[i] + " "); - } -} -​ - // Driver code to test above -let ob = new IterativeQuickSort(); -let arr = [4, 3, 5, 2, 1, 3, 2, 3]; -ob.QuickSort(arr, 0, arr.length - 1); -ob.printArr(arr, arr.length); diff --git a/Exercise_5.py b/Exercise_5.py index 1da24ffb..44e5eef8 100644 --- a/Exercise_5.py +++ b/Exercise_5.py @@ -1,10 +1,60 @@ +"""" +// Time Complexity : +// Space Complexity : +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : + + +// Your code here along with comments explaining your approach + + +""" + # Python program for implementation of Quicksort # This function is same in both iterative and recursive def partition(arr, l, h): + pivot = arr[h] + i = l - 1 + + for j in range(l, h): + if arr[j] <= pivot: + i += 1 + arr[i], arr[j] = arr[j], arr[i] + + arr[i + 1], arr[h] = arr[h], arr[i + 1] + return i + 1 #write your code here def quickSortIterative(arr, l, h): + size = h - l + 1 + stack = [0] * size + + top = -1 + top += 1 + stack[top] = l + top += 1 + stack[top] = h + + while top >= 0: + h = stack[top] + top -= 1 + l = stack[top] + top -= 1 + + p = partition(arr, l, h) + + if p - 1 > l: + top += 1 + stack[top] = l + top += 1 + stack[top] = p - 1 + + if p + 1 < h: + top += 1 + stack[top] = p + 1 + top += 1 + stack[top] = h #write your code here diff --git a/Sample.java b/Sample.java deleted file mode 100644 index 1739a9cb..00000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach