From af4c10f5ea67361741ff35bee417678c27fe5116 Mon Sep 17 00:00:00 2001 From: Divya Agarwal <56511764+divya116@users.noreply.github.com> Date: Fri, 2 Oct 2020 13:43:08 +0530 Subject: [PATCH 1/4] Create heap_sort.py --- Algorithms/sorting/heap_sort.py | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Algorithms/sorting/heap_sort.py diff --git a/Algorithms/sorting/heap_sort.py b/Algorithms/sorting/heap_sort.py new file mode 100644 index 0000000..6645bad --- /dev/null +++ b/Algorithms/sorting/heap_sort.py @@ -0,0 +1,42 @@ +# To heapify subtree rooted at index i. +# n is size of heap +def heapify(arr, n, i): + largest = i # Initialize largest as root + l = 2 * i + 1 # left = 2*i + 1 + r = 2 * i + 2 # right = 2*i + 2 + + # See if left child of root exists and is + # greater than root + if l < n and arr[i] < arr[l]: + largest = l + + # See if right child of root exists and is + # greater than root + if r < n and arr[largest] < arr[r]: + largest = r + + # Change root, if needed + if largest != i: + arr[i],arr[largest] = arr[largest],arr[i] # swap + + # Heapify the root. + heapify(arr, n, largest) + +def heapSort(arr): + n = len(arr) + + # Build a maxheap - since last parent will be at ((n//2)-1) we can start at that location. + for i in range(n // 2 - 1, -1, -1): + heapify(arr, n, i) + + # One by one extract elements + for i in range(n-1, 0, -1): + arr[i], arr[0] = arr[0], arr[i] # swap + heapify(arr, i, 0) + +arr = [ 12, 11, 13, 5, 6, 7] +heapSort(arr) +n = len(arr) +print ("Sorted array is") +for i in range(n): + print ("%d" %arr[i]), From 4254756ac68d1ed87bdd8d8883b14d40f0465c8a Mon Sep 17 00:00:00 2001 From: Divya Agarwal <56511764+divya116@users.noreply.github.com> Date: Fri, 2 Oct 2020 13:44:58 +0530 Subject: [PATCH 2/4] Create selection_sort.py --- Algorithms/sorting/selection_sort.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Algorithms/sorting/selection_sort.py diff --git a/Algorithms/sorting/selection_sort.py b/Algorithms/sorting/selection_sort.py new file mode 100644 index 0000000..8542ef1 --- /dev/null +++ b/Algorithms/sorting/selection_sort.py @@ -0,0 +1,18 @@ +import sys +A = [100, 50, 200, 60, 40] + +# Traverse through all array elements +for i in range(len(A)): + + # Find the minimum element in remaining unsorted array + min_idx = i + for j in range(i+1, len(A)): + if A[min_idx] > A[j]: + min_idx = j + + # Swap the found minimum element with the first element + A[i], A[min_idx] = A[min_idx], A[i] + +print ("Sorted array") +for i in range(len(A)): + print("%d" %A[i]), From 718d055e142d24cda27ebac31e5e5f56e148c4a8 Mon Sep 17 00:00:00 2001 From: Divya Agarwal <56511764+divya116@users.noreply.github.com> Date: Fri, 2 Oct 2020 13:49:50 +0530 Subject: [PATCH 3/4] Create create_list.cpp --- Data Structures/Linked List/create_list.cpp | 33 +++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Data Structures/Linked List/create_list.cpp diff --git a/Data Structures/Linked List/create_list.cpp b/Data Structures/Linked List/create_list.cpp new file mode 100644 index 0000000..d53a0ed --- /dev/null +++ b/Data Structures/Linked List/create_list.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +class Node +{ +public: + int data; + Node* next; +}; + +int main() +{ + Node* head = NULL; + Node* second = NULL; + Node* third = NULL; + + head = new Node(); + second = new Node(); + third = new Node(); + + head->data = 1; + head->next = second; + + second->data = 2; + + second->next = third; + + + third->data = 3; + third->next = NULL; + + return 0; +} From 871228cc49e19cfa5c5be079b31fbf9b0e2fdd81 Mon Sep 17 00:00:00 2001 From: Divya Agarwal <56511764+divya116@users.noreply.github.com> Date: Fri, 2 Oct 2020 13:52:06 +0530 Subject: [PATCH 4/4] Create insert_node.cpp --- Data Structures/Linked List/insert_node.cpp | 129 ++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 Data Structures/Linked List/insert_node.cpp diff --git a/Data Structures/Linked List/insert_node.cpp b/Data Structures/Linked List/insert_node.cpp new file mode 100644 index 0000000..717ecef --- /dev/null +++ b/Data Structures/Linked List/insert_node.cpp @@ -0,0 +1,129 @@ +#include +using namespace std; + +// A linked list node +class Node +{ + public: + int data; + Node *next; +}; + +/* Given a reference (pointer to pointer) +to the head of a list and an int, inserts +a new node on the front of the list. */ +void push(Node** head_ref, int new_data) +{ + /* 1. allocate node */ + Node* new_node = new Node(); + + /* 2. put in the data */ + new_node->data = new_data; + + /* 3. Make next of new node as head */ + new_node->next = (*head_ref); + + /* 4. move the head to point to the new node */ + (*head_ref) = new_node; +} + +/* Given a node prev_node, insert a new node after the given +prev_node */ +void insertAfter(Node* prev_node, int new_data) +{ + /*1. check if the given prev_node is NULL */ + if (prev_node == NULL) + { + cout<<"the given previous node cannot be NULL"; + return; + } + + /* 2. allocate new node */ + Node* new_node = new Node(); + + /* 3. put in the data */ + new_node->data = new_data; + + /* 4. Make next of new node as next of prev_node */ + new_node->next = prev_node->next; + + /* 5. move the next of prev_node as new_node */ + prev_node->next = new_node; +} + +/* Given a reference (pointer to pointer) to the head +of a list and an int, appends a new node at the end */ +void append(Node** head_ref, int new_data) +{ + /* 1. allocate node */ + Node* new_node = new Node(); + + Node *last = *head_ref; /* used in step 5*/ + + /* 2. put in the data */ + new_node->data = new_data; + + /* 3. This new node is going to be + the last node, so make next of + it as NULL*/ + new_node->next = NULL; + + /* 4. If the Linked List is empty, + then make the new node as head */ + if (*head_ref == NULL) + { + *head_ref = new_node; + return; + } + + /* 5. Else traverse till the last node */ + while (last->next != NULL) + last = last->next; + + /* 6. Change the next of last node */ + last->next = new_node; + return; +} + +// This function prints contents of +// linked list starting from head +void printList(Node *node) +{ + while (node != NULL) + { + cout<<" "<data; + node = node->next; + } +} + +int main() +{ + /* Start with the empty list */ + Node* head = NULL; + + // Insert 6. So linked list becomes 6->NULL + append(&head, 6); + + // Insert 7 at the beginning. + // So linked list becomes 7->6->NULL + push(&head, 7); + + // Insert 1 at the beginning. + // So linked list becomes 1->7->6->NULL + push(&head, 1); + + // Insert 4 at the end. So + // linked list becomes 1->7->6->4->NULL + append(&head, 4); + + // Insert 8, after 7. So linked + // list becomes 1->7->8->6->4->NULL + insertAfter(head->next, 8); + + cout<<"Created Linked list is: "; + printList(head); + + return 0; +} + +