From 9b4a83cdfa86aee2d3522d93dbea2577b52680d3 Mon Sep 17 00:00:00 2001 From: sneha-tambade Date: Mon, 22 Dec 2025 18:39:03 -0800 Subject: [PATCH] Progress: Commited Exercise 1-4 --- Exercise_1.cpp | 21 --------- Exercise_1.cs | 47 +++++++++++++++++++ Exercise_1.java | 21 --------- Exercise_1.js | 16 ------- Exercise_1.py | 22 --------- Exercise_2.cpp | 47 ------------------- Exercise_2.cs | 72 ++++++++++++++++++++++++++++ Exercise_2.java | 48 ------------------- Exercise_2.js | 41 ---------------- Exercise_2.py | 23 --------- Exercise_3.cpp | 50 -------------------- Exercise_3.cs | 66 ++++++++++++++++++++++++++ Exercise_3.java | 53 --------------------- Exercise_3.js | 43 ----------------- Exercise_3.py | 26 ----------- Exercise_4.cpp | 43 ----------------- Exercise_4.cs | 80 ++++++++++++++++++++++++++++++++ Exercise_4.java | 42 ----------------- Exercise_4.js | 34 -------------- Exercise_4.py | 18 ------- Exercise_5.cpp | 42 ----------------- Exercise_5.java => Exercise_5.cs | 14 +++--- Exercise_5.js | 36 -------------- Exercise_5.py | 10 ---- 24 files changed, 272 insertions(+), 643 deletions(-) delete mode 100644 Exercise_1.cpp create mode 100644 Exercise_1.cs delete mode 100644 Exercise_1.java delete mode 100644 Exercise_1.js delete mode 100644 Exercise_1.py delete mode 100644 Exercise_2.cpp create mode 100644 Exercise_2.cs delete mode 100644 Exercise_2.java delete mode 100644 Exercise_2.js delete mode 100644 Exercise_2.py delete mode 100644 Exercise_3.cpp create mode 100644 Exercise_3.cs delete mode 100644 Exercise_3.java delete mode 100644 Exercise_3.js delete mode 100644 Exercise_3.py delete mode 100644 Exercise_4.cpp create mode 100644 Exercise_4.cs delete mode 100644 Exercise_4.java delete mode 100644 Exercise_4.js delete mode 100644 Exercise_4.py delete mode 100644 Exercise_5.cpp rename Exercise_5.java => Exercise_5.cs (65%) delete mode 100644 Exercise_5.js delete mode 100644 Exercise_5.py 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.cs b/Exercise_1.cs new file mode 100644 index 00000000..4c5415b6 --- /dev/null +++ b/Exercise_1.cs @@ -0,0 +1,47 @@ + +// Time Complexity +// Best Case - O(1) +// Average Case - O(log n) +// Worst Case - O(log n) + +// Space Complexity +// O(1) +public class BinarySearch +{ + // Returns index of x if it is present in arr[l.. r], else return -1 + public int binarySearch(int[] arr, int l, int r, int x) + { + //Write your code here + while (l <= r) + { + int mid = l + (r - l) / 2; + if (arr[mid] == x) + { + return mid; + } + else if (arr[mid] > x) + { + r = mid - 1; + } + else + { + l = mid + 1; + } + } + return -1; + } + + // 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) + Console.WriteLine("Element not present"); + else + Console.WriteLine("Element found at index " + result); + } +} 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 deleted file mode 100644 index 3e6adcf4..00000000 --- a/Exercise_1.py +++ /dev/null @@ -1,22 +0,0 @@ -# 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 - - - -# Test array -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 -else: - print "Element is not present in array" 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.cs b/Exercise_2.cs new file mode 100644 index 00000000..95e63a03 --- /dev/null +++ b/Exercise_2.cs @@ -0,0 +1,72 @@ +//Time Complexity = O(nlogn) +//Space Complexity = O(logn) +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 = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + + 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 = low; j < high; j++) + { + if (arr[j] < pivot) + { + i++; + swap(arr, i, j); + } + } + swap(arr, i + 1, high); + return i + 1; + } + //{10, 7, 8, 9, 1, 5}; + //1,5,7,8,9,10 + /* 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 + if (low < high) + { + int pivot = partition(arr, low, high); + sort(arr, low, pivot - 1); + 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) + Console.WriteLine(arr[i] + " "); + } + + // 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); + + Console.WriteLine("sorted array"); + printArray(arr); + } +} 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 deleted file mode 100644 index 35abf0dd..00000000 --- a/Exercise_2.py +++ /dev/null @@ -1,23 +0,0 @@ -# Python program for implementation of Quicksort Sort - -# give you explanation for the approach -def partition(arr,low,high): - - - #write your code here - - -# Function to do Quick sort -def quickSort(arr,low,high): - - #write your code here - -# Driver code to test above -arr = [10, 7, 8, 9, 1, 5] -n = len(arr) -quickSort(arr,0,n-1) -print ("Sorted array is:") -for i in range(n): - print ("%d" %arr[i]), - - 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.cs b/Exercise_3.cs new file mode 100644 index 00000000..71b07343 --- /dev/null +++ b/Exercise_3.cs @@ -0,0 +1,66 @@ +//Time Complexity = O(n) +//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; + } + } + + /* Function to print middle of linked list */ + //Complete this function + void printMiddle() + { + //Write your code here + //Implement using Fast and slow pointers + if (head == null) + return; + Node slow = head; + Node fast = head; + while (fast != null && fast.next != null) + { + slow = slow.next; + fast = fast.next.next; + } + Console.WriteLine(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) + { + Console.WriteLine(tnode.data + "->"); + tnode = tnode.next; + } + Console.WriteLine("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.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 deleted file mode 100644 index a26a69b8..00000000 --- a/Exercise_3.py +++ /dev/null @@ -1,26 +0,0 @@ -# Node class -class Node: - - # Function to initialise the node object - def __init__(self, data): - -class LinkedList: - - def __init__(self): - - - def push(self, new_data): - - - # Function to get the middle of - # the linked list - def printMiddle(self): - -# Driver code -list1 = LinkedList() -list1.push(5) -list1.push(4) -list1.push(2) -list1.push(3) -list1.push(1) -list1.printMiddle() 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.cs b/Exercise_4.cs new file mode 100644 index 00000000..6c12efa5 --- /dev/null +++ b/Exercise_4.cs @@ -0,0 +1,80 @@ +//Time Complexity = O(nlogn) +//Space Complexity = O(n) +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 + int n1 = m - l + 1; + int n2 = r - m; + + int[] left = new int[n1]; + int[] right = new int[n2]; + + for (int i = 0; i < n1; i++) + left[i] = arr[l + i]; + + for (int j = 0; j < n2; j++) + right[j] = arr[m + 1 + j]; + + int iIndex = 0, jIndex = 0, k = l; + + while (iIndex < n1 && jIndex < n2) + { + if (left[iIndex] <= right[jIndex]) + arr[k++] = left[iIndex++]; + else + arr[k++] = right[jIndex++]; + } + + while (iIndex < n1) + arr[k++] = left[iIndex++]; + + while (jIndex < n2) + arr[k++] = right[jIndex++]; + } + + // 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 + + if (l < r) + { + int m = l + (r - l) / 2; + sort(arr, l, m); + sort(arr, m + 1, r); + merge(arr, l, m, r); + } + } + + //int[] arr = {12, 11, 13, 5, 6, 7}; + /* 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) + Console.WriteLine(arr[i] + " "); + Console.ReadLine(); + } + + // Driver method + public static void main(String[] args) + { + int[] arr = { 12, 11, 13, 5, 6, 7 }; + + Console.WriteLine("Given Array"); + printArray(arr); + + MergeSort ob = new MergeSort(); + ob.sort(arr, 0, arr.length - 1); + + Console.WriteLine("\nSorted array"); + printArray(arr); + } +} \ 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 -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.cs similarity index 65% rename from Exercise_5.java rename to Exercise_5.cs index 30e82675..82e2f900 100644 --- a/Exercise_5.java +++ b/Exercise_5.cs @@ -1,35 +1,35 @@ class IterativeQuickSort { - void swap(int arr[], int i, int j) + 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) + 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) + 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) + void printArr(int[] arr, int n) { int i; for (i = 0; i < n; ++i) - System.out.print(arr[i] + " "); + Console.WriteLine(arr[i] + " "); } // Driver code to test above - public static void main(String args[]) + public static void Main(string[] args) { IterativeQuickSort ob = new IterativeQuickSort(); - int arr[] = { 4, 3, 5, 2, 1, 3, 2, 3 }; + int[] 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.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 deleted file mode 100644 index 1da24ffb..00000000 --- a/Exercise_5.py +++ /dev/null @@ -1,10 +0,0 @@ -# Python program for implementation of Quicksort - -# This function is same in both iterative and recursive -def partition(arr, l, h): - #write your code here - - -def quickSortIterative(arr, l, h): - #write your code here -