├── DjikstraAlgorithmCode.cpp ├── LICENSE ├── codebinarysearch.cpp ├── codebubblesort.cpp ├── codebucketsort.cpp ├── codeheapsort.cpp ├── codeinsertionsort.cpp ├── codemergesort.cpp ├── codequicksort.cpp ├── coderadixsort.cpp ├── codeselectionsort.cpp ├── hacktoberfest0.png ├── hacktoberfest1.png ├── hacktoberfest2.png ├── hacktoberfest3.png ├── hacktoberfest4.png ├── holopin.png ├── linearsearch.cpp ├── mattermost_hacktoberfest.png └── mlh.png /DjikstraAlgorithmCode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | // Function to implement Dijkstra's algorithm 8 | void dijkstra(int graph[][10], int source, int size) { 9 | vector distance(size, INT_MAX); 10 | distance[source] = 0; 11 | set> s; 12 | s.insert({0, source}); 13 | 14 | while (!s.empty()) { 15 | auto top = *(s.begin()); 16 | int u = top.second; 17 | s.erase(s.begin()); 18 | 19 | for (int v = 0; v < size; v++) { 20 | if (graph[u][v] != 0) { 21 | if (distance[u] + graph[u][v] < distance[v]) { 22 | distance[v] = distance[u] + graph[u][v]; 23 | s.insert({distance[v], v}); 24 | } 25 | } 26 | } 27 | } 28 | 29 | cout << "Vertex Distance from Source" << endl; 30 | for (int i = 0; i < size; i++) { 31 | cout << i << "\t\t" << distance[i] << endl; 32 | } 33 | } 34 | 35 | int main() { 36 | int graph[10][10] = { 37 | {0, 7, 9, 0, 0, 14}, 38 | {7, 0, 10, 15, 0, 0}, 39 | {9, 10, 0, 11, 0, 2}, 40 | {0, 15, 11, 0, 6, 0}, 41 | {0, 0, 0, 6, 0, 9}, 42 | {14, 0, 2, 0, 9, 0} 43 | }; 44 | int size = 6; 45 | 46 | int source = 0; 47 | dijkstra(graph, source, size); 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /codebinarysearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int binarySearch(int arr[], int size, int key) { 5 | int left = 0, right = size - 1; 6 | 7 | while (left <= right) { 8 | int mid = left + (right - left) / 2; 9 | 10 | // Check if the key is at mid 11 | if (arr[mid] == key) 12 | return mid; 13 | 14 | // If key is greater, ignore left half 15 | if (arr[mid] < key) 16 | left = mid + 1; 17 | 18 | // If key is smaller, ignore right half 19 | else 20 | right = mid - 1; 21 | } 22 | 23 | // Key is not present in the array 24 | return -1; 25 | } 26 | 27 | int main() { 28 | int arr[] = {2, 3, 4, 10, 40}; 29 | int size = sizeof(arr) / sizeof(arr[0]); 30 | int key = 10; 31 | 32 | int result = binarySearch(arr, size, key); 33 | 34 | if (result != -1) 35 | cout << "Element found at index " << result << endl; 36 | else 37 | cout << "Element not found in the array" << endl; 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /codebubblesort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void bubbleSort(int arr[], int size) { 5 | for (int i = 0; i < size - 1; i++) { 6 | // Last i elements are already in place 7 | for (int j = 0; j < size - i - 1; j++) { 8 | if (arr[j] > arr[j + 1]) { 9 | // Swap arr[j] and arr[j+1] 10 | int temp = arr[j]; 11 | arr[j] = arr[j + 1]; 12 | arr[j + 1] = temp; 13 | } 14 | } 15 | } 16 | } 17 | 18 | void printArray(int arr[], int size) { 19 | for (int i = 0; i < size; i++) 20 | cout << arr[i] << " "; 21 | cout << endl; 22 | } 23 | 24 | int main() { 25 | int arr[] = {64, 34, 25, 12, 22, 11, 90}; 26 | int size = sizeof(arr) / sizeof(arr[0]); 27 | 28 | cout << "Original array: "; 29 | printArray(arr, size); 30 | 31 | bubbleSort(arr, size); 32 | 33 | cout << "Sorted array: "; 34 | printArray(arr, size); 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /codebucketsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // Function to perform Bucket Sort 7 | void bucketSort(float arr[], int size) { 8 | // Create an empty array of buckets 9 | vector buckets[size]; 10 | 11 | // Put array elements in different buckets based on their value 12 | for (int i = 0; i < size; i++) { 13 | int bucketIndex = size * arr[i]; // Index in the bucket 14 | buckets[bucketIndex].push_back(arr[i]); 15 | } 16 | 17 | // Sort individual buckets 18 | for (int i = 0; i < size; i++) { 19 | sort(buckets[i].begin(), buckets[i].end()); 20 | } 21 | 22 | // Concatenate all buckets into arr[] 23 | int index = 0; 24 | for (int i = 0; i < size; i++) { 25 | for (int j = 0; j < buckets[i].size(); j++) { 26 | arr[index++] = buckets[i][j]; 27 | } 28 | } 29 | } 30 | 31 | // Function to print the array 32 | void printArray(float arr[], int size) { 33 | for (int i = 0; i < size; i++) 34 | cout << arr[i] << " "; 35 | cout << endl; 36 | } 37 | 38 | int main() { 39 | float arr[] = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434}; 40 | int size = sizeof(arr) / sizeof(arr[0]); 41 | 42 | cout << "Original array: "; 43 | printArray(arr, size); 44 | 45 | bucketSort(arr, size); 46 | 47 | cout << "Sorted array: "; 48 | printArray(arr, size); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /codeheapsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Function to heapify a subtree rooted at node i 5 | void heapify(int arr[], int size, int i) { 6 | int largest = i; // Initialize largest as root 7 | int left = 2 * i + 1; // Left child 8 | int right = 2 * i + 2; // Right child 9 | 10 | // If the left child is larger than root 11 | if (left < size && arr[left] > arr[largest]) 12 | largest = left; 13 | 14 | // If the right child is larger than largest so far 15 | if (right < size && arr[right] > arr[largest]) 16 | largest = right; 17 | 18 | // If largest is not root 19 | if (largest != i) { 20 | swap(arr[i], arr[largest]); 21 | 22 | // Recursively heapify the affected subtree 23 | heapify(arr, size, largest); 24 | } 25 | } 26 | 27 | // Main function to implement Heap Sort 28 | void heapSort(int arr[], int size) { 29 | // Build the max heap 30 | for (int i = size / 2 - 1; i >= 0; i--) 31 | heapify(arr, size, i); 32 | 33 | // Extract elements one by one from the heap 34 | for (int i = size - 1; i >= 0; i--) { 35 | // Move the current root to the end 36 | swap(arr[0], arr[i]); 37 | 38 | // Call heapify on the reduced heap 39 | heapify(arr, i, 0); 40 | } 41 | } 42 | 43 | // Function to print the array 44 | void printArray(int arr[], int size) { 45 | for (int i = 0; i < size; i++) 46 | cout << arr[i] << " "; 47 | cout << endl; 48 | } 49 | 50 | int main() { 51 | int arr[] = {12, 11, 13, 5, 6, 7}; 52 | int size = sizeof(arr) / sizeof(arr[0]); 53 | 54 | cout << "Original array: "; 55 | printArray(arr, size); 56 | 57 | heapSort(arr, size); 58 | 59 | cout << "Sorted array: "; 60 | printArray(arr, size); 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /codeinsertionsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void insertionSort(int arr[], int size) { 5 | for (int i = 1; i < size; i++) { 6 | int key = arr[i]; 7 | int j = i - 1; 8 | 9 | // Move elements of arr[0..i-1], that are greater than key, to one position ahead 10 | // of their current position 11 | while (j >= 0 && arr[j] > key) { 12 | arr[j + 1] = arr[j]; 13 | j = j - 1; 14 | } 15 | arr[j + 1] = key; 16 | } 17 | } 18 | 19 | void printArray(int arr[], int size) { 20 | for (int i = 0; i < size; i++) 21 | cout << arr[i] << " "; 22 | cout << endl; 23 | } 24 | 25 | int main() { 26 | int arr[] = {12, 11, 13, 5, 6}; 27 | int size = sizeof(arr) / sizeof(arr[0]); 28 | 29 | cout << "Original array: "; 30 | printArray(arr, size); 31 | 32 | insertionSort(arr, size); 33 | 34 | cout << "Sorted array: "; 35 | printArray(arr, size); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /codemergesort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Function to merge two halves 5 | void merge(int arr[], int left, int mid, int right) { 6 | int n1 = mid - left + 1; 7 | int n2 = right - mid; 8 | 9 | // Create temporary arrays 10 | int leftArr[n1], rightArr[n2]; 11 | 12 | // Copy data to temporary arrays 13 | for (int i = 0; i < n1; i++) 14 | leftArr[i] = arr[left + i]; 15 | for (int j = 0; j < n2; j++) 16 | rightArr[j] = arr[mid + 1 + j]; 17 | 18 | // Merge the temporary arrays back into arr[left..right] 19 | int i = 0, j = 0, k = left; 20 | 21 | while (i < n1 && j < n2) { 22 | if (leftArr[i] <= rightArr[j]) { 23 | arr[k] = leftArr[i]; 24 | i++; 25 | } else { 26 | arr[k] = rightArr[j]; 27 | j++; 28 | } 29 | k++; 30 | } 31 | 32 | // Copy the remaining elements of leftArr[], if any 33 | while (i < n1) { 34 | arr[k] = leftArr[i]; 35 | i++; 36 | k++; 37 | } 38 | 39 | // Copy the remaining elements of rightArr[], if any 40 | while (j < n2) { 41 | arr[k] = rightArr[j]; 42 | j++; 43 | k++; 44 | } 45 | } 46 | 47 | // Function to implement merge sort 48 | void mergeSort(int arr[], int left, int right) { 49 | if (left < right) { 50 | int mid = left + (right - left) / 2; 51 | 52 | // Sort first and second halves 53 | mergeSort(arr, left, mid); 54 | mergeSort(arr, mid + 1, right); 55 | 56 | // Merge the sorted halves 57 | merge(arr, left, mid, right); 58 | } 59 | } 60 | 61 | // Function to print the array 62 | void printArray(int arr[], int size) { 63 | for (int i = 0; i < size; i++) 64 | cout << arr[i] << " "; 65 | cout << endl; 66 | } 67 | 68 | int main() { 69 | int arr[] = {12, 11, 13, 5, 6, 7}; 70 | int size = sizeof(arr) / sizeof(arr[0]); 71 | 72 | cout << "Original array: "; 73 | printArray(arr, size); 74 | 75 | mergeSort(arr, 0, size - 1); 76 | 77 | cout << "Sorted array: "; 78 | printArray(arr, size); 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /codequicksort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Function to swap two elements 5 | void swap(int* a, int* b) { 6 | int temp = *a; 7 | *a = *b; 8 | *b = temp; 9 | } 10 | 11 | // Function to partition the array using the last element as the pivot 12 | int partition(int arr[], int low, int high) { 13 | int pivot = arr[high]; // Choose the last element as the pivot 14 | int i = (low - 1); // Index of the smaller element 15 | 16 | for (int j = low; j < high; j++) { 17 | // If the current element is smaller than or equal to the pivot 18 | if (arr[j] <= pivot) { 19 | i++; // Increment index of the smaller element 20 | swap(&arr[i], &arr[j]); 21 | } 22 | } 23 | 24 | // Swap the pivot element with the element at i+1 position 25 | swap(&arr[i + 1], &arr[high]); 26 | return (i + 1); // Return the partition index 27 | } 28 | 29 | // Main function to implement Quick Sort 30 | void quickSort(int arr[], int low, int high) { 31 | if (low < high) { 32 | // Partition the array and get the pivot index 33 | int pi = partition(arr, low, high); 34 | 35 | // Recursively sort the elements before and after the partition 36 | quickSort(arr, low, pi - 1); 37 | quickSort(arr, pi + 1, high); 38 | } 39 | } 40 | 41 | // Function to print the array 42 | void printArray(int arr[], int size) { 43 | for (int i = 0; i < size; i++) 44 | cout << arr[i] << " "; 45 | cout << endl; 46 | } 47 | 48 | int main() { 49 | int arr[] = {10, 7, 8, 9, 1, 5}; 50 | int size = sizeof(arr) / sizeof(arr[0]); 51 | 52 | cout << "Original array: "; 53 | printArray(arr, size); 54 | 55 | quickSort(arr, 0, size - 1); 56 | 57 | cout << "Sorted array: "; 58 | printArray(arr, size); 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /coderadixsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Function to get the maximum value in an array 5 | int getMax(int arr[], int size) { 6 | int max = arr[0]; 7 | for (int i = 1; i < size; i++) 8 | if (arr[i] > max) 9 | max = arr[i]; 10 | return max; 11 | } 12 | 13 | // Function to do counting sort of arr[] according to the digit represented by exp (exponent) 14 | void countingSort(int arr[], int size, int exp) { 15 | int output[size]; // Output array 16 | int count[10] = {0}; 17 | 18 | // Store the count of occurrences in count[] 19 | for (int i = 0; i < size; i++) 20 | count[(arr[i] / exp) % 10]++; 21 | 22 | // Change count[i] so that it contains the actual position of this digit in output[] 23 | for (int i = 1; i < 10; i++) 24 | count[i] += count[i - 1]; 25 | 26 | // Build the output array 27 | for (int i = size - 1; i >= 0; i--) { 28 | output[count[(arr[i] / exp) % 10] - 1] = arr[i]; 29 | count[(arr[i] / exp) % 10]--; 30 | } 31 | 32 | // Copy the output array to arr[], so that arr[] now contains sorted numbers according to the current digit 33 | for (int i = 0; i < size; i++) 34 | arr[i] = output[i]; 35 | } 36 | 37 | // Main function to implement Radix Sort 38 | void radixSort(int arr[], int size) { 39 | // Find the maximum number to determine the number of digits 40 | int max = getMax(arr, size); 41 | 42 | // Do counting sort for every digit. Note that exp is 10^i where i is the current digit number 43 | for (int exp = 1; max / exp > 0; exp *= 10) 44 | countingSort(arr, size, exp); 45 | } 46 | 47 | // Function to print the array 48 | void printArray(int arr[], int size) { 49 | for (int i = 0; i < size; i++) 50 | cout << arr[i] << " "; 51 | cout << endl; 52 | } 53 | 54 | int main() { 55 | int arr[] = {170, 45, 75, 90, 802, 24, 2, 66}; 56 | int size = sizeof(arr) / sizeof(arr[0]); 57 | 58 | cout << "Original array: "; 59 | printArray(arr, size); 60 | 61 | radixSort(arr, size); 62 | 63 | cout << "Sorted array: "; 64 | printArray(arr, size); 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /codeselectionsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void selectionSort(int arr[], int size) { 5 | for (int i = 0; i < size - 1; i++) { 6 | // Find the minimum element in the unsorted part of the array 7 | int minIndex = i; 8 | for (int j = i + 1; j < size; j++) { 9 | if (arr[j] < arr[minIndex]) { 10 | minIndex = j; 11 | } 12 | } 13 | 14 | // Swap the found minimum element with the first element of the unsorted part 15 | int temp = arr[minIndex]; 16 | arr[minIndex] = arr[i]; 17 | arr[i] = temp; 18 | } 19 | } 20 | 21 | void printArray(int arr[], int size) { 22 | for (int i = 0; i < size; i++) 23 | cout << arr[i] << " "; 24 | cout << endl; 25 | } 26 | 27 | int main() { 28 | int arr[] = {64, 25, 12, 22, 11}; 29 | int size = sizeof(arr) / sizeof(arr[0]); 30 | 31 | cout << "Original array: "; 32 | printArray(arr, size); 33 | 34 | selectionSort(arr, size); 35 | 36 | cout << "Sorted array: "; 37 | printArray(arr, size); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /hacktoberfest0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anshumansinha3301/linearsearchcpp/2f339e2303a4f3da004847701ef3d6f88e76fb85/hacktoberfest0.png -------------------------------------------------------------------------------- /hacktoberfest1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anshumansinha3301/linearsearchcpp/2f339e2303a4f3da004847701ef3d6f88e76fb85/hacktoberfest1.png -------------------------------------------------------------------------------- /hacktoberfest2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anshumansinha3301/linearsearchcpp/2f339e2303a4f3da004847701ef3d6f88e76fb85/hacktoberfest2.png -------------------------------------------------------------------------------- /hacktoberfest3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anshumansinha3301/linearsearchcpp/2f339e2303a4f3da004847701ef3d6f88e76fb85/hacktoberfest3.png -------------------------------------------------------------------------------- /hacktoberfest4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anshumansinha3301/linearsearchcpp/2f339e2303a4f3da004847701ef3d6f88e76fb85/hacktoberfest4.png -------------------------------------------------------------------------------- /holopin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anshumansinha3301/linearsearchcpp/2f339e2303a4f3da004847701ef3d6f88e76fb85/holopin.png -------------------------------------------------------------------------------- /linearsearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int linearSearch(int arr[], int n, int target) { 5 | for (int i = 0; i < n; i++) { 6 | if (arr[i] == target) { 7 | return i; // Return the index if the target is found 8 | } 9 | } 10 | return -1; // Return -1 if the target is not found 11 | } 12 | 13 | int main() { 14 | int n, target; 15 | 16 | cout << "Enter the number of elements in the array: "; 17 | cin >> n; 18 | 19 | int arr[n]; 20 | cout << "Enter the elements of the array: "; 21 | for (int i = 0; i < n; i++) { 22 | cin >> arr[i]; 23 | } 24 | 25 | cout << "Enter the target element to search: "; 26 | cin >> target; 27 | 28 | int result = linearSearch(arr, n, target); 29 | 30 | if (result != -1) { 31 | cout << "Element found at index " << result << endl; 32 | } else { 33 | cout << "Element not found" << endl; 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /mattermost_hacktoberfest.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anshumansinha3301/linearsearchcpp/2f339e2303a4f3da004847701ef3d6f88e76fb85/mattermost_hacktoberfest.png -------------------------------------------------------------------------------- /mlh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anshumansinha3301/linearsearchcpp/2f339e2303a4f3da004847701ef3d6f88e76fb85/mlh.png --------------------------------------------------------------------------------