From 58eed502248457b2e2dafba5c5fe2253175f8f85 Mon Sep 17 00:00:00 2001 From: Harsh Maurya <210108030@hbtu.ac.in> Date: Thu, 29 Jun 2023 18:10:22 +0530 Subject: [PATCH 1/4] this ish the firsht commit --- 07. Sorting/quicksort.cpp | 46 +++- 11. Linked List/doubly_linked_list.cpp | 321 +++++++++++++++++++------ Sorting | 1 + 3 files changed, 279 insertions(+), 89 deletions(-) create mode 160000 Sorting diff --git a/07. Sorting/quicksort.cpp b/07. Sorting/quicksort.cpp index 8871d5c3..fa283b45 100644 --- a/07. Sorting/quicksort.cpp +++ b/07. Sorting/quicksort.cpp @@ -1,4 +1,8 @@ -#include +/* C++ implementation of QuickSort */ +#include +using namespace std; + +// A utility function to swap two elements void swap(int* a, int* b) { int t = *a; @@ -6,16 +10,22 @@ void swap(int* a, int* b) *b = t; } +/* 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) { - int pivot = arr[high]; - int i = (low - 1); + int pivot = arr[high]; // pivot + int i = (low - 1); // Index of smaller element and indicates the right position of pivot found so far - for (int j = low; j <= high- 1; j++) + for (int j = low; j <= high - 1; j++) { - if (arr[j] <= pivot) + // If current element is smaller than the pivot + if (arr[j] < pivot) { - i++; + i++; // increment index of smaller element swap(&arr[i], &arr[j]); } } @@ -23,32 +33,42 @@ int partition (int arr[], int low, int high) return (i + 1); } +/* 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) { if (low < high) { - + /* pi is partitioning index, arr[p] is now + at right place */ int pi = partition(arr, low, high); + // Separately sort elements before + // partition and after partition quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } +/* Function to print an array */ void printArray(int arr[], int size) { int i; - for (i=0; i < size; i++) - printf("%d ", arr[i]); - printf("\n"); + 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); - printf("Sorted array: \n"); + int n = sizeof(arr) / sizeof(arr[0]); + quickSort(arr, 0, n - 1); + cout << "Sorted array: \n"; printArray(arr, n); return 0; } + diff --git a/11. Linked List/doubly_linked_list.cpp b/11. Linked List/doubly_linked_list.cpp index e6c2a89c..b0360275 100644 --- a/11. Linked List/doubly_linked_list.cpp +++ b/11. Linked List/doubly_linked_list.cpp @@ -1,89 +1,258 @@ -#include +#include using namespace std; - -class Node -{ - public: - Node *back; - int data; - Node *next; + +// A doubly linked list node +struct Node { + int data; + struct Node* next; + struct Node* prev; }; - -Node *first=NULL; - -void create_dll(int A[],int n) + +//inserts node at the front of the list +void insert_front(struct Node** head, int new_data) { - first=new Node; - first->back=NULL; - first->data=A[0]; - first->next=NULL; - - Node *current_ptr=first; - - for(int i=1;idata=A[i]; - - temp->back=current_ptr; - temp->next=NULL; - current_ptr->next=temp; - current_ptr=temp; - } + //allocate memory for New node + struct Node* newNode = new Node; + + //assign data to new node + newNode->data = new_data; + + //new node is head and previous is null, since we are adding at the front + newNode->next = (*head); + newNode->prev = NULL; + + //previous of head is new node + if ((*head) != NULL) + (*head)->prev = newNode; + + //head points to new node + (*head) = newNode; } - - -void Display() +/* Given a node as prev_node, insert a new node after the given node */ +void insert_After(struct Node* prev_node, int new_data) +{ + //check if prev node is null + if (prev_node == NULL) { + cout<<"Previous node is required , it cannot be NULL"; + return; +} + //allocate memory for new node + struct Node* newNode = new Node; + + //assign data to new node + newNode->data = new_data; + + //set next of newnode to next of prev node + newNode->next = prev_node->next; + + //set next of prev node to newnode + prev_node->next = newNode; + + //now set prev of newnode to prev node + newNode->prev = prev_node; + + //set prev of new node's next to newnode + if (newNode->next != NULL) + newNode->next->prev = newNode; +} + +//insert a new node at the end of the list +void insert_end(struct Node** head, int new_data) { - Node *ptr=first; - while(ptr) - { - cout<<"<-"<data<<"->"; - ptr=ptr->next; + //allocate memory for node + struct Node* newNode = new Node; + + struct Node* last = *head; //set last node value to head + + //set data for new node + newNode->data = new_data; + + //new node is the last node , so set next of new node to null + newNode->next = NULL; + + //check if list is empty, if yes make new node the head of list + if (*head == NULL) { + newNode->prev = NULL; + *head = newNode; + return; +} + +//otherwise traverse the list to go to last node +while (last->next != NULL) +last = last->next; + +//set next of last to new node +last->next = newNode; + +//set last to prev of new node +newNode->prev = last; +return; +} + +// This function prints contents of linked list starting from the given node +void displayList(struct Node* node) { + struct Node* last; + + while (node != NULL) { + cout<data<<"<==>"; + last = node; + node = node->next; + } + if(node == NULL) + cout<<"NULL"; } - cout<next, 30); + + cout<<"Doubly linked list is as follows: "<next; - x=ptr->data; - delete(ptr); - first->back=NULL; - } - - else - { - Node *q=first; - for(int i=0;inext; - } - if(q!=NULL) - { - q->back->next=q->next; - q->next->back=q->back; +10<==>20<==>30<==>40<==>50<==>NULL - x=q->data; - delete(q); - } - } - return x; -} +The above program constructs a doubly linked list by inserting the nodes using three insertion methods i.e. inserting the node at the front, inserting the node at the end and inserting the node after the given node. +Next, we demonstrate the same operation as a Java implementation. -int main() +// Java Class for Doubly Linked List +class Doubly_linkedList { + Node head; // list head + + /* Doubly Linked list Node*/ + class Node { + int data; + Node prev; + Node next; + + //create a new node using constructor + Node(int d) { data = d; } + } + +// insert a node at the front of the list +public void insert_front(int new_data) { - int A[]={1,2,3,4,5}; - int n ; - n= sizeof(A)/sizeof(A[0]); - create_dll(A,n); - Display(); - cout<"); + last = node; + node = node.next; + } + if(node == null) + System.out.print("null"); + System.out.println(); + + } +} +class Main{ + public static void main(String[] args) + { + /* Start with the empty list */ + Doubly_linkedList dll = new Doubly_linkedList(); + + // Insert 40. + dll.insert_end(40); + + // Insert 20 at the beginning. + dll.insert_front(20); + + // Insert 10 at the beginning. + dll.insert_front(10); + + // Insert 50 at the end. + dll.insert_end(50); + + // Insert 30, after 20. + dll.Insert_After(dll.head.next, 30); + + System.out.println("Doubly linked list created is as follows: "); + dll.displaylist(dll.head); + } +} diff --git a/Sorting b/Sorting new file mode 160000 index 00000000..89544b7a --- /dev/null +++ b/Sorting @@ -0,0 +1 @@ +Subproject commit 89544b7aa974e133ae7b520ffc48297fd4f78d92 From 7a7b835fd00ce086fae749a26d024192fdae7a96 Mon Sep 17 00:00:00 2001 From: Harsh Maurya <210108030@hbtu.ac.in> Date: Sat, 1 Jul 2023 16:55:06 +0530 Subject: [PATCH 2/4] the reverse of array using while --- 05. Arrays/Reverse an array.cpp | 19 ++++++++++++++ 07. Sorting/quicksort.cpp | 46 ++++++++++----------------------- Sorting | 1 - 3 files changed, 32 insertions(+), 34 deletions(-) delete mode 160000 Sorting diff --git a/05. Arrays/Reverse an array.cpp b/05. Arrays/Reverse an array.cpp index e4aba831..874f5ec7 100644 --- a/05. Arrays/Reverse an array.cpp +++ b/05. Arrays/Reverse an array.cpp @@ -1,5 +1,23 @@ #include using namespace std; +void reverse_array(int arr[],int n) +{ + int start=0; + int end=n-1; + while(start>A[i]; } + reverse_array(A,n); for(int begin=0;begin<(n/2);begin++) { diff --git a/07. Sorting/quicksort.cpp b/07. Sorting/quicksort.cpp index fa283b45..8871d5c3 100644 --- a/07. Sorting/quicksort.cpp +++ b/07. Sorting/quicksort.cpp @@ -1,8 +1,4 @@ -/* C++ implementation of QuickSort */ -#include -using namespace std; - -// A utility function to swap two elements +#include void swap(int* a, int* b) { int t = *a; @@ -10,22 +6,16 @@ void swap(int* a, int* b) *b = t; } -/* 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) { - int pivot = arr[high]; // pivot - int i = (low - 1); // Index of smaller element and indicates the right position of pivot found so far + int pivot = arr[high]; + int i = (low - 1); - for (int j = low; j <= high - 1; j++) + for (int j = low; j <= high- 1; j++) { - // If current element is smaller than the pivot - if (arr[j] < pivot) + if (arr[j] <= pivot) { - i++; // increment index of smaller element + i++; swap(&arr[i], &arr[j]); } } @@ -33,42 +23,32 @@ int partition (int arr[], int low, int high) return (i + 1); } -/* 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) { if (low < high) { - /* pi is partitioning index, arr[p] is now - at right place */ + int pi = partition(arr, low, high); - // Separately sort elements before - // partition and after partition quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } -/* Function to print an array */ void printArray(int arr[], int size) { int i; - for (i = 0; i < size; i++) - cout << arr[i] << " "; - cout << endl; + for (i=0; i < size; i++) + printf("%d ", arr[i]); + printf("\n"); } -// 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"; + int n = sizeof(arr)/sizeof(arr[0]); + quickSort(arr, 0, n-1); + printf("Sorted array: \n"); printArray(arr, n); return 0; } - diff --git a/Sorting b/Sorting deleted file mode 160000 index 89544b7a..00000000 --- a/Sorting +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 89544b7aa974e133ae7b520ffc48297fd4f78d92 From f88157b7c14e11b99d5974649985449bccd39790 Mon Sep 17 00:00:00 2001 From: Harsh Maurya <210108030@hbtu.ac.in> Date: Sat, 1 Jul 2023 17:08:36 +0530 Subject: [PATCH 3/4] comment --- 05. Arrays/Reverse an array.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/05. Arrays/Reverse an array.cpp b/05. Arrays/Reverse an array.cpp index 874f5ec7..bc48ce7f 100644 --- a/05. Arrays/Reverse an array.cpp +++ b/05. Arrays/Reverse an array.cpp @@ -1,5 +1,7 @@ #include using namespace std; + +//fun reverse array using while loop -start here void reverse_array(int arr[],int n) { int start=0; @@ -11,6 +13,7 @@ void reverse_array(int arr[],int n) arr[end]=temp; } cout<<"array is reversed using while loop:"< Date: Sat, 1 Jul 2023 17:56:44 +0530 Subject: [PATCH 4/4] stock buy and sell prob folder --- stock_buy_and_sell_variations/stock_first_type | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 stock_buy_and_sell_variations/stock_first_type diff --git a/stock_buy_and_sell_variations/stock_first_type b/stock_buy_and_sell_variations/stock_first_type new file mode 100644 index 00000000..e69de29b