├── Exercise_1.cpp ├── Exercise_1.java ├── Exercise_1.js ├── Exercise_1.py ├── Exercise_2.cpp ├── Exercise_2.java ├── Exercise_2.js ├── Exercise_2.py ├── Exercise_3.cpp ├── Exercise_3.java ├── Exercise_3.js ├── Exercise_3.py ├── Exercise_4.cpp ├── Exercise_4.java ├── Exercise_4.js ├── Exercise_4.py ├── Exercise_5.cpp ├── Exercise_5.java ├── Exercise_5.js ├── Exercise_5.py ├── README.md └── Sample.java /Exercise_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // A recursive binary search function. It returns 4 | // location of x in given array arr[l..r] is present, 5 | // otherwise -1 6 | int binarySearch(int arr[], int l, int r, int x) 7 | { 8 | //Your Code here 9 | } 10 | 11 | int main(void) 12 | { 13 | int arr[] = { 2, 3, 4, 10, 40 }; 14 | int n = sizeof(arr) / sizeof(arr[0]); 15 | int x = 10; 16 | int result = binarySearch(arr, 0, n - 1, x); 17 | (result == -1) ? printf("Element is not present in array") 18 | : printf("Element is present at index %d", 19 | result); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Exercise_1.java: -------------------------------------------------------------------------------- 1 | class BinarySearch { 2 | // Returns index of x if it is present in arr[l.. r], else return -1 3 | int binarySearch(int arr[], int l, int r, int x) 4 | { 5 | //Write your code here 6 | } 7 | 8 | // Driver method to test above 9 | public static void main(String args[]) 10 | { 11 | BinarySearch ob = new BinarySearch(); 12 | int arr[] = { 2, 3, 4, 10, 40 }; 13 | int n = arr.length; 14 | int x = 10; 15 | int result = ob.binarySearch(arr, 0, n - 1, x); 16 | if (result == -1) 17 | System.out.println("Element not present"); 18 | else 19 | System.out.println("Element found at index " + result); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Exercise_1.js: -------------------------------------------------------------------------------- 1 | class BinarySearch { 2 | // Returns index of x if it is present in arr[l.. r], else return -1 3 | function binarySearch(arr, l, r, x) { 4 | ​ 5 | } 6 | } 7 | // Driver method to test above 8 | const ob = new BinarySearch(); 9 | const arr = [2, 3, 4, 10, 40]; 10 | const n = arr.length; 11 | const x = 10; 12 | const result = ob.binarySearch(arr, 0, n - 1, x); 13 | if (result === -1) 14 | console.log("Element not present"); 15 | else 16 | console.log("Element found at index " + result); 17 | -------------------------------------------------------------------------------- /Exercise_1.py: -------------------------------------------------------------------------------- 1 | # Python code to implement iterative Binary 2 | # Search. 3 | 4 | # It returns location of x in given array arr 5 | # if present, else returns -1 6 | def binarySearch(arr, l, r, x): 7 | 8 | #write your code here 9 | 10 | 11 | 12 | # Test array 13 | arr = [ 2, 3, 4, 10, 40 ] 14 | x = 10 15 | 16 | # Function call 17 | result = binarySearch(arr, 0, len(arr)-1, x) 18 | 19 | if result != -1: 20 | print "Element is present at index % d" % result 21 | else: 22 | print "Element is not present in array" 23 | -------------------------------------------------------------------------------- /Exercise_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // A utility function to swap two elements 5 | void swap(int* a, int* b) 6 | { 7 | //Your Code here 8 | } 9 | 10 | /* This function takes last element as pivot, places 11 | the pivot element at its correct position in sorted 12 | array, and places all smaller (smaller than pivot) 13 | to left of pivot and all greater elements to right 14 | of pivot */ 15 | int partition (int arr[], int low, int high) 16 | { 17 | //Your Code here 18 | } 19 | 20 | /* The main function that implements QuickSort 21 | arr[] --> Array to be sorted, 22 | low --> Starting index, 23 | high --> Ending index */ 24 | void quickSort(int arr[], int low, int high) 25 | { 26 | //Your Code here 27 | } 28 | 29 | /* Function to print an array */ 30 | void printArray(int arr[], int size) 31 | { 32 | int i; 33 | for (i = 0; i < size; i++) 34 | cout << arr[i] << " "; 35 | cout << endl; 36 | } 37 | 38 | // Driver Code 39 | int main() 40 | { 41 | int arr[] = {10, 7, 8, 9, 1, 5}; 42 | int n = sizeof(arr) / sizeof(arr[0]); 43 | quickSort(arr, 0, n - 1); 44 | cout << "Sorted array: \n"; 45 | printArray(arr, n); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Exercise_2.java: -------------------------------------------------------------------------------- 1 | class QuickSort 2 | { 3 | /* This function takes last element as pivot, 4 | places the pivot element at its correct 5 | position in sorted array, and places all 6 | smaller (smaller than pivot) to left of 7 | pivot and all greater elements to right 8 | of pivot */ 9 | void swap(int arr[],int i,int j){ 10 | //Your code here 11 | } 12 | 13 | int partition(int arr[], int low, int high) 14 | { 15 | //Write code here for Partition and Swap 16 | } 17 | /* The main function that implements QuickSort() 18 | arr[] --> Array to be sorted, 19 | low --> Starting index, 20 | high --> Ending index */ 21 | void sort(int arr[], int low, int high) 22 | { 23 | // Recursively sort elements before 24 | // partition and after partition 25 | } 26 | 27 | /* A utility function to print array of size n */ 28 | static void printArray(int arr[]) 29 | { 30 | int n = arr.length; 31 | for (int i=0; i Array to be sorted, 20 | low --> Starting index, 21 | high --> Ending index */ 22 | function sort(arr, low, high) { 23 | // Recursively sort elements before 24 | // partition and after partition 25 | } 26 | ​ 27 | /* A utility function to print array of size n */ 28 | function printArray(arr) { 29 | let n = arr.length; 30 | for (let i = 0; i < n; ++i) 31 | console.log(arr[i] + " "); 32 | console.log(); 33 | } 34 | } 35 | // Driver program 36 | let arr = [10, 7, 8, 9, 1, 5]; 37 | let n = arr.length; 38 | let ob = new QuickSort(); 39 | ob.sort(arr, 0, n - 1); 40 | console.log("sorted array"); 41 | ob.printArray(arr); 42 | -------------------------------------------------------------------------------- /Exercise_2.py: -------------------------------------------------------------------------------- 1 | # Python program for implementation of Quicksort Sort 2 | 3 | # give you explanation for the approach 4 | def partition(arr,low,high): 5 | 6 | 7 | #write your code here 8 | 9 | 10 | # Function to do Quick sort 11 | def quickSort(arr,low,high): 12 | 13 | #write your code here 14 | 15 | # Driver code to test above 16 | arr = [10, 7, 8, 9, 1, 5] 17 | n = len(arr) 18 | quickSort(arr,0,n-1) 19 | print ("Sorted array is:") 20 | for i in range(n): 21 | print ("%d" %arr[i]), 22 | 23 | 24 | -------------------------------------------------------------------------------- /Exercise_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Struct 5 | struct Node 6 | { 7 | int data; 8 | struct Node* next; 9 | }; 10 | 11 | /* Function to get the middle of the linked list*/ 12 | void printMiddle(struct Node *head) 13 | { 14 | //YourCode here 15 | //Use fast and slow pointer technique 16 | } 17 | 18 | // Function to add a new node 19 | void push(struct Node** head_ref, int new_data) 20 | { 21 | struct Node* new_node = new Node; 22 | new_node->data = new_data; 23 | new_node->next = (*head_ref); 24 | (*head_ref) = new_node; 25 | } 26 | 27 | // A utility function to print a given linked list 28 | void printList(struct Node *ptr) 29 | { 30 | while (ptr != NULL) 31 | { 32 | printf("%d->", ptr->data); 33 | ptr = ptr->next; 34 | } 35 | printf("NULL\n"); 36 | } 37 | 38 | // Driver Code 39 | int main() 40 | { 41 | struct Node* head = NULL; 42 | for (int i=15; i>0; i--) 43 | { 44 | push(&head, i); 45 | printList(head); 46 | printMiddle(head); 47 | } 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Exercise_3.java: -------------------------------------------------------------------------------- 1 | class LinkedList 2 | { 3 | Node head; // head of linked list 4 | 5 | /* Linked list node */ 6 | class Node 7 | { 8 | int data; 9 | Node next; 10 | Node(int d) 11 | { 12 | data = d; 13 | next = null; 14 | } 15 | } 16 | 17 | /* Function to print middle of linked list */ 18 | //Complete this function 19 | void printMiddle() 20 | { 21 | //Write your code here 22 | //Implement using Fast and slow pointers 23 | } 24 | 25 | public void push(int new_data) 26 | { 27 | Node new_node = new Node(new_data); 28 | new_node.next = head; 29 | head = new_node; 30 | } 31 | 32 | public void printList() 33 | { 34 | Node tnode = head; 35 | while (tnode != null) 36 | { 37 | System.out.print(tnode.data+"->"); 38 | tnode = tnode.next; 39 | } 40 | System.out.println("NULL"); 41 | } 42 | 43 | public static void main(String [] args) 44 | { 45 | LinkedList llist = new LinkedList(); 46 | for (int i=15; i>0; --i) 47 | { 48 | llist.push(i); 49 | llist.printList(); 50 | llist.printMiddle(); 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /Exercise_3.js: -------------------------------------------------------------------------------- 1 | class LinkedList { 2 | constructor() { 3 | this.head = null; // head of linked list 4 | } 5 | ​ 6 | /* Linked list node */ 7 | static Node = class { 8 | constructor(d) { 9 | //Constructor here 10 | this.data = d; 11 | this.next = null; 12 | } 13 | } 14 | ​ 15 | /* Function to print middle of linked list */ 16 | //Complete this function 17 | function printMiddle() { 18 | //Write your code here 19 | //Implement using Fast and slow pointers 20 | } 21 | ​ 22 | function push(new_data) { 23 | let new_node = new this.Node(new_data); 24 | new_node.next = this.head; 25 | this.head = new_node; 26 | } 27 | ​ 28 | function printList() { 29 | let tnode = this.head; 30 | while (tnode != null) { 31 | console.log(tnode.data + "->"); 32 | tnode = tnode.next; 33 | } 34 | console.log("NULL"); 35 | } 36 | } 37 | ​ 38 | let llist = new LinkedList(); 39 | for (let i = 15; i > 0; --i) { 40 | llist.push(i); 41 | llist.printList(); 42 | llist.printMiddle(); 43 | } 44 | -------------------------------------------------------------------------------- /Exercise_3.py: -------------------------------------------------------------------------------- 1 | # Node class 2 | class Node: 3 | 4 | # Function to initialise the node object 5 | def __init__(self, data): 6 | 7 | class LinkedList: 8 | 9 | def __init__(self): 10 | 11 | 12 | def push(self, new_data): 13 | 14 | 15 | # Function to get the middle of 16 | # the linked list 17 | def printMiddle(self): 18 | 19 | # Driver code 20 | list1 = LinkedList() 21 | list1.push(5) 22 | list1.push(4) 23 | list1.push(2) 24 | list1.push(3) 25 | list1.push(1) 26 | list1.printMiddle() 27 | -------------------------------------------------------------------------------- /Exercise_4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Merges two subarrays of arr[]. 5 | // First subarray is arr[l..m] 6 | // Second subarray is arr[m+1..r] 7 | void merge(int arr[], int l, int m, int r) 8 | { 9 | //Your code here 10 | } 11 | 12 | /* l is for left index and r is right index of the 13 | sub-array of arr to be sorted */ 14 | void mergeSort(int arr[], int l, int r) 15 | { 16 | //Your code here 17 | } 18 | 19 | /* UTILITY FUNCTIONS */ 20 | /* Function to print an array */ 21 | void printArray(int A[], int size) 22 | { 23 | int i; 24 | for (i=0; i < size; i++) 25 | printf("%d ", A[i]); 26 | printf("\n"); 27 | } 28 | 29 | /* Driver program to test above functions */ 30 | int main() 31 | { 32 | int arr[] = {12, 11, 13, 5, 6, 7}; 33 | int arr_size = sizeof(arr)/sizeof(arr[0]); 34 | 35 | printf("Given array is \n"); 36 | printArray(arr, arr_size); 37 | 38 | mergeSort(arr, 0, arr_size - 1); 39 | 40 | printf("\nSorted array is \n"); 41 | printArray(arr, arr_size); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Exercise_4.java: -------------------------------------------------------------------------------- 1 | class MergeSort 2 | { 3 | // Merges two subarrays of arr[]. 4 | // First subarray is arr[l..m] 5 | // Second subarray is arr[m+1..r] 6 | void merge(int arr[], int l, int m, int r) 7 | { 8 | //Your code here 9 | } 10 | 11 | // Main function that sorts arr[l..r] using 12 | // merge() 13 | void sort(int arr[], int l, int r) 14 | { 15 | //Write your code here 16 | //Call mergeSort from here 17 | } 18 | 19 | /* A utility function to print array of size n */ 20 | static void printArray(int arr[]) 21 | { 22 | int n = arr.length; 23 | for (int i=0; i 2 | using namespace std; 3 | 4 | // A utility function to swap two elements 5 | void swap(int* a, int* b) 6 | { 7 | int t = *a; 8 | *a = *b; 9 | *b = t; 10 | } 11 | 12 | /* This function is same in both iterative and recursive*/ 13 | int partition(int arr[], int l, int h) 14 | { 15 | //Do the comparison and swapping here 16 | } 17 | 18 | /* A[] --> Array to be sorted, 19 | l --> Starting index, 20 | h --> Ending index */ 21 | void quickSortIterative(int arr[], int l, int h) 22 | { 23 | //Try to think that how you can use stack here to remove recursion. 24 | } 25 | 26 | // A utility function to print contents of arr 27 | void printArr(int arr[], int n) 28 | { 29 | int i; 30 | for (i = 0; i < n; ++i) 31 | cout << arr[i] << " "; 32 | } 33 | 34 | // Driver code 35 | int main() 36 | { 37 | int arr[] = { 4, 3, 5, 2, 1, 3, 2, 3 }; 38 | int n = sizeof(arr) / sizeof(*arr); 39 | quickSortIterative(arr, 0, n - 1); 40 | printArr(arr, n); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Exercise_5.java: -------------------------------------------------------------------------------- 1 | class IterativeQuickSort { 2 | void swap(int arr[], int i, int j) 3 | { 4 | //Try swapping without extra variable 5 | } 6 | 7 | /* This function is same in both iterative and 8 | recursive*/ 9 | int partition(int arr[], int l, int h) 10 | { 11 | //Compare elements and swap. 12 | } 13 | 14 | // Sorts arr[l..h] using iterative QuickSort 15 | void QuickSort(int arr[], int l, int h) 16 | { 17 | //Try using Stack Data Structure to remove recursion. 18 | } 19 | 20 | // A utility function to print contents of arr 21 | void printArr(int arr[], int n) 22 | { 23 | int i; 24 | for (i = 0; i < n; ++i) 25 | System.out.print(arr[i] + " "); 26 | } 27 | 28 | // Driver code to test above 29 | public static void main(String args[]) 30 | { 31 | IterativeQuickSort ob = new IterativeQuickSort(); 32 | int arr[] = { 4, 3, 5, 2, 1, 3, 2, 3 }; 33 | ob.QuickSort(arr, 0, arr.length - 1); 34 | ob.printArr(arr, arr.length); 35 | } 36 | } -------------------------------------------------------------------------------- /Exercise_5.js: -------------------------------------------------------------------------------- 1 | class IterativeQuickSort { 2 | ​ 3 | function swap(arr, i, j) { 4 | ​ 5 | //Try swapping without extra variable 6 | ​ 7 | } 8 | ​ 9 | /* This function is same in both iterative and 10 | recursive*/ 11 | function partition(arr, l, h) { 12 | ​ 13 | //Compare elements and swap. 14 | ​ 15 | } 16 | ​ 17 | // Sorts arr[l..h] using iterative QuickSort 18 | function QuickSort(arr, l, h) { 19 | ​ 20 | //Try using Stack Data Structure to remove recursion. 21 | ​ 22 | } 23 | ​ 24 | // A utility function to print contents of arr 25 | function printArr(arr, n) { 26 | let i; 27 | for (i = 0; i < n; ++i) 28 | console.log(arr[i] + " "); 29 | } 30 | } 31 | ​ 32 | // Driver code to test above 33 | let ob = new IterativeQuickSort(); 34 | let arr = [4, 3, 5, 2, 1, 3, 2, 3]; 35 | ob.QuickSort(arr, 0, arr.length - 1); 36 | ob.printArr(arr, arr.length); 37 | -------------------------------------------------------------------------------- /Exercise_5.py: -------------------------------------------------------------------------------- 1 | # Python program for implementation of Quicksort 2 | 3 | # This function is same in both iterative and recursive 4 | def partition(arr, l, h): 5 | #write your code here 6 | 7 | 8 | def quickSortIterative(arr, l, h): 9 | #write your code here 10 | 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PreCourse_2 2 | 3 | # All Instructions are already provided in the respective files. 4 | 5 | Exercise_1 : Binary Search. 6 | 7 | Exercise_2 : Quick sort. 8 | 9 | Exercise_3 : Find Mid Point of a Singly Linked List. 10 | 11 | Exercise_4 : Merge Sort. 12 | 13 | Exercise_5 : Iterative Quick Sort. 14 | 15 | *After completing the project kindly submit a pull request* 16 | -------------------------------------------------------------------------------- /Sample.java: -------------------------------------------------------------------------------- 1 | // Time Complexity : 2 | // Space Complexity : 3 | // Did this code successfully run on Leetcode : 4 | // Any problem you faced while coding this : 5 | 6 | 7 | // Your code here along with comments explaining your approach 8 | --------------------------------------------------------------------------------