├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── CONTRIBUTING.md ├── Graph Theory ├── A Star │ ├── A_Star.cpp │ └── README.md ├── BFS │ ├── BFS.cpp │ └── README.md ├── Bellman Ford │ ├── README.md │ └── bellmanford.cpp ├── DFS │ ├── DFS.cpp │ ├── DFS_CycleDetection.cpp │ ├── DFS_TopologicalSort.cpp │ └── README.md ├── Dijkstra Algorithm │ ├── Dijkstra.cpp │ └── README.md ├── Kruskals MST │ ├── Kruskal_MST.cpp │ └── README.md ├── Prims MST │ ├── README.md │ └── code.cpp └── test ├── Greedy Algorithm └── Knapsack │ └── Knapsack_problem_algorthm.cpp ├── LICENSE ├── LinkedList ├── DLL.cpp └── SLL.cpp ├── Miscellaneous ├── Largest Prime Factor │ ├── largest_prime_factor.cpp │ └── test ├── Largest Product of a series │ ├── test │ └── thirteen_digits.py ├── Luhn's Algorithm │ ├── luhn.py │ └── test ├── Palindrome Product │ ├── palin_product.cpp │ └── test └── test ├── README.md ├── Randomized Algorithms ├── Armstrong's Permutation │ ├── Permute_By_Cylic.cpp │ ├── README.md │ └── test ├── Permute In Place │ ├── Permute_In_Place.cpp │ ├── README.md │ ├── Simple_Permute.exe │ └── test ├── Permute Without Identity │ ├── Permute_Without_Identity.cpp │ ├── Permute_Without_Identity.exe │ ├── README.md │ └── test ├── README.md └── test ├── Searching ├── Binary Search │ ├── BinarySearch-1.jpg │ ├── Binary_Search.cpp │ ├── README.md │ ├── arrayio.cpp │ └── arrayio.h ├── Fibonacci Search │ ├── README.md │ └── fibonacciSearch.cpp ├── Linear Search │ ├── README.md │ ├── linear_search_array_approach.cpp │ └── linear_search_vector_approach.cpp ├── README.md └── test ├── Sorting ├── Bubble Sort │ ├── Bubble_Sort.cpp │ └── README.md ├── Bucket Sort │ ├── Bucket_Sort.cpp │ └── README.md ├── Insertion Sort │ ├── Insertion_Sort.cpp │ └── README.md ├── Merge Sort │ ├── Merge_Sort.cpp │ └── README.md ├── Quicksort │ ├── Quicksort.cpp │ ├── README.md │ ├── arrayio.cpp │ └── arrayio.h ├── README.md ├── Selection Sort │ ├── README.md │ └── Selection_Sort.cpp ├── input.txt └── test └── images └── algorithms.jpg /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Description ## 2 | (Brief description on what this PR is about) 3 | 4 | ## If it is solving an issue, mention the issue number 5 | 6 | ## Language ## 7 | (enter x where it is correct for you) 8 | - [] C 9 | - [] C++ 10 | - [] Java 11 | - [] Python 12 | 13 | ## Language - Branch mapping 14 | | Lang | The branch to which you are making the PR| 15 | | ----------- | ----------- | 16 | | C | C | 17 | | C++ | master | 18 | | Java | Java | 19 | | Python | Python | 20 | 21 | ## Does your PR satisfy this mapping? 22 | - [] Yes 23 | - [] No 24 | 25 | ## Checklist ## 26 | ### Essentials ### 27 | - [ ] Changes are complete (i.e. I finished coding on this PR) 28 | - [ ] Code is well-documented 29 | 30 | ### Changes ### 31 | - [ ] Feature1 32 | - [ ] Feature2 33 | 34 | ## Comments ## 35 | - Interesting edge cases to note here 36 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributions 2 | 3 | ## Making a PR ## 4 | 5 | Make sure that you fill out the relevant details in the PR. 6 | 7 | ## Guidelines for Merging 8 | 9 | The PR which will be having the specific file name as given in the issue, the one whose commit descriptions are relevant, and the code is readable, would be merged. 10 | 11 | For docs, make sure that you use markdown syntax. Checkout these links: [Markdown Guide](https://www.markdownguide.org/), [Markdown Cheat-Sheet](https://www.markdownguide.org/cheat-sheet/). 12 | 13 | ## How to Collaborate: 14 | 15 | 1. Fork the repository to your own GitHub account. 16 | 17 | 2. Clone the repository to your local machine 18 | ``` 19 | $ git clone "https://www.github.com/{Username}/Algorithms.git" 20 | ``` 21 | where username is your GitHub account username. 22 | 23 | 3. Create a branch where you can do your local work. 24 | Never work on **master** branch as we do not allow master commits except by admins. 25 | ``` 26 | $ git branch {branchname} 27 | $ git checkout branchname 28 | ``` 29 | 30 | 4. Do your work and stage your changes. 31 | ``` 32 | $ git add 33 | ``` 34 | 35 | 5. Commit you changes with a commit message containing your name, file(s) worked upon, changes added. 36 | 37 | 6. Push changes to your forked repository 38 | ``` 39 | $ git push -u origin branchname 40 | ``` 41 | 7. Create a pull request to the upstream repository. 42 | 43 | ## Synchronize forked repository with Upstream repository 44 | 45 | 1. Create upstream as our repository 46 | ``` 47 | $ git remote add upstream "https://www.github.com/ekdnam/Algorithms" 48 | ``` 49 | 50 | 2. Fetch upstream changes in local machine 51 | ``` 52 | $ git fetch upstream 53 | ``` 54 | 55 | 3. Switch to master branch 56 | ``` 57 | $ git checkout master 58 | ``` 59 | 60 | 4. Merge changes in local machine 61 | ``` 62 | $ git merge upstream/master 63 | ``` 64 | 65 | 5. Push changes to your forked GitHub repository 66 | ``` 67 | $ git push -f origin master 68 | ``` 69 | -------------------------------------------------------------------------------- /Graph Theory/A Star/A_Star.cpp: -------------------------------------------------------------------------------- 1 | // A C++ Program to implement A* Search Algorithm 2 | #include 3 | using namespace std; 4 | 5 | #define ROW 9 6 | #define COL 10 7 | 8 | // Creating a shortcut for int, int pair type 9 | typedef pair Pair; 10 | 11 | // Creating a shortcut for pair> type 12 | typedef pair> pPair; 13 | 14 | // A structure to hold the neccesary parameters 15 | struct cell 16 | { 17 | // Row and Column index of its parent 18 | // Note that 0 <= i <= ROW-1 & 0 <= j <= COL-1 19 | int parent_i, parent_j; 20 | // f = g + h 21 | double f, g, h; 22 | }; 23 | 24 | // A Utility Function to check whether given cell (row, col) 25 | // is a valid cell or not. 26 | bool isValid(int row, int col) 27 | { 28 | // Returns true if row number and column number 29 | // is in range 30 | return (row >= 0) && (row < ROW) && 31 | (col >= 0) && (col < COL); 32 | } 33 | 34 | // A Utility Function to check whether the given cell is 35 | // blocked or not 36 | bool isUnBlocked(int grid[][COL], int row, int col) 37 | { 38 | // Returns true if the cell is not blocked else false 39 | if (grid[row][col] == 1) 40 | return (true); 41 | else 42 | return (false); 43 | } 44 | 45 | // A Utility Function to check whether destination cell has 46 | // been reached or not 47 | bool isDestination(int row, int col, Pair dest) 48 | { 49 | if (row == dest.first && col == dest.second) 50 | return (true); 51 | else 52 | return (false); 53 | } 54 | 55 | // A Utility Function to calculate the 'h' heuristics. 56 | double calculateHValue(int row, int col, Pair dest) 57 | { 58 | // Return using the distance formula 59 | return ((double)sqrt ((row-dest.first)*(row-dest.first) 60 | + (col-dest.second)*(col-dest.second))); 61 | } 62 | 63 | // A Utility Function to trace the path from the source 64 | // to destination 65 | void tracePath(cell cellDetails[][COL], Pair dest) 66 | { 67 | printf ("\nThe Path is "); 68 | int row = dest.first; 69 | int col = dest.second; 70 | 71 | stack Path; 72 | 73 | while (!(cellDetails[row][col].parent_i == row 74 | && cellDetails[row][col].parent_j == col )) 75 | { 76 | Path.push (make_pair (row, col)); 77 | int temp_row = cellDetails[row][col].parent_i; 78 | int temp_col = cellDetails[row][col].parent_j; 79 | row = temp_row; 80 | col = temp_col; 81 | } 82 | 83 | Path.push (make_pair (row, col)); 84 | while (!Path.empty()) 85 | { 86 | pair p = Path.top(); 87 | Path.pop(); 88 | printf("-> (%d,%d) ",p.first,p.second); 89 | } 90 | 91 | return; 92 | } 93 | 94 | // A Function to find the shortest path between 95 | // a given source cell to a destination cell according 96 | // to A* Search Algorithm 97 | void aStarSearch(int grid[][COL], Pair src, Pair dest) 98 | { 99 | // If the source is out of range 100 | if (isValid (src.first, src.second) == false) 101 | { 102 | printf ("Source is invalid\n"); 103 | return; 104 | } 105 | 106 | // If the destination is out of range 107 | if (isValid (dest.first, dest.second) == false) 108 | { 109 | printf ("Destination is invalid\n"); 110 | return; 111 | } 112 | 113 | // Either the source or the destination is blocked 114 | if (isUnBlocked(grid, src.first, src.second) == false || 115 | isUnBlocked(grid, dest.first, dest.second) == false) 116 | { 117 | printf ("Source or the destination is blocked\n"); 118 | return; 119 | } 120 | 121 | // If the destination cell is the same as source cell 122 | if (isDestination(src.first, src.second, dest) == true) 123 | { 124 | printf ("We are already at the destination\n"); 125 | return; 126 | } 127 | 128 | // Create a closed list and initialise it to false which means 129 | // that no cell has been included yet 130 | // This closed list is implemented as a boolean 2D array 131 | bool closedList[ROW][COL]; 132 | memset(closedList, false, sizeof (closedList)); 133 | 134 | // Declare a 2D array of structure to hold the details 135 | //of that cell 136 | cell cellDetails[ROW][COL]; 137 | 138 | int i, j; 139 | 140 | for (i=0; i> 163 | where f = g + h, 164 | and i, j are the row and column index of that cell 165 | Note that 0 <= i <= ROW-1 & 0 <= j <= COL-1 166 | This open list is implenented as a set of pair of pair.*/ 167 | set openList; 168 | 169 | // Put the starting cell on the open list and set its 170 | // 'f' as 0 171 | openList.insert(make_pair (0.0, make_pair (i, j))); 172 | 173 | // We set this boolean value as false as initially 174 | // the destination is not reached. 175 | bool foundDest = false; 176 | 177 | while (!openList.empty()) 178 | { 179 | pPair p = *openList.begin(); 180 | 181 | // Remove this vertex from the open list 182 | openList.erase(openList.begin()); 183 | 184 | // Add this vertex to the closed list 185 | i = p.second.first; 186 | j = p.second.second; 187 | closedList[i][j] = true; 188 | 189 | /* 190 | Generating all the 8 successor of this cell 191 | 192 | N.W N N.E 193 | \ | / 194 | \ | / 195 | W----Cell----E 196 | / | \ 197 | / | \ 198 | S.W S S.E 199 | 200 | Cell-->Popped Cell (i, j) 201 | N --> North (i-1, j) 202 | S --> South (i+1, j) 203 | E --> East (i, j+1) 204 | W --> West (i, j-1) 205 | N.E--> North-East (i-1, j+1) 206 | N.W--> North-West (i-1, j-1) 207 | S.E--> South-East (i+1, j+1) 208 | S.W--> South-West (i+1, j-1)*/ 209 | 210 | // To store the 'g', 'h' and 'f' of the 8 successors 211 | double gNew, hNew, fNew; 212 | 213 | //----------- 1st Successor (North) ------------ 214 | 215 | // Only process this cell if this is a valid one 216 | if (isValid(i-1, j) == true) 217 | { 218 | // If the destination cell is the same as the 219 | // current successor 220 | if (isDestination(i-1, j, dest) == true) 221 | { 222 | // Set the Parent of the destination cell 223 | cellDetails[i-1][j].parent_i = i; 224 | cellDetails[i-1][j].parent_j = j; 225 | printf ("The destination cell is found\n"); 226 | tracePath (cellDetails, dest); 227 | foundDest = true; 228 | return; 229 | } 230 | // If the successor is already on the closed 231 | // list or if it is blocked, then ignore it. 232 | // Else do the following 233 | else if (closedList[i-1][j] == false && 234 | isUnBlocked(grid, i-1, j) == true) 235 | { 236 | gNew = cellDetails[i][j].g + 1.0; 237 | hNew = calculateHValue (i-1, j, dest); 238 | fNew = gNew + hNew; 239 | 240 | // If it isn’t on the open list, add it to 241 | // the open list. Make the current square 242 | // the parent of this square. Record the 243 | // f, g, and h costs of the square cell 244 | // OR 245 | // If it is on the open list already, check 246 | // to see if this path to that square is better, 247 | // using 'f' cost as the measure. 248 | if (cellDetails[i-1][j].f == FLT_MAX || 249 | cellDetails[i-1][j].f > fNew) 250 | { 251 | openList.insert( make_pair(fNew, 252 | make_pair(i-1, j))); 253 | 254 | // Update the details of this cell 255 | cellDetails[i-1][j].f = fNew; 256 | cellDetails[i-1][j].g = gNew; 257 | cellDetails[i-1][j].h = hNew; 258 | cellDetails[i-1][j].parent_i = i; 259 | cellDetails[i-1][j].parent_j = j; 260 | } 261 | } 262 | } 263 | 264 | //----------- 2nd Successor (South) ------------ 265 | 266 | // Only process this cell if this is a valid one 267 | if (isValid(i+1, j) == true) 268 | { 269 | // If the destination cell is the same as the 270 | // current successor 271 | if (isDestination(i+1, j, dest) == true) 272 | { 273 | // Set the Parent of the destination cell 274 | cellDetails[i+1][j].parent_i = i; 275 | cellDetails[i+1][j].parent_j = j; 276 | printf("The destination cell is found\n"); 277 | tracePath(cellDetails, dest); 278 | foundDest = true; 279 | return; 280 | } 281 | // If the successor is already on the closed 282 | // list or if it is blocked, then ignore it. 283 | // Else do the following 284 | else if (closedList[i+1][j] == false && 285 | isUnBlocked(grid, i+1, j) == true) 286 | { 287 | gNew = cellDetails[i][j].g + 1.0; 288 | hNew = calculateHValue(i+1, j, dest); 289 | fNew = gNew + hNew; 290 | 291 | // If it isn’t on the open list, add it to 292 | // the open list. Make the current square 293 | // the parent of this square. Record the 294 | // f, g, and h costs of the square cell 295 | // OR 296 | // If it is on the open list already, check 297 | // to see if this path to that square is better, 298 | // using 'f' cost as the measure. 299 | if (cellDetails[i+1][j].f == FLT_MAX || 300 | cellDetails[i+1][j].f > fNew) 301 | { 302 | openList.insert( make_pair (fNew, make_pair (i+1, j))); 303 | // Update the details of this cell 304 | cellDetails[i+1][j].f = fNew; 305 | cellDetails[i+1][j].g = gNew; 306 | cellDetails[i+1][j].h = hNew; 307 | cellDetails[i+1][j].parent_i = i; 308 | cellDetails[i+1][j].parent_j = j; 309 | } 310 | } 311 | } 312 | 313 | //----------- 3rd Successor (East) ------------ 314 | 315 | // Only process this cell if this is a valid one 316 | if (isValid (i, j+1) == true) 317 | { 318 | // If the destination cell is the same as the 319 | // current successor 320 | if (isDestination(i, j+1, dest) == true) 321 | { 322 | // Set the Parent of the destination cell 323 | cellDetails[i][j+1].parent_i = i; 324 | cellDetails[i][j+1].parent_j = j; 325 | printf("The destination cell is found\n"); 326 | tracePath(cellDetails, dest); 327 | foundDest = true; 328 | return; 329 | } 330 | 331 | // If the successor is already on the closed 332 | // list or if it is blocked, then ignore it. 333 | // Else do the following 334 | else if (closedList[i][j+1] == false && 335 | isUnBlocked (grid, i, j+1) == true) 336 | { 337 | gNew = cellDetails[i][j].g + 1.0; 338 | hNew = calculateHValue (i, j+1, dest); 339 | fNew = gNew + hNew; 340 | 341 | // If it isn’t on the open list, add it to 342 | // the open list. Make the current square 343 | // the parent of this square. Record the 344 | // f, g, and h costs of the square cell 345 | // OR 346 | // If it is on the open list already, check 347 | // to see if this path to that square is better, 348 | // using 'f' cost as the measure. 349 | if (cellDetails[i][j+1].f == FLT_MAX || 350 | cellDetails[i][j+1].f > fNew) 351 | { 352 | openList.insert( make_pair(fNew, 353 | make_pair (i, j+1))); 354 | 355 | // Update the details of this cell 356 | cellDetails[i][j+1].f = fNew; 357 | cellDetails[i][j+1].g = gNew; 358 | cellDetails[i][j+1].h = hNew; 359 | cellDetails[i][j+1].parent_i = i; 360 | cellDetails[i][j+1].parent_j = j; 361 | } 362 | } 363 | } 364 | 365 | //----------- 4th Successor (West) ------------ 366 | 367 | // Only process this cell if this is a valid one 368 | if (isValid(i, j-1) == true) 369 | { 370 | // If the destination cell is the same as the 371 | // current successor 372 | if (isDestination(i, j-1, dest) == true) 373 | { 374 | // Set the Parent of the destination cell 375 | cellDetails[i][j-1].parent_i = i; 376 | cellDetails[i][j-1].parent_j = j; 377 | printf("The destination cell is found\n"); 378 | tracePath(cellDetails, dest); 379 | foundDest = true; 380 | return; 381 | } 382 | 383 | // If the successor is already on the closed 384 | // list or if it is blocked, then ignore it. 385 | // Else do the following 386 | else if (closedList[i][j-1] == false && 387 | isUnBlocked(grid, i, j-1) == true) 388 | { 389 | gNew = cellDetails[i][j].g + 1.0; 390 | hNew = calculateHValue(i, j-1, dest); 391 | fNew = gNew + hNew; 392 | 393 | // If it isn’t on the open list, add it to 394 | // the open list. Make the current square 395 | // the parent of this square. Record the 396 | // f, g, and h costs of the square cell 397 | // OR 398 | // If it is on the open list already, check 399 | // to see if this path to that square is better, 400 | // using 'f' cost as the measure. 401 | if (cellDetails[i][j-1].f == FLT_MAX || 402 | cellDetails[i][j-1].f > fNew) 403 | { 404 | openList.insert( make_pair (fNew, 405 | make_pair (i, j-1))); 406 | 407 | // Update the details of this cell 408 | cellDetails[i][j-1].f = fNew; 409 | cellDetails[i][j-1].g = gNew; 410 | cellDetails[i][j-1].h = hNew; 411 | cellDetails[i][j-1].parent_i = i; 412 | cellDetails[i][j-1].parent_j = j; 413 | } 414 | } 415 | } 416 | 417 | //----------- 5th Successor (North-East) ------------ 418 | 419 | // Only process this cell if this is a valid one 420 | if (isValid(i-1, j+1) == true) 421 | { 422 | // If the destination cell is the same as the 423 | // current successor 424 | if (isDestination(i-1, j+1, dest) == true) 425 | { 426 | // Set the Parent of the destination cell 427 | cellDetails[i-1][j+1].parent_i = i; 428 | cellDetails[i-1][j+1].parent_j = j; 429 | printf ("The destination cell is found\n"); 430 | tracePath (cellDetails, dest); 431 | foundDest = true; 432 | return; 433 | } 434 | 435 | // If the successor is already on the closed 436 | // list or if it is blocked, then ignore it. 437 | // Else do the following 438 | else if (closedList[i-1][j+1] == false && 439 | isUnBlocked(grid, i-1, j+1) == true) 440 | { 441 | gNew = cellDetails[i][j].g + 1.414; 442 | hNew = calculateHValue(i-1, j+1, dest); 443 | fNew = gNew + hNew; 444 | 445 | // If it isn’t on the open list, add it to 446 | // the open list. Make the current square 447 | // the parent of this square. Record the 448 | // f, g, and h costs of the square cell 449 | // OR 450 | // If it is on the open list already, check 451 | // to see if this path to that square is better, 452 | // using 'f' cost as the measure. 453 | if (cellDetails[i-1][j+1].f == FLT_MAX || 454 | cellDetails[i-1][j+1].f > fNew) 455 | { 456 | openList.insert( make_pair (fNew, 457 | make_pair(i-1, j+1))); 458 | 459 | // Update the details of this cell 460 | cellDetails[i-1][j+1].f = fNew; 461 | cellDetails[i-1][j+1].g = gNew; 462 | cellDetails[i-1][j+1].h = hNew; 463 | cellDetails[i-1][j+1].parent_i = i; 464 | cellDetails[i-1][j+1].parent_j = j; 465 | } 466 | } 467 | } 468 | 469 | //----------- 6th Successor (North-West) ------------ 470 | 471 | // Only process this cell if this is a valid one 472 | if (isValid (i-1, j-1) == true) 473 | { 474 | // If the destination cell is the same as the 475 | // current successor 476 | if (isDestination (i-1, j-1, dest) == true) 477 | { 478 | // Set the Parent of the destination cell 479 | cellDetails[i-1][j-1].parent_i = i; 480 | cellDetails[i-1][j-1].parent_j = j; 481 | printf ("The destination cell is found\n"); 482 | tracePath (cellDetails, dest); 483 | foundDest = true; 484 | return; 485 | } 486 | 487 | // If the successor is already on the closed 488 | // list or if it is blocked, then ignore it. 489 | // Else do the following 490 | else if (closedList[i-1][j-1] == false && 491 | isUnBlocked(grid, i-1, j-1) == true) 492 | { 493 | gNew = cellDetails[i][j].g + 1.414; 494 | hNew = calculateHValue(i-1, j-1, dest); 495 | fNew = gNew + hNew; 496 | 497 | // If it isn’t on the open list, add it to 498 | // the open list. Make the current square 499 | // the parent of this square. Record the 500 | // f, g, and h costs of the square cell 501 | // OR 502 | // If it is on the open list already, check 503 | // to see if this path to that square is better, 504 | // using 'f' cost as the measure. 505 | if (cellDetails[i-1][j-1].f == FLT_MAX || 506 | cellDetails[i-1][j-1].f > fNew) 507 | { 508 | openList.insert( make_pair (fNew, make_pair (i-1, j-1))); 509 | // Update the details of this cell 510 | cellDetails[i-1][j-1].f = fNew; 511 | cellDetails[i-1][j-1].g = gNew; 512 | cellDetails[i-1][j-1].h = hNew; 513 | cellDetails[i-1][j-1].parent_i = i; 514 | cellDetails[i-1][j-1].parent_j = j; 515 | } 516 | } 517 | } 518 | 519 | //----------- 7th Successor (South-East) ------------ 520 | 521 | // Only process this cell if this is a valid one 522 | if (isValid(i+1, j+1) == true) 523 | { 524 | // If the destination cell is the same as the 525 | // current successor 526 | if (isDestination(i+1, j+1, dest) == true) 527 | { 528 | // Set the Parent of the destination cell 529 | cellDetails[i+1][j+1].parent_i = i; 530 | cellDetails[i+1][j+1].parent_j = j; 531 | printf ("The destination cell is found\n"); 532 | tracePath (cellDetails, dest); 533 | foundDest = true; 534 | return; 535 | } 536 | 537 | // If the successor is already on the closed 538 | // list or if it is blocked, then ignore it. 539 | // Else do the following 540 | else if (closedList[i+1][j+1] == false && 541 | isUnBlocked(grid, i+1, j+1) == true) 542 | { 543 | gNew = cellDetails[i][j].g + 1.414; 544 | hNew = calculateHValue(i+1, j+1, dest); 545 | fNew = gNew + hNew; 546 | 547 | // If it isn’t on the open list, add it to 548 | // the open list. Make the current square 549 | // the parent of this square. Record the 550 | // f, g, and h costs of the square cell 551 | // OR 552 | // If it is on the open list already, check 553 | // to see if this path to that square is better, 554 | // using 'f' cost as the measure. 555 | if (cellDetails[i+1][j+1].f == FLT_MAX || 556 | cellDetails[i+1][j+1].f > fNew) 557 | { 558 | openList.insert(make_pair(fNew, 559 | make_pair (i+1, j+1))); 560 | 561 | // Update the details of this cell 562 | cellDetails[i+1][j+1].f = fNew; 563 | cellDetails[i+1][j+1].g = gNew; 564 | cellDetails[i+1][j+1].h = hNew; 565 | cellDetails[i+1][j+1].parent_i = i; 566 | cellDetails[i+1][j+1].parent_j = j; 567 | } 568 | } 569 | } 570 | 571 | //----------- 8th Successor (South-West) ------------ 572 | 573 | // Only process this cell if this is a valid one 574 | if (isValid (i+1, j-1) == true) 575 | { 576 | // If the destination cell is the same as the 577 | // current successor 578 | if (isDestination(i+1, j-1, dest) == true) 579 | { 580 | // Set the Parent of the destination cell 581 | cellDetails[i+1][j-1].parent_i = i; 582 | cellDetails[i+1][j-1].parent_j = j; 583 | printf("The destination cell is found\n"); 584 | tracePath(cellDetails, dest); 585 | foundDest = true; 586 | return; 587 | } 588 | 589 | // If the successor is already on the closed 590 | // list or if it is blocked, then ignore it. 591 | // Else do the following 592 | else if (closedList[i+1][j-1] == false && 593 | isUnBlocked(grid, i+1, j-1) == true) 594 | { 595 | gNew = cellDetails[i][j].g + 1.414; 596 | hNew = calculateHValue(i+1, j-1, dest); 597 | fNew = gNew + hNew; 598 | 599 | // If it isn’t on the open list, add it to 600 | // the open list. Make the current square 601 | // the parent of this square. Record the 602 | // f, g, and h costs of the square cell 603 | // OR 604 | // If it is on the open list already, check 605 | // to see if this path to that square is better, 606 | // using 'f' cost as the measure. 607 | if (cellDetails[i+1][j-1].f == FLT_MAX || 608 | cellDetails[i+1][j-1].f > fNew) 609 | { 610 | openList.insert(make_pair(fNew, 611 | make_pair(i+1, j-1))); 612 | 613 | // Update the details of this cell 614 | cellDetails[i+1][j-1].f = fNew; 615 | cellDetails[i+1][j-1].g = gNew; 616 | cellDetails[i+1][j-1].h = hNew; 617 | cellDetails[i+1][j-1].parent_i = i; 618 | cellDetails[i+1][j-1].parent_j = j; 619 | } 620 | } 621 | } 622 | } 623 | 624 | // When the destination cell is not found and the open 625 | // list is empty, then we conclude that we failed to 626 | // reach the destiantion cell. This may happen when the 627 | // there is no way to destination cell (due to blockages) 628 | if (foundDest == false) 629 | printf("Failed to find the Destination Cell\n"); 630 | 631 | return; 632 | } 633 | 634 | 635 | // Driver program to test above function 636 | int main() 637 | { 638 | /* Description of the Grid- 639 | 1--> The cell is not blocked 640 | 0--> The cell is blocked */ 641 | int grid[ROW][COL] = 642 | { 643 | { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, 644 | { 1, 1, 1, 0, 1, 1, 1, 0, 1, 1 }, 645 | { 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 }, 646 | { 0, 0, 1, 0, 1, 0, 0, 0, 0, 1 }, 647 | { 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 }, 648 | { 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 }, 649 | { 1, 0, 0, 0, 0, 1, 0, 0, 0, 1 }, 650 | { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, 651 | { 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 } 652 | }; 653 | 654 | // Source is the left-most bottom-most corner 655 | Pair src = make_pair(8, 0); 656 | 657 | // Destination is the left-most top-most corner 658 | Pair dest = make_pair(0, 0); 659 | 660 | aStarSearch(grid, src, dest); 661 | 662 | return(0); 663 | } 664 | -------------------------------------------------------------------------------- /Graph Theory/A Star/README.md: -------------------------------------------------------------------------------- 1 | # A* algorithm 2 | 3 | ## What is A* Search Algorithm? 4 | A* Search algorithm is one of the best and popular technique used in path-finding and graph traversals. 5 | 6 | ## Why A* Search Algorithm ? # 7 | Informally speaking, A* Search algorithms, unlike other traversal techniques, it has “brains”. What it means is that it is really a smart algorithm which separates it from the other conventional algorithms. This fact is cleared in detail in below sections. 8 | And it is also worth mentioning that many games and web-based maps use this algorithm to find the shortest path very efficiently (approximation). 9 | 10 | ### Explanation:- ## 11 | Consider a square grid having many obstacles and we are given a starting cell and a target cell. We want to reach the target cell (if possible) from the starting cell as quickly as possible. Here A* Search Algorithm comes to the rescue. 12 | 13 | What A* Search Algorithm does is that at each step it picks the node according to a value-‘f’ which is a parameter equal to the sum of two other parameters – ‘g’ and ‘h’. At each step it picks the node/cell having the lowest ‘f’, and process that node/cell. 14 | 15 | We define ‘g’ and ‘h’ as simply as possible below 16 | 17 | g = the movement cost to move from the starting point to a given square on the grid, following the path generated to get there. 18 | h = the estimated movement cost to move from that given square on the grid to the final destination. This is often referred to as the heuristic, which is nothing but a kind of smart guess. We really don’t know the actual distance until we find the path, because all sorts of things can be in the way (walls, water, etc.). There can be many ways to calculate this ‘h’ which are discussed in the later sections. 19 | 20 | 21 | ## Algorithm 22 | We create two lists – Open List and Closed List (just like Dijkstra Algorithm) 23 | 24 | ``` 25 | A* Search Algorithm 26 | 1. Initialize the open list 27 | 2. Initialize the closed list 28 | put the starting node on the open 29 | list (you can leave its f at zero) 30 | 31 | 3. while the open list is not empty 32 | a) find the node with the least f on 33 | the open list, call it "q" 34 | 35 | b) pop q off the open list 36 | 37 | c) generate q's 8 successors and set their 38 | parents to q 39 | 40 | d) for each successor 41 | 42 | i) if successor is the goal, stop search 43 | successor.g = q.g + distance between 44 | successor and q 45 | successor.h = distance from goal to 46 | successor (This can be done using many 47 | ways, we will discuss three heuristics- 48 | Manhattan, Diagonal and Euclidean 49 | Heuristics) 50 | 51 | successor.f = successor.g + successor.h 52 | 53 | ii) if a node with the same position as 54 | successor is in the OPEN list which has a 55 | lower f than successor, skip this successor 56 | 57 | iii) if a node with the same position as 58 | successor is in the CLOSED list which has 59 | a lower f than successor, skip this successor 60 | otherwise, add the node to the open list 61 | end (for loop) 62 | 63 | e) push q on the closed list 64 | end (while loop 65 | ``` 66 | -------------------------------------------------------------------------------- /Graph Theory/BFS/BFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | template 7 | class Graph{ 8 | map > l; 9 | 10 | public: 11 | void addEgde(int x, int y){ 12 | l[x].push_back(y); 13 | l[y].push_back(x); 14 | 15 | } 16 | 17 | void bfs(T src) 18 | { 19 | map visited; 20 | queue q; 21 | 22 | q.push(src); 23 | visited[src] = true; 24 | 25 | while(!q.empty()){ 26 | T node = q.front(); 27 | q.pop(); 28 | 29 | for (int nbr : l[node]) 30 | { 31 | if(!visited[nbr]){ 32 | q.push(nbr); 33 | //make that nbr as visited 34 | visited[nbr] = true; 35 | } 36 | } 37 | } 38 | } 39 | }; 40 | int main() 41 | { 42 | graph g; 43 | g.addEdge(0,1); 44 | g.addEdge(1,2); 45 | g.addEdge(2,3); 46 | g.addEdge(3,4); 47 | g.addEdge(4,5); 48 | 49 | g.bfs(0); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Graph Theory/BFS/README.md: -------------------------------------------------------------------------------- 1 | # BFS 2 | -------------------------------------------------------------------------------- /Graph Theory/Bellman Ford/README.md: -------------------------------------------------------------------------------- 1 | # Bellman Ford Algorithm 2 | -------------------------------------------------------------------------------- /Graph Theory/Bellman Ford/bellmanford.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | vector bellman_ford(int n, int src,vector > edges){ 6 | // Create vector 7 | vector dist(V+1, INT_MAX); 8 | dist[src]=0; 9 | //relax all edges v-1 times 10 | for( int i=0; i>n>>m; 39 | 40 | 41 | //Edge list 42 | vector > edges; // (a,b,3) (c,d,5)....... 43 | for(int i=0; i>u>>v>>wt; 46 | edges.push_back({u,v,wt}); 47 | } 48 | //bellman ford 49 | vector distances = bellman_ford(n,1,edges); 50 | for(int i=1; i<=n; i++){ 51 | cout<<"Node "< 2 | #include 3 | using namespace std; 4 | vector getPath(int **adjmatrix,int v,int v1,int v2,bool *visited) 5 | { 6 | if(v1==v2) 7 | { vector v; 8 | v.push_back(v1); 9 | return v; 10 | } 11 | //Explore paths from all unvisited neighbours of v1 12 | 13 | for(int vertex=0;vertex pathfromneigh= getPath(adjmatrix,v,vertex,v2,visited); 20 | if(pathfromneigh.size() > 0){ 21 | pathfromneigh.push_back(v1); 22 | return pathfromneigh;} 23 | } 24 | } 25 | vector path; 26 | return path; 27 | } 28 | 29 | vector getPath(int **adjmatrix,int v,int v1,int v2) 30 | { 31 | bool *visited=new bool[v](); 32 | visited[v1]=true; 33 | return getPath(adjmatrix,v,v1,v2,visited); 34 | } 35 | int main() { 36 | int V, E; 37 | cin >> V >> E; 38 | 39 | int **adjmatrix=new int*[V]; 40 | 41 | for(int i=0;i>v1>>v2; 50 | adjmatrix[v1][v2]=1; 51 | adjmatrix[v2][v1]=1; 52 | } 53 | //find path from vi to v2 54 | int v1,v2; 55 | cin>>v1>>v2; 56 | 57 | vector path=getPath(adjmatrix,V,v1,v2); 58 | int i=0; 59 | while(i 2 | using namespace std; 3 | 4 | // Detect cycle using DFS 5 | 6 | void dfs(vector g[],bool visited[],int source,int parent[],int *count, int V, int *temp){ 7 | visited[source] = true; 8 | *count = *count +1; 9 | if(*count!=V){ 10 | for(int x : g[source]){ 11 | if(visited[x]){ 12 | if(parent[source]!=x){ 13 | *temp = 1; 14 | } 15 | } 16 | else{ 17 | parent[x] = source; 18 | dfs(g,visited,x,parent,count,V,temp); 19 | } 20 | } 21 | } 22 | } 23 | 24 | // Function to check whether cycle is present or not 25 | 26 | bool detectCycle(vector g[], int V) 27 | { 28 | int parent[V]={0}; 29 | parent[0] = -1; 30 | bool visited[V] = {false}; 31 | int count = 0; 32 | int temp = 0; 33 | for(int i=0;i>n>>e; 51 | vector adj_list[n]; 52 | 53 | for(int i=0;i>u>>v; 55 | adj_list[u].push_back(v); 56 | adj_list[v].push_back(u); 57 | } 58 | 59 | cout << detectCycle(adj_list, n) < 2 | using namespace std; 3 | 4 | // DFS Algorithm 5 | void DFS(vector a[], stack &s,vector &visited, int i){ 6 | visited[i] = true; 7 | for(x : a[i]){ 8 | if(!visited[x]){ 9 | DFS(a,s,visited,x); 10 | s.push(x); 11 | }} 12 | 13 | } 14 | // Topological Sort 15 | // V is number of vertices and adj is adjacency list 16 | vector topologicalSort(int V, vector adj[]) { 17 | vector ts; 18 | vector visited(V,false); 19 | stack s; 20 | for(int i=0;i>n>>e; 39 | vector adj_list[n]; 40 | 41 | for(int i=0;i>u>>v; 43 | adj_list[u].push_back(v); 44 | } 45 | vector result = topologicalSort(n, adj_list); 46 | 47 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | template 6 | 7 | class Graph 8 | { 9 | unordered_map > >m; 10 | public: 11 | void addEdge(T u,T v,int dist,bool bidir=true) 12 | { 13 | m[u].push_back(make_pair(v,dist)); 14 | if(bidir) 15 | { 16 | m[v].push_back(make_pair(u,dist)); 17 | } 18 | } 19 | void printAdj() 20 | { 21 | for(auto j:m){ 22 | cout<"; 23 | for(auto l:j.second){ 24 | cout<<"("<dist; 31 | //set all the distance to infinity 32 | for(auto j:m){ 33 | dist[j.first]=INT_MAX; 34 | } 35 | //make a set to find a out node with minimum distance 36 | set>s; 37 | dist[src]=0; 38 | s.insert(make_pair(0,src)); 39 | while(!s.empty()){ 40 | //find the pair at front 41 | auto p=*(s.begin()); 42 | T node=p.second; 43 | int nodeDist=p.first; 44 | s.erase(s.begin()); 45 | //iterate over neighbours/children of the current node 46 | for(auto childPair:m[node]){ 47 | if(nodeDist+childPair.secondg; 71 | //number of edges 72 | int n; 73 | cin>>n; 74 | //u and v are the vertex and there is an edge connecting them whose weight/distance is dist 75 | int u,v,dist; 76 | 77 | for(int i=0;i>u>>v>>dist; 79 | g.addEdge(u,v,dist); 80 | } 81 | //print adjacency list 82 | g.printAdj(); 83 | //source from which we want to find distances to all other nodes 84 | int src; 85 | cin>>src; 86 | g.dijsktraSSSP(src); 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /Graph Theory/Dijkstra Algorithm/README.md: -------------------------------------------------------------------------------- 1 | # Dijkstra Algorithm 2 | 3 | You are given a directed or undirected weighted graph with n vertices and m edges. The weights of all edges are non-negative. You are also given a starting vertex s. This article discusses finding the lengths of the shortest paths from a starting vertex s to all other vertices, and output the shortest paths themselves. 4 | This problem is also called **single-source shortest paths problem**. 5 | 6 | ## Algorithm 7 | Let's create an array d[] where for each vertex v we store the current length of the shortest path from s to v in d[v]. Initially d[s]=0, and for all other vertices this length equals infinity. In the implementation a sufficiently large number (which is guaranteed to be greater than any possible path length) is chosen as infinity. 8 | 9 | ``` 10 | d[v]=∞, v≠s 11 | ``` 12 | In addition, we maintain a Boolean array u[] which stores for each vertex v whether it's marked. Initially all vertices are unmarked: 13 | ``` 14 | u[v]=false 15 | ``` 16 | The Dijkstra's algorithm runs for n iterations. At each iteration a vertex v is chosen as unmarked vertex which has the least value d[v]: 17 | 18 | Evidently, in the first iteration the starting vertex s will be selected. 19 | 20 | The selected vertex v is marked. Next, from vertex v relaxations are performed: all edges of the form (v,to) are considered, and for each vertex to the algorithm tries to improve the value d[to]. If the length of the current edge equals len, the code for relaxation is: 21 | ``` 22 | d[to]=min(d[to],d[v]+len) 23 | ``` 24 | After all such edges are considered, the current iteration ends. Finally, after n iterations, all vertices will be marked, and the algorithm terminates. We claim that the found values d[v] are the lengths of shortest paths from s to all vertices v. 25 | 26 | Note that if some vertices are unreachable from the starting vertex s, the values d[v] for them will remain infinite. Obviously, the last few iterations of the algorithm will choose those vertices, but no useful work will be done for them. Therefore, the algorithm can be stopped as soon as the selected vertex has infinite distance to it. 27 | 28 | **Restoring Shortest Paths** 29 | 30 | Usually one needs to know not only the lengths of shortest paths but also the shortest paths themselves. Let's see how to maintain sufficient information to restore the shortest path from s to any vertex. We'll maintain an array of predecessors p[] in which for each vertex v≠s p[v] is the penultimate vertex in the shortest path from s to v. Here we use the fact that if we take the shortest path to some vertex v and remove v from this path, we'll get a path ending in at vertex p[v], and this path will be the shortest for the vertex p[v]. This array of predecessors can be used to restore the shortest path to any vertex: starting with v, repeatedly take the predecessor of the current vertex until we reach the starting vertex s to get the required shortest path with vertices listed in reverse order. So, the shortest path P to the vertex v is equal to: 31 | 32 | ``` 33 | P=(s,…,p[p[p[v]]],p[p[v]],p[v],v) 34 | ``` 35 | Building this array of predecessors is very simple: for each successful relaxation, i.e. when for some selected vertex v, there is an improvement in the distance to some vertex to, we update the predecessor vertex for to with vertex v: 36 | 37 | ``` 38 | p[to]=v 39 | ``` 40 | **Time Complexity of Dijkstra Algorithm** 41 | 42 | Time complexity of Dijkstra's Algorithm changes with the method of implementation of the priority queue. Using Min Heap time complexity of O(V + E log V) can be acheived. Using array implementation of priority queue time complexity of O(V^2) can be acheived. Most efficient implemenatation of priority queue in terms of time complexity is using Fibonacci Heap which yeilds results in O(E + V log V). 43 | -------------------------------------------------------------------------------- /Graph Theory/Kruskals MST/Kruskal_MST.cpp: -------------------------------------------------------------------------------- 1 | // C++ program for Kruskal's algorithm to find Minimum Spanning Tree 2 | // of a given connected, undirected and weighted graph 3 | #include 4 | using namespace std; 5 | 6 | // a structure to represent a weighted edge in graph 7 | class Edge 8 | { 9 | public: 10 | int src, dest, weight; 11 | }; 12 | 13 | // a structure to represent a connected, undirected 14 | // and weighted graph 15 | class Graph 16 | { 17 | public: 18 | // V-> Number of vertices, E-> Number of edges 19 | int V, E; 20 | 21 | // graph is represented as an array of edges. 22 | // Since the graph is undirected, the edge 23 | // from src to dest is also edge from dest 24 | // to src. Both are counted as 1 edge here. 25 | Edge* edge; 26 | }; 27 | 28 | // Creates a graph with V vertices and E edges 29 | Graph* createGraph(int V, int E) 30 | { 31 | Graph* graph = new Graph; 32 | graph->V = V; 33 | graph->E = E; 34 | 35 | graph->edge = new Edge[E]; 36 | 37 | return graph; 38 | } 39 | 40 | // A structure to represent a subset for union-find 41 | class subset 42 | { 43 | public: 44 | int parent; 45 | int rank; 46 | }; 47 | 48 | // A utility function to find set of an element i 49 | // (uses path compression technique) 50 | int find(subset subsets[], int i) 51 | { 52 | // find root and make root as parent of i 53 | // (path compression) 54 | if (subsets[i].parent != i) 55 | subsets[i].parent = find(subsets, subsets[i].parent); 56 | 57 | return subsets[i].parent; 58 | } 59 | 60 | // A function that does union of two sets of x and y 61 | // (uses union by rank) 62 | void Union(subset subsets[], int x, int y) 63 | { 64 | int xroot = find(subsets, x); 65 | int yroot = find(subsets, y); 66 | 67 | // Attach smaller rank tree under root of high 68 | // rank tree (Union by Rank) 69 | if (subsets[xroot].rank < subsets[yroot].rank) 70 | subsets[xroot].parent = yroot; 71 | else if (subsets[xroot].rank > subsets[yroot].rank) 72 | subsets[yroot].parent = xroot; 73 | 74 | // If ranks are same, then make one as root and 75 | // increment its rank by one 76 | else 77 | { 78 | subsets[yroot].parent = xroot; 79 | subsets[xroot].rank++; 80 | } 81 | } 82 | 83 | // Compare two edges according to their weights. 84 | // Used in qsort() for sorting an array of edges 85 | int myComp(const void* a, const void* b) 86 | { 87 | Edge* a1 = (Edge*)a; 88 | Edge* b1 = (Edge*)b; 89 | return a1->weight > b1->weight; 90 | } 91 | 92 | // The main function to construct MST using Kruskal's algorithm 93 | void KruskalMST(Graph* graph) 94 | { 95 | int V = graph->V; 96 | Edge result[V]; // Tnis will store the resultant MST 97 | int e = 0; // An index variable, used for result[] 98 | int i = 0; // An index variable, used for sorted edges 99 | 100 | // Step 1: Sort all the edges in non-decreasing 101 | // order of their weight. If we are not allowed to 102 | // change the given graph, we can create a copy of 103 | // array of edges 104 | qsort(graph->edge, graph->E, sizeof(graph->edge[0]), myComp); 105 | 106 | // Allocate memory for creating V ssubsets 107 | subset *subsets = new subset[( V * sizeof(subset) )]; 108 | 109 | // Create V subsets with single elements 110 | for (int v = 0; v < V; ++v) 111 | { 112 | subsets[v].parent = v; 113 | subsets[v].rank = 0; 114 | } 115 | 116 | // Number of edges to be taken is equal to V-1 117 | while (e < V - 1 && i < graph->E) 118 | { 119 | // Step 2: Pick the smallest edge. And increment 120 | // the index for next iteration 121 | Edge next_edge = graph->edge[i++]; 122 | 123 | int x = find(subsets, next_edge.src); 124 | int y = find(subsets, next_edge.dest); 125 | 126 | // If including this edge does't cause cycle, 127 | // include it in result and increment the index 128 | // of result for next edge 129 | if (x != y) 130 | { 131 | result[e++] = next_edge; 132 | Union(subsets, x, y); 133 | } 134 | // Else discard the next_edge 135 | } 136 | 137 | // print the contents of result[] to display the 138 | // built MST 139 | cout<<"Following are the edges in the constructed MST\n"; 140 | for (i = 0; i < e; ++i) 141 | cout<edge[0].src = 0; 163 | graph->edge[0].dest = 1; 164 | graph->edge[0].weight = 10; 165 | 166 | // add edge 0-2 167 | graph->edge[1].src = 0; 168 | graph->edge[1].dest = 2; 169 | graph->edge[1].weight = 6; 170 | 171 | // add edge 0-3 172 | graph->edge[2].src = 0; 173 | graph->edge[2].dest = 3; 174 | graph->edge[2].weight = 5; 175 | 176 | // add edge 1-3 177 | graph->edge[3].src = 1; 178 | graph->edge[3].dest = 3; 179 | graph->edge[3].weight = 15; 180 | 181 | // add edge 2-3 182 | graph->edge[4].src = 2; 183 | graph->edge[4].dest = 3; 184 | graph->edge[4].weight = 4; 185 | 186 | KruskalMST(graph); 187 | 188 | return 0; 189 | } 190 | 191 | // This code is contributed by rathbhupendra 192 | -------------------------------------------------------------------------------- /Graph Theory/Kruskals MST/README.md: -------------------------------------------------------------------------------- 1 | # Kruskal’s Minimum Spanning Tree -------------------------------------------------------------------------------- /Graph Theory/Prims MST/README.md: -------------------------------------------------------------------------------- 1 | # Prim’s Minimum Spanning Tree (MST) -------------------------------------------------------------------------------- /Graph Theory/Prims MST/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Number of vertices in the graph 5 | #define V 5 6 | 7 | // A utility function to find the vertex with 8 | // minimum key value, from the set of vertices 9 | // not yet included in MST 10 | int minKey(int key[], bool mstSet[]) 11 | { 12 | // Initialize min value 13 | int min = INT_MAX, min_index; 14 | 15 | for (int v = 0; v < V; v++) 16 | if (mstSet[v] == false && key[v] < min) 17 | min = key[v], min_index = v; 18 | 19 | return min_index; 20 | } 21 | 22 | // A utility function to print the 23 | // constructed MST stored in parent[] 24 | void printMST(int parent[], int graph[V][V]) 25 | { 26 | cout<<"Edge \tWeight\n"; 27 | for (int i = 1; i < V; i++) 28 | cout< 3 | #include 4 | using namespace std; 5 | 6 | typedef struct { 7 | int value; 8 | int weight; 9 | float density; 10 | }Item; 11 | // input function 12 | void input(Item items[],int sizeOfItems){ 13 | cout << "Enter total "<< sizeOfItems <<" item's values and weight" << endl; 14 | for(int i=0; i> items[i].value; 17 | cout << "Enter "<< i+1 << " Weight "; 18 | cin >> items[i].weight; 19 | } 20 | } 21 | // display or print function 22 | void display(Item items[],int sizeOfItems){ 23 | int i; 24 | cout << "values: "; 25 | for(i=0; i i2.density); 37 | } 38 | // Knapsack problem function 39 | float knapsack(Item items[],int sizeOfItems, int W){ 40 | int i, j, pos; 41 | Item mx,temp; 42 | float totalValue=0, totalWeight=0; 43 | for(i=0; i> W; 72 | float mxVal = knapsack(items,3,W); 73 | cout << "---Max value for "<< W <<" weight is "<< mxVal; 74 | 75 | } -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /LinkedList/DLL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node 5 | { 6 | node* prev; 7 | int num; 8 | node* next; 9 | }; 10 | class DLL 11 | { 12 | public: 13 | node *head; 14 | DLL() 15 | { head=NULL; } 16 | void insertfirst(); 17 | void insertlast(); 18 | void deletefirst(); 19 | void deletelast(); 20 | void display(); 21 | }; 22 | 23 | void DLL::insertfirst() 24 | { 25 | node *ptr; 26 | ptr=new node; 27 | cout<<"Enter number\n"; 28 | cin>>ptr->num; 29 | if(head==NULL) 30 | { head=ptr; } 31 | else 32 | { 33 | ptr->next=head; 34 | ptr->prev=NULL; 35 | head=ptr; 36 | } 37 | } 38 | 39 | void DLL::insertlast() 40 | { 41 | node *ptr; 42 | ptr=new node; 43 | cout<<"Enter number\n"; 44 | cin>>ptr->num; 45 | if(head==NULL) 46 | { head=ptr; } 47 | else 48 | { 49 | node *temp=head; 50 | while(temp->next!=NULL) 51 | temp=temp->next; 52 | temp->next=ptr; 53 | 54 | ptr->next=head; 55 | head=ptr; 56 | } 57 | } 58 | 59 | void DLL::deletefirst() 60 | { 61 | if(head==NULL) 62 | { return; } 63 | else 64 | { 65 | node *t=head; 66 | head=head->next; 67 | t->next=NULL; 68 | head->prev=NULL; 69 | delete t; 70 | t=NULL; 71 | } 72 | } 73 | 74 | void DLL::deletelast() 75 | { 76 | if(head==NULL) 77 | { return; } 78 | else 79 | { 80 | node *t=head; 81 | node *p=NULL; 82 | while(t->next) 83 | { 84 | p=t; 85 | t=t->next; 86 | } 87 | p->next=NULL; 88 | t->prev=NULL; 89 | delete t; 90 | t=NULL; 91 | } 92 | } 93 | 94 | void DLL::display() 95 | { 96 | cout<num<next; 110 | count++; 111 | } 112 | cout< 3 | using namespace std; 4 | 5 | struct node 6 | { 7 | char name[20]; 8 | long int PRN; 9 | node* next; 10 | }; 11 | 12 | class SLL 13 | { 14 | public: 15 | node *head; 16 | SLL() 17 | { head=NULL; } 18 | void insertNode(); 19 | void insertfirst(); 20 | void insertlast(); 21 | void display(); 22 | void deletefirst(); 23 | void deletelast(); 24 | void deleteNode(); 25 | void reverseDisplay(node* head); 26 | }; 27 | 28 | void SLL::insertNode() 29 | { 30 | node *ptr; 31 | ptr=new node; 32 | cout<<"Enter name and PRN number\n"; 33 | cin>>ptr->name>>ptr->PRN; 34 | cout<<"Enter the position to add\t"; 35 | int p,count=0; 36 | cin>>p; 37 | ptr->next=NULL; 38 | if(head==NULL) 39 | { head=ptr; } 40 | else 41 | { 42 | node *t=head; 43 | //count++; 44 | while(count<(p-1) && t) 45 | { 46 | t=t->next; 47 | count++; 48 | } 49 | ptr->next=t->next; 50 | t->next=ptr; 51 | } 52 | } 53 | 54 | void SLL::insertfirst() 55 | { 56 | node *ptr; 57 | ptr=new node; 58 | cout<<"Enter name and PRN number\n"; 59 | cin>>ptr->name>>ptr->PRN; 60 | if(head==NULL) 61 | { head=ptr; } 62 | else 63 | { 64 | ptr->next=head; 65 | head=ptr; 66 | } 67 | } 68 | 69 | void SLL::insertlast() 70 | { 71 | node *ptr; 72 | ptr=new node; 73 | cout<<"Enter name and PRN number\n"; 74 | cin>>ptr->name>>ptr->PRN; 75 | if(head==NULL) 76 | { head=ptr; } 77 | else 78 | { 79 | node *temp=head; 80 | while(temp->next!=NULL) 81 | temp=temp->next; 82 | temp->next=ptr; 83 | } 84 | } 85 | 86 | void SLL::display() 87 | { 88 | cout<PRN<<"\t"<name<next; 102 | count++; 103 | } 104 | cout<>pos; 118 | node *p,*t; 119 | t=head; 120 | while(countnext; 124 | count++; 125 | } 126 | p->next=t->next; 127 | delete t; 128 | t=NULL; 129 | } 130 | } 131 | 132 | void SLL::deletefirst() 133 | { 134 | if(head==NULL) 135 | { return; } 136 | else 137 | { 138 | node *t=head; 139 | head=head->next; 140 | t->next=NULL; 141 | delete t; 142 | } 143 | } 144 | 145 | void SLL::deletelast() 146 | { 147 | if(head==NULL) 148 | { return; } 149 | else 150 | { 151 | node *t=head; 152 | node *p=NULL; 153 | while(t->next) 154 | { 155 | p=t; 156 | t=t->next; 157 | } 158 | p->next=NULL; 159 | delete t; 160 | t=NULL; 161 | } 162 | } 163 | 164 | void SLL::reverseDisplay(node* head) 165 | { 166 | if(!head) 167 | { return; } 168 | else 169 | { 170 | reverseDisplay(head->next); 171 | cout<PRN<<"\t"<name<>choice; 185 | switch (choice) 186 | { 187 | case 1: 188 | p1.display(); 189 | break; 190 | case 2: 191 | p1.insertfirst(); 192 | break; 193 | case 3: 194 | p1.insertlast(); 195 | break; 196 | case 4: 197 | p1.insertNode(); 198 | break; 199 | case 5: 200 | p1.reverseDisplay(p1.head); 201 | break; 202 | case 6: 203 | p1.deletefirst(); 204 | break; 205 | case 7: 206 | p1.deletelast(); 207 | break; 208 | case 8: 209 | p1.deleteNode(); 210 | break; 211 | case 9: 212 | cout<<"Thank you.\n"; 213 | break; 214 | default: 215 | cout<<"Invalid choice, Please try again.\n"; 216 | break; 217 | } 218 | }while(choice!=9); 219 | 220 | return 0; 221 | } 222 | -------------------------------------------------------------------------------- /Miscellaneous/Largest Prime Factor/largest_prime_factor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | long long int max_prime_factor(long long int n){ 7 | long long int max_prime = -1; 8 | 9 | while(n % 2 == 0){ 10 | max_prime = 2; 11 | n >>= 1; 12 | } 13 | 14 | for(int i = 3; i <= int(sqrt(n)); i+= 2){ 15 | while(n % i == 0){ 16 | max_prime = i; 17 | n /= i; 18 | } 19 | 20 | } 21 | 22 | if(n > 2){ 23 | max_prime = n; 24 | } 25 | 26 | return max_prime; 27 | 28 | } 29 | 30 | int main(void){ 31 | long long int n; 32 | 33 | cin >> n; 34 | cout << "Max Prime Factor of number is: " << max_prime_factor(n); 35 | 36 | cin.get(); 37 | cin.ignore(); 38 | } -------------------------------------------------------------------------------- /Miscellaneous/Largest Prime Factor/test: -------------------------------------------------------------------------------- 1 | test 2 | -------------------------------------------------------------------------------- /Miscellaneous/Largest Product of a series/test: -------------------------------------------------------------------------------- 1 | test 2 | -------------------------------------------------------------------------------- /Miscellaneous/Largest Product of a series/thirteen_digits.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | in_1 = "73167176531330624919225119674426574742355349194934" 4 | in_2 = "96983520312774506326239578318016984801869478851843" 5 | in_3 = "85861560789112949495459501737958331952853208805511" 6 | in_4 = "12540698747158523863050715693290963295227443043557" 7 | in_5 = "66896648950445244523161731856403098711121722383113" 8 | in_6 = "62229893423380308135336276614282806444486645238749" 9 | in_7 = "30358907296290491560440772390713810515859307960866" 10 | in_8 = "70172427121883998797908792274921901699720888093776" 11 | in_9 = "65727333001053367881220235421809751254540594752243" 12 | in_10 = "52584907711670556013604839586446706324415722155397" 13 | in_11 = "53697817977846174064955149290862569321978468622482" 14 | in_12 = "83972241375657056057490261407972968652414535100474" 15 | in_13 = "82166370484403199890008895243450658541227588666881" 16 | in_14 = "16427171479924442928230863465674813919123162824586" 17 | in_15 = "17866458359124566529476545682848912883142607690042" 18 | in_16 = "24219022671055626321111109370544217506941658960408" 19 | in_17 = "07198403850962455444362981230987879927244284909188" 20 | in_18 = "84580156166097919133875499200524063689912560717606" 21 | in_19 = "05886116467109405077541002256983155200055935729725" 22 | in_20 = "71636269561882670428252483600823257530420752963450" 23 | 24 | in_number = in_1 + in_2 + in_3 + in_4 + in_5 + in_6 + in_7 + in_8 + in_9 + in_10 + in_11 + in_12 + in_13 + in_14 + in_15 + in_16 + in_17 + in_18 + in_19 + in_20 25 | 26 | max_index = 20 27 | max_prod = 0 28 | 29 | i = 0 30 | j = 0 31 | print(int(in_number[0])) 32 | for i in range(0, 986): 33 | prod = 1 34 | for j in range(i, i + 13): 35 | prod = prod * int(in_number[j]) 36 | if(prod > max_prod): 37 | max_prod = prod 38 | 39 | print(max_prod) -------------------------------------------------------------------------------- /Miscellaneous/Luhn's Algorithm/luhn.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Apr 20 13:51:06 2020 4 | 5 | @author: vgadi 6 | """ 7 | 8 | import numpy as np 9 | import pandas as pd 10 | 11 | def get_even_odd_indexed_digits(credit_card_number, len_number): 12 | odd_list = [] 13 | even_list = [] 14 | for i in range(0, len_number): 15 | if i%2 == 1: 16 | odd_list.append(int(credit_card_number[i])) 17 | else: 18 | even_list.append(int(credit_card_number[i])) 19 | return np.array(odd_list, dtype = int), np.array(even_list, dtype = int) 20 | 21 | def odd_indexed_array_operations(array_odd): 22 | list_modulo = [] 23 | len_odd = len(array_odd) 24 | array_odd = 2*array_odd 25 | for i in range(0, len_odd): 26 | if(int(array_odd[i]/(10)) == 0): 27 | list_modulo.append(array_odd[i]) 28 | else: 29 | list_modulo.append(int(array_odd[i]%10)) 30 | list_modulo.append(int(array_odd[i]/10)) 31 | return np.array(list_modulo, dtype = int) 32 | 33 | 34 | def get_sum(array_modulo, array_even): 35 | sum_modulo = np.sum(array_modulo, axis = 0) 36 | sum_even = np.sum(array_even, axis = 0) 37 | sum_required = sum_even + sum_modulo 38 | return sum_required 39 | 40 | def validate_credit_card(credit_card_number): 41 | len_number = len(credit_card_number) 42 | 43 | array_odd, array_even = get_even_odd_indexed_digits(credit_card_number, len_number) 44 | 45 | array_modulo = odd_indexed_array_operations(array_odd) 46 | 47 | sum_required = get_sum(array_modulo, array_even) 48 | 49 | if(int(sum_required % 10) == 0): 50 | return 1 51 | else: 52 | return 0 53 | 54 | amex = "378282246310000" 55 | 56 | if(validate_credit_card(amex) == 1): 57 | print("Credit card is legit!") 58 | else: 59 | print("Credit card is faulty") 60 | 61 | #def luhnalg(num): 62 | # a=num 63 | # count=0 64 | # while a>=1: 65 | # a//=10 66 | # count+=1 67 | # l=0 68 | # m=0 69 | # n=0 70 | # o=0 71 | # p=0 72 | # for i in range(2,count+1,2): 73 | # k=i-1 74 | # digit=num%10**i 75 | # digit=digit//10**k 76 | # digit*=2 77 | # if digit>10: 78 | # l=digit 79 | # m=l%10 80 | # n=l//10 81 | # o=m+n 82 | # else: 83 | # p+=digit 84 | # total=o+p 85 | # 86 | # t=0 87 | # for i1 in range(1,count+1,2): 88 | # k1=i1-1 89 | # dig=num%10**i1 90 | # dig=dig//10**k1 91 | # t=+dig 92 | # 93 | # grandtot=total+t 94 | # 95 | # if grandtot%10==0: 96 | # print('valid') 97 | # else: 98 | # print('Not valid') 99 | # 100 | # 101 | #luhnalg(378282246310000) 102 | # 103 | # 104 | -------------------------------------------------------------------------------- /Miscellaneous/Luhn's Algorithm/test: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Miscellaneous/Palindrome Product/palin_product.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isPalindrome(int num1){ 5 | int num2, digit, rev = 0; 6 | num1 = 0; 7 | 8 | num2 = num1; 9 | do 10 | { 11 | digit = num1 % 10; 12 | rev = (rev * 10) + digit; 13 | num1 = num1 / 10; 14 | } while (num1 != 0); 15 | 16 | if (num2 == rev){ 17 | return true; 18 | } 19 | else{ 20 | return false; 21 | } 22 | } 23 | 24 | long long int palidrome_prod(){ 25 | long long int bigPalin = -1; 26 | int n1, n2; 27 | for(n1 = 100; n1 < 1000; n1++){ 28 | for(n2 = 100; n2<1000;n2++){ 29 | long long int prod = n1*n2; 30 | if(prod > bigPalin){ 31 | if(isPalindrome(prod)){ 32 | bigPalin = prod; 33 | } 34 | } 35 | } 36 | } 37 | if(bigPalin != -1){ 38 | return bigPalin; 39 | } 40 | return -1; 41 | } 42 | 43 | int main(){ 44 | cout << palidrome_prod(); 45 | cin.get(); 46 | cin.ignore(); 47 | } -------------------------------------------------------------------------------- /Miscellaneous/Palindrome Product/test: -------------------------------------------------------------------------------- 1 | test 2 | -------------------------------------------------------------------------------- /Miscellaneous/test: -------------------------------------------------------------------------------- 1 | test 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Algorithms 2 | 3 | ![Algorithms](./images/algorithms.jpg) 4 | 5 | An attempt to aggregate all the various algorithms used in CS. 6 | 7 | ## What you will gain by contributing here? 8 | 9 | - How to contribute to open source 10 | - Version Control 11 | - Since we have quite a few branches, you will learn how to work with branches 12 | - Overall, a great learning experience for y'all! 13 | 14 | ## Branches 15 | 16 | | Branch | Language | 17 | | ----------- | ----------- | 18 | | master | C++ | 19 | | python | Python | 20 | | java | Java | 21 | 22 | ## How to contribute? 23 | 24 | Please check [CONTRIBUTING.md](./CONTRIBUTING.md) for further instructions. 25 | 26 | ### NOTE: 27 | 28 | All issues are open issues. If there are multiple PR's trying to solve an issue, they will be merged on a first-come, first-serve basis. The PR which will be passing all the criteria mentioned in [CONTRIBUTING.md](./CONTRIBUTING.md) would be getting merged. 29 | 30 | **If you want, give us a star ⭐!** 31 | -------------------------------------------------------------------------------- /Randomized Algorithms/Armstrong's Permutation/Permute_By_Cylic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | using namespace std::chrono; 9 | 10 | void get_input(int *arr, int n) { 11 | for (int i = 0; i < n; i++) { 12 | cin >> arr[i]; 13 | } 14 | } 15 | 16 | void permute_by_cyclic(int *arr, int n) { 17 | srand(time(0)); 18 | int *b = new int[n]; 19 | int *c; 20 | int offset = rand() % n; 21 | 22 | for (int i = 0; i < n; i++) { 23 | int dest = i + offset; 24 | if (dest > n) { 25 | dest -= n; 26 | } 27 | b[dest] = arr[i]; 28 | } 29 | 30 | c = arr; 31 | c = NULL; 32 | arr = b; 33 | } 34 | 35 | void display(int *arr, int n) { 36 | for (int i = 0; i < n; i++) { 37 | cout << arr[i] << " "; 38 | } 39 | } 40 | int main() 41 | { 42 | int n, *arr; 43 | 44 | cout << "Number of elements: "; 45 | cin >> n; 46 | 47 | arr = new int[n]; 48 | 49 | get_input(arr, n); 50 | 51 | permute_by_cyclic(arr, n); 52 | 53 | cout << "The randomized elements are: " << endl; 54 | display(arr, n); 55 | } 56 | -------------------------------------------------------------------------------- /Randomized Algorithms/Armstrong's Permutation/README.md: -------------------------------------------------------------------------------- 1 | # Armstrong's Permutation 2 | 3 | - Aditya Mandke 4 | -------------------------------------------------------------------------------- /Randomized Algorithms/Armstrong's Permutation/test: -------------------------------------------------------------------------------- 1 | test 2 | -------------------------------------------------------------------------------- /Randomized Algorithms/Permute In Place/Permute_In_Place.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | using namespace std::chrono; 9 | 10 | void swap(int *xp, int *yp) { 11 | int temp = *xp; 12 | *xp = *yp; 13 | *yp = temp; 14 | } 15 | 16 | void permute(int *arr, int n) { 17 | srand(time(0)); 18 | int j = 0; 19 | for (int i = 0; i < n; i++) { 20 | j = rand() % n; 21 | swap(&arr[i], &arr[j]); 22 | } 23 | } 24 | 25 | void get_input(int *arr, int n) { 26 | cout << "Elements: " << endl; 27 | for (int i = 0; i < n; i++) { 28 | cin >> arr[i]; 29 | } 30 | } 31 | 32 | void display(int *arr, int n) { 33 | for (int i = 0; i < n; i++) { 34 | cout << arr[i] << " "; 35 | } 36 | } 37 | 38 | int main() 39 | { 40 | int n, *arr; 41 | cout << "Number of elements: "; 42 | cin >> n; 43 | 44 | arr = new int[n]; 45 | 46 | get_input(arr, n); 47 | 48 | auto start = high_resolution_clock::now(); 49 | permute(arr, n); 50 | auto stop = high_resolution_clock::now(); 51 | auto duration = duration_cast(stop - start); 52 | cout << "The running time of algorithm is: " << duration.count() << "ns" << endl; 53 | 54 | cout << "The randomized list of elements is: "; 55 | display(arr, n); 56 | 57 | cout << "\nPress any key to exit "; 58 | cin.get(); 59 | cin.ignore(); 60 | } 61 | -------------------------------------------------------------------------------- /Randomized Algorithms/Permute In Place/README.md: -------------------------------------------------------------------------------- 1 | # Permute in Place 2 | 3 | - Aditya Mandke 4 | -------------------------------------------------------------------------------- /Randomized Algorithms/Permute In Place/Simple_Permute.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ekdnam/Algorithms/22b9c893e154e322573597efb2c8ef0181abdae2/Randomized Algorithms/Permute In Place/Simple_Permute.exe -------------------------------------------------------------------------------- /Randomized Algorithms/Permute In Place/test: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Randomized Algorithms/Permute Without Identity/Permute_Without_Identity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | using namespace std::chrono; 9 | 10 | void swap(int *xp, int *yp) { 11 | int temp = *xp; 12 | *xp = *yp; 13 | *yp = temp; 14 | } 15 | 16 | void get_input(int *arr, int n) { 17 | for (int i = 0; i < n; i++) { 18 | cin >> arr[i]; 19 | } 20 | } 21 | 22 | void permute_without_identity(int *arr, int n) { 23 | 24 | srand(time(0)); 25 | 26 | for (int i = 0; i < n; i++) { 27 | swap(&arr[i], &arr[(rand() % (n - i)) + i]); 28 | } 29 | } 30 | 31 | void display(int *arr, int n) { 32 | for (int i = 0; i < n; i++) { 33 | cout << arr[i] << " "; 34 | } 35 | } 36 | 37 | int main() 38 | { 39 | int n, *arr; 40 | cout << "Number of elements: "; 41 | cin >> n; 42 | 43 | arr = new int[n]; 44 | 45 | cout << "Elements: " << endl; 46 | get_input(arr, n); 47 | 48 | auto start = high_resolution_clock::now(); 49 | permute_without_identity(arr, n); 50 | auto stop = high_resolution_clock::now(); 51 | auto duration = duration_cast(stop - start); 52 | cout << "Running time of algorithm is: " << duration.count() << "ns " << endl; 53 | 54 | cout << "The randomized array is: " << endl; 55 | display(arr, n); 56 | } 57 | -------------------------------------------------------------------------------- /Randomized Algorithms/Permute Without Identity/Permute_Without_Identity.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ekdnam/Algorithms/22b9c893e154e322573597efb2c8ef0181abdae2/Randomized Algorithms/Permute Without Identity/Permute_Without_Identity.exe -------------------------------------------------------------------------------- /Randomized Algorithms/Permute Without Identity/README.md: -------------------------------------------------------------------------------- 1 | # Permute without identity 2 | 3 | - Aditya Mandke 4 | -------------------------------------------------------------------------------- /Randomized Algorithms/Permute Without Identity/test: -------------------------------------------------------------------------------- 1 | test 2 | -------------------------------------------------------------------------------- /Randomized Algorithms/README.md: -------------------------------------------------------------------------------- 1 | # Randomized Algorithms 2 | 3 | Creating random permutations from a given array. 4 | 5 | - Aditya Mandke 6 | -------------------------------------------------------------------------------- /Randomized Algorithms/test: -------------------------------------------------------------------------------- 1 | test 2 | -------------------------------------------------------------------------------- /Searching/Binary Search/BinarySearch-1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ekdnam/Algorithms/22b9c893e154e322573597efb2c8ef0181abdae2/Searching/Binary Search/BinarySearch-1.jpg -------------------------------------------------------------------------------- /Searching/Binary Search/Binary_Search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "arrayio.h" 3 | #include 4 | 5 | 6 | using namespace std; 7 | using namespace std::chrono; 8 | 9 | void swap(int *xp, int *yp) { 10 | int temp = *xp; 11 | *xp = *yp; 12 | *yp = temp; 13 | } 14 | 15 | int partition(int *arr, int p, int r) { 16 | int x = arr[r]; 17 | int i = p - 1; 18 | int j = p; 19 | for (int j = p; j <= r - 1; j++) { 20 | if (arr[j] <= x) { 21 | i += 1; 22 | swap(&arr[i], &arr[j]); 23 | } 24 | } 25 | swap(&arr[i + 1], &arr[r]); 26 | return i + 1; 27 | } 28 | 29 | void quicksort(int *arr, int p, int r) { 30 | if (p < r) { 31 | int q = partition(arr, p, r); 32 | quicksort(arr, p, q - 1); 33 | quicksort(arr, q + 1, r); 34 | } 35 | } 36 | 37 | int binary_search(int *arr, int low, int high, int key) { 38 | 39 | if (high >= low) { 40 | int mid = low + (high - low) / 2; 41 | if (key == arr[mid]) { 42 | return mid; 43 | } 44 | else if (arr[mid] > key) { 45 | return binary_search(arr, low, mid - 1, key); 46 | } 47 | else{ 48 | return binary_search(arr, mid + 1, high, key); 49 | } 50 | } 51 | return -1; 52 | } 53 | 54 | 55 | int main() 56 | { 57 | int n, *arr, key; 58 | cout << "Number of elements: "; 59 | cin >> n; 60 | 61 | arr = new int[n]; 62 | 63 | cout << "Enter elements: "; 64 | get_input(arr, n); 65 | 66 | cout << "Enter element to be found: "; 67 | cin >> key; 68 | 69 | quicksort(arr, 0, n-1); 70 | auto start = high_resolution_clock::now(); 71 | int index = binary_search(arr, 0, n-1, key); 72 | auto stop = high_resolution_clock::now(); 73 | auto duration = duration_cast(stop - start); 74 | 75 | if (index == -1) { 76 | cout << "Element not found." << endl; 77 | } 78 | else { 79 | cout << "Element found at position: " << index << endl; 80 | } 81 | cout << "Running time of binary search is: " << duration.count() << " microseconds." << endl; 82 | 83 | cout << "\nPress anny key to exit "; 84 | cin.get(); 85 | cin.ignore(); 86 | } 87 | 88 | // Run program: Ctrl + F5 or Debug > Start Without Debugging menu 89 | // Debug program: F5 or Debug > Start Debugging menu 90 | 91 | // Tips for Getting Started: 92 | // 1. Use the Solution Explorer window to add/manage files 93 | // 2. Use the Team Explorer window to connect to source control 94 | // 3. Use the Output window to see build output and other messages 95 | // 4. Use the Error List window to view errors 96 | // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project 97 | // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file 98 | -------------------------------------------------------------------------------- /Searching/Binary Search/README.md: -------------------------------------------------------------------------------- 1 | # Binary Search 2 | 3 | - Binary Search works on the principle of divide and conquer. 4 | - For this algorithm to work properly, the data collection should be in the sorted form. 5 | - The time complexity is O(Log n). 6 | 7 | 8 | ## Algorithm 9 | Let the searched element be x. 10 | Find the middle of the array, divide the array in half. 11 | Search a sorted array by repeatedly dividing the search interval in half. 12 | Begin with an interval covering the whole array.If the value of the search 13 | key is less than the item in the middle of the interval, narrow the interval to the first lower half. 14 | Otherwise narrow it to the second upper half 15 | 16 | 1.Compare x with the middle element. 17 | 2.If x matches with middle element, we return the mid index. 18 | 3.Else If x is greater than the mid element, then x can only lie in right half subarray after the mid element. So we recur for right half. 19 | 4.Else (x is smaller) recur for the left half 20 | 21 | 22 | ## High Level Flow Of The Algorithm 23 | 24 | ![BinarySearch](BinarySearch-1.jpg) 25 | 26 | -------------------------------------------------------------------------------- /Searching/Binary Search/arrayio.cpp: -------------------------------------------------------------------------------- 1 | #include "arrayio.h" 2 | #include 3 | using namespace std; 4 | 5 | void get_input(int *arr, int n) { 6 | for (int i = 0; i < n; i++) { 7 | cin >> arr[i]; 8 | } 9 | } 10 | 11 | void display(int *arr, int n) { 12 | for (int i = 0; i < n; i++) { 13 | cout << arr[i] << " "; 14 | } 15 | cout << endl; 16 | } -------------------------------------------------------------------------------- /Searching/Binary Search/arrayio.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef _AARAYIO_H 3 | #define _ARRAYIO_H 4 | 5 | void get_input(int *arr, int n); 6 | 7 | void display(int *arr, int n); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /Searching/Fibonacci Search/README.md: -------------------------------------------------------------------------------- 1 | # Fibonacci Search 2 | 3 | Fibonacci Search is a comparison-based technique that uses Fibonacci numbers to search an element in a sorted array. 4 | 5 | Similarities with Binary Search: 6 | 7 | Works for sorted arrays 8 | A Divide and Conquer Algorithm. 9 | Has Log n time complexity. 10 | 11 | ## Differences with Binary Search: 12 | 13 | 1.Fibonacci Search divides given array in unequal parts 14 | 15 | 2.Binary Search uses division operator to divide range. Fibonacci Search doesn’t use /, but uses + and -. The division operator may be costly on some CPUs. 16 | 17 | 3.Fibonacci Search examines relatively closer elements in subsequent steps. So when input array is big that cannot fit in CPU cache or even in RAM, Fibonacci Search can be useful. 18 | 19 | 20 | ## ALGORITHM: 21 | 22 | Let the searched element be x. 23 | 24 | The idea is to first find the smallest Fibonacci number that is greater than or equal to the length of given array. Let the found Fibonacci number be fib (m’th Fibonacci number). We use (m-2)’th Fibonacci number as the index (If it is a valid index). Let (m-2)’th Fibonacci Number be i, we compare arr[i] with x, if x is same, we return i. Else if x is greater, we recur for subarray after i, else we recur for subarray before i. 25 | 26 | Below is the complete algorithm: 27 | 28 | Let arr[0..n-1] be the input array and element to be searched be x. 29 | 30 | Find the smallest Fibonacci Number greater than or equal to n. Let this number be fibM [m’th Fibonacci Number]. Let the two Fibonacci numbers preceding it be fibMm1 [(m-1)’th Fibonacci Number] and fibMm2 [(m-2)’th Fibonacci Number]. 31 | While the array has elements to be inspected: 32 | Compare x with the last element of the range covered by fibMm2 33 | If x matches, return index 34 | Else If x is less than the element, move the three Fibonacci variables two Fibonacci down, indicating elimination of approximately rear two-third of the remaining array. 35 | Else x is greater than the element, move the three Fibonacci variables one Fibonacci down. Reset offset to index. Together these indicate elimination of approximately front one-third of the remaining array. 36 | Since there might be a single element remaining for comparison, check if fibMm1 is 1. If Yes, compare x with that remaining element. If match, return index. 37 | -------------------------------------------------------------------------------- /Searching/Fibonacci Search/fibonacciSearch.cpp: -------------------------------------------------------------------------------- 1 | // C program for Fibonacci Search 2 | #include 3 | 4 | // Utility function to find minimum of two elements 5 | int min(int x, int y) { return (x<=y)? x : y; } 6 | 7 | /* Returns index of x if present, else returns -1 */ 8 | int fibonacciSearch(int arr[], int x, int n) 9 | { 10 | /* Initialize fibonacci numbers */ 11 | int fibMMm2 = 0; // (m-2)'th Fibonacci No. 12 | int fibMMm1 = 1; // (m-1)'th Fibonacci No. 13 | int fibM = fibMMm2 + fibMMm1; // m'th Fibonacci 14 | 15 | /* fibM is going to store the smallest Fibonacci 16 | Number greater than or equal to n */ 17 | while (fibM < n) 18 | { 19 | fibMMm2 = fibMMm1; 20 | fibMMm1 = fibM; 21 | fibM = fibMMm2 + fibMMm1; 22 | } 23 | 24 | // Marks the eliminated range from front 25 | int offset = -1; 26 | 27 | /* while there are elements to be inspected. Note that 28 | we compare arr[fibMm2] with x. When fibM becomes 1, 29 | fibMm2 becomes 0 */ 30 | while (fibM > 1) 31 | { 32 | // Check if fibMm2 is a valid location 33 | int i = min(offset+fibMMm2, n-1); 34 | 35 | /* If x is greater than the value at index fibMm2, 36 | cut the subarray array from offset to i */ 37 | if (arr[i] < x) 38 | { 39 | fibM = fibMMm1; 40 | fibMMm1 = fibMMm2; 41 | fibMMm2 = fibM - fibMMm1; 42 | offset = i; 43 | } 44 | 45 | /* If x is greater than the value at index fibMm2, 46 | cut the subarray after i+1 */ 47 | else if (arr[i] > x) 48 | { 49 | fibM = fibMMm2; 50 | fibMMm1 = fibMMm1 - fibMMm2; 51 | fibMMm2 = fibM - fibMMm1; 52 | } 53 | 54 | /* element found. return index */ 55 | else return i; 56 | } 57 | 58 | /* comparing the last element with x */ 59 | if(fibMMm1 && arr[offset+1]==x)return offset+1; 60 | 61 | /*element not found. return -1 */ 62 | return -1; 63 | } 64 | 65 | /* driver function */ 66 | int main(void) 67 | { 68 | int arr[] = {10, 22, 35, 40, 45, 50, 80, 82, 69 | 85, 90, 100}; 70 | int n = sizeof(arr)/sizeof(arr[0]); 71 | int x = 85; 72 | printf("Found at index: %d", 73 | fibonacciSearch(arr, x, n)); 74 | return 0; 75 | } 76 | Upda 77 | -------------------------------------------------------------------------------- /Searching/Linear Search/README.md: -------------------------------------------------------------------------------- 1 | # **Linear Search** 2 | 3 | Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection. This algorithm works on Linear Data structures such as Arrays. 4 | 5 | 6 | ## _EXAMPLE for linear search_ 7 | 8 | For example: We are searching for number **_33_** in the given array. We will begin searching number **_33_** from starting of the given array till end. And As soon as we find first number **_33_** in array we stop searching further in the array. 9 | 10 | ![Gif](https://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif) 11 | 12 | 13 | # **Simple algorithm for linear search** 14 | 15 | Linear Search ( Array A, Value x) 16 | 17 | **Step 1**: Set i to 1 18 | 19 | **Step 2**: if i > n then go to step 7 20 | 21 | **Step 3**: if A[i] = x then go to step 6 22 | 23 | **Step 4**: Set i to i + 1 24 | 25 | **Step 5**: Go to Step 2 26 | 27 | **Step 6**: Print Element x Found at index i and go to step 8 28 | 29 | **Step 7**: Print element not found 30 | 31 | **Step 8**: Exit 32 | 33 | 34 | 35 | # **Pseudo code for Linear search** 36 | 37 | procedure linear_search (list, value) 38 | for each item in the list 39 | if match item == value 40 | return the item's location 41 | end if 42 | end for 43 | end procedure 44 | -------------------------------------------------------------------------------- /Searching/Linear Search/linear_search_array_approach.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int search(int arr[], int n, int x) 5 | { 6 | int i; 7 | for (i = 0; i < n; i++){ 8 | if (arr[i] == x){ 9 | return i; 10 | } 11 | } 12 | return -1; 13 | } 14 | 15 | int main(void) 16 | { 17 | int n; //number of elements in array 18 | cin >> n; 19 | int arr[n]; 20 | //storing the elements in array 21 | for(int i = 0 ; i> arr[i]; 23 | } 24 | int x; //element to be searched 25 | cin >> x; 26 | int result = search(arr, n, x); 27 | (result == -1)? cout << "Element is not present in array" 28 | : cout << "Element is present at index " << result; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Searching/Linear Search/linear_search_vector_approach.cpp: -------------------------------------------------------------------------------- 1 | // #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int search(vector arr, int n, int x) 7 | { 8 | int i; 9 | for (i = 0; i < n; i++) 10 | if (arr[i] == x) 11 | return i; 12 | return -1; 13 | } 14 | 15 | // Driver code 16 | int main() 17 | { 18 | vector arr; 19 | int n; 20 | int x; 21 | cout << "Enter the number of elements in input : "; 22 | cin >> n; 23 | for(int i = 0; i < n ; i++) 24 | { 25 | cout << "Enter the input value of element "<> p; 28 | arr.push_back(p); 29 | } 30 | cout << "Enter the value to be searched: "; 31 | cin >> x; 32 | int index = search(arr, n, x); 33 | if (index == -1) 34 | cout << "Element is not present in the array"; 35 | else 36 | cout << "Element found at position " << index; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Searching/README.md: -------------------------------------------------------------------------------- 1 | # Searching 2 | 3 | - Aditya Mandke 4 | -------------------------------------------------------------------------------- /Searching/test: -------------------------------------------------------------------------------- 1 | test 2 | -------------------------------------------------------------------------------- /Sorting/Bubble Sort/Bubble_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | using namespace std::chrono; 5 | 6 | /* 7 | Bubblesort is a popular sorting algorithm. It works by repeatedly swapping adjacent elements 8 | that are out of order. 9 | 10 | BUBBLESORT(A) 11 | 1 for i ← 1 to length[A] 12 | 2 do for j ← length[A] downto i + 1 13 | 3 do if A[j] < A[j - 1] 14 | 4 then exchange A[j] ↔ A[j - 1] 15 | 16 | Worst case time complexity: O(n^2) when the list is not pre-sorted 17 | Average case time complexity: O(n^2) 18 | Best case time complexity: O(n) list is pre sorted 19 | 20 | Space complexity: O(1) 21 | 22 | */ 23 | 24 | 25 | void bubble_sort(int *arr, int n) { 26 | int i, j; 27 | for (i = 0; i < n - 1; i++) { 28 | for (j = 0; j < n - i - 1 ; j++) { 29 | if (arr[j] > arr[j + 1]) { 30 | int temp = arr[j]; 31 | arr[j] = arr[j+1]; 32 | arr[j+1] = temp; 33 | } 34 | } 35 | } 36 | } 37 | 38 | //If the array gets sorted in any iteration before the n^2 th iteration, the sorting process is halted 39 | void optimized_bubble_sort(int *arr, int n) { 40 | int i, j; 41 | for (i = 0; i < n; i++) { 42 | //initialize flag to monitor swapping process 43 | bool flag = 0; 44 | for (j = 0; j > n - i - 1; j++) { 45 | if (arr[j] > arr[j + 1]) { 46 | // set flag to 1 if the swapping condition is true anywhere in the inner for loop 47 | flag = 1; 48 | int temp = arr[j]; 49 | arr[j] = arr[j + 1]; 50 | arr[j + 1] = temp; 51 | } 52 | } 53 | // if the flag is still not set that means the array is sorted and thus there is no need to continue the sorting process 54 | if (!flag) { 55 | break; 56 | } 57 | } 58 | } 59 | 60 | 61 | void get_input(int *arr, int n) { 62 | for (int i = 0; i < n; i++) { 63 | cin >> arr[i]; 64 | } 65 | } 66 | 67 | void display(int *arr, int n) { 68 | for (int i = 0; i < n; i++) { 69 | cout << arr[i] << " "; 70 | } 71 | } 72 | 73 | int main() 74 | { 75 | int *arr, n; 76 | cout << "Number of elements: "; 77 | cin >> n; 78 | arr = new int[n]; 79 | 80 | //get the elements of the array 81 | cout << "Elements of array: " << endl; 82 | get_input(arr, n); 83 | 84 | //perform bubble sort 85 | auto start = high_resolution_clock::now(); 86 | bubble_sort(arr, n); 87 | auto stop = high_resolution_clock::now(); 88 | auto duration = duration_cast(stop - start); 89 | cout << "Running time: " << duration.count() << " microseconds." <(stop2 - start2); 101 | cout << "\nRunning time for already sorted list and normal bubble sort: " << duration2.count() << " microseconds" << endl; 102 | 103 | auto start3 = high_resolution_clock::now(); 104 | optimized_bubble_sort(arr, n); 105 | auto stop3 = high_resolution_clock::now(); 106 | auto duration3 = duration_cast(stop3 - start3); 107 | cout << "\nRunning time for already sorted list and optimized bubble sort: " << duration3.count() << " microseconds" << endl; 108 | 109 | cout << "Enter values again, first number of elements: " << endl; 110 | cin >> n; 111 | cout << "Enter values: " << endl; 112 | get_input(arr, n); 113 | 114 | auto start4 = high_resolution_clock::now(); 115 | optimized_bubble_sort(arr, n); 116 | auto stop4 = high_resolution_clock::now(); 117 | auto duration4 = duration_cast(stop4 - start4); 118 | cout << "\nRunning time for optimized bubble sort: " << duration4.count() << " microseconds" << endl; 119 | 120 | cout << "Press any key to exit. "; 121 | cin.get(); 122 | cin.ignore(); 123 | 124 | } 125 | 126 | -------------------------------------------------------------------------------- /Sorting/Bubble Sort/README.md: -------------------------------------------------------------------------------- 1 | # Bubble Sort 2 | 3 | Bubble Sort is a sorting algorithm which continuously swaps adjacent elements of a list till a completely sorted list is obtained. 4 | 5 | ## Pseudo code: 6 | BUBBLESORT(A) 7 | 8 | for i ← 1 to length[A] 9 | 10 | do for j ← length[A] downto i + 1 11 | 12 | do if A[j] < A[j - 1] 13 | 14 | then exchange A[j] ↔ A[j - 1] 15 | 16 | ## Time complexity and space complexity 17 | Worst case time complexity: O(n^2) 18 | Average case time complexity: O(n^2) 19 | Best case time complexity: O(n) 20 | 21 | Worst case and average case time complexity is O(n^2) as a result of the two for loops and the list is unsorted. 22 | Best case time complexity occcurs when the list is already sorted. 23 | 24 | The space complexity is O(1) 25 | 26 | ## Optimized bubble sort 27 | If in an iteration no swapping occurs, this means that the list has been sorted, and thus the program stops. 28 | 29 | ## Comparison of time for the different bubble sorts 30 | 31 | A) Number of elements: 1000 (List is unsorted) 32 | 33 | Bubble Sort: 2737100 ns 34 | 35 | Optimized Bubble Sort: 800 ns 36 | 37 | 38 | B) Number of elements: 1000 (List is sorted) 39 | 40 | Bubble Sort: 1207300 ns 41 | 42 | Optimized Bubble Sort: 400 ns 43 | 44 | 45 | C) Number of elements: 100 (List is unsorted) 46 | 47 | Bubble Sort: 38300 ns 48 | 49 | Optimized Bubble Sort: 1000 ns 50 | 51 | 52 | D) Number of elements: 100 (List is sorted) 53 | 54 | Bubble Sort: 15700 ns 55 | 56 | Optimized Bubble Sort: 400 ns 57 | 58 | ## Conclusion: 59 | It can be seen that optimized bubble sort is much more time efficient in comparison with normal bubble sort. 60 | 61 | - Aditya Mandke 62 | -------------------------------------------------------------------------------- /Sorting/Bucket Sort/Bucket_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | void display(float *array, int size) { 6 | for(int i = 0; i bucket[size]; 12 | for(int i = 0; i> n; 30 | float arr[n]; //create an array with given number of elements 31 | cout << "Enter elements:" << endl; 32 | for(int i = 0; i> arr[i]; 34 | } 35 | cout << "Array before Sorting: "; 36 | display(arr, n); 37 | bucketSort(arr, n); 38 | cout << "Array after Sorting: "; 39 | display(arr, n); 40 | } -------------------------------------------------------------------------------- /Sorting/Bucket Sort/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ekdnam/Algorithms/22b9c893e154e322573597efb2c8ef0181abdae2/Sorting/Bucket Sort/README.md -------------------------------------------------------------------------------- /Sorting/Insertion Sort/Insertion_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | using namespace std::chrono; 5 | 6 | /* 7 | 8 | input: a sequence of n numbers - a_0, a_1, ... a_(n-1) 9 | output: a reordering of numbers where they are arranged in ascending order 10 | 11 | Pseudo code: 12 | 1 for j ← 2 to length[A] 13 | 2 do key ← A[j] 14 | 3 Insert A[j] into the sorted sequence A[1  j - 1]. 15 | 4 i ← j - 1 16 | 5 while i > 0 and A[i] > key 17 | 6 do A[i + 1] ← A[i] 18 | 7 i ← i - 1 19 | 8 A[i + 1] ← key 20 | 21 | */ 22 | 23 | /* 24 | 1> Worst case: O(n^2) time complexity and swaps 25 | 2> Best case: O(n) time complexity and O(1) swaps 26 | 3> Average case: O(n^2) time complexity and swaps 27 | 28 | Space complexity: Worst case - O(n) 29 | */ 30 | 31 | void insertion_sort(int *arr, int n) { 32 | int iter = 2, key = 0, i; 33 | for (iter = 1; iter < n; iter++) { 34 | key = arr[iter]; 35 | i = iter - 1; 36 | while (i > -1 && arr[i] > key) { 37 | arr[i + 1] = arr[i]; 38 | i = i - 1; 39 | } 40 | arr[i + 1] = key; 41 | } 42 | } 43 | 44 | void input(int *arr, int n) { 45 | cout << "Elements: "; 46 | for (int i = 0; i < n; i++) { 47 | cin >> arr[i]; 48 | } 49 | } 50 | 51 | void display(int *arr, int n) { 52 | for (int i = 0; i < n; i++) { 53 | cout << arr[i] << " "; 54 | } 55 | } 56 | 57 | int main() 58 | { 59 | int n, *arr; 60 | cout << "Number of elements: "; 61 | cin >> n; 62 | arr = new int[n]; 63 | 64 | input(arr, n); 65 | 66 | auto start = high_resolution_clock::now(); 67 | insertion_sort(arr, n); 68 | auto stop = high_resolution_clock::now(); 69 | auto duration1 = duration_cast(stop - start); 70 | cout << "Running time: " << duration1.count() << " microseconds" << endl; 71 | 72 | 73 | display(arr, n); 74 | 75 | start = high_resolution_clock::now(); 76 | insertion_sort(arr, n); 77 | stop = high_resolution_clock::now(); 78 | auto duration2 = duration_cast(stop - start); 79 | cout << "\n\nRunning time: " << duration2.count() << " microseconds" << endl; 80 | cout << "Press any key to exit. "; 81 | cin.get(); 82 | cin.ignore(); 83 | } 84 | -------------------------------------------------------------------------------- /Sorting/Insertion Sort/README.md: -------------------------------------------------------------------------------- 1 | ## Insertion Sort 2 | Insertion sort works the way many people sort a hand of playing cards. We start 3 | with an empty left hand and the cards face down on the table. We then remove one card at a 4 | time from the table and insert it into the correct position in the left hand. To find the correct 5 | position for a card, we compare it with each of the cards already in the hand, from right to 6 | left. At all times, the cards held in the left hand are sorted, and 7 | these cards were originally the top cards of the pile on the table. 8 | 9 | ## Pseudocode 10 | INSERTION-SORT(A) 11 | 12 | for j ← 2 to length[A] 13 | 14 | do key ← A[j] 15 | 16 | ▹ Insert A[j] into the sorted sequence A[1  j - 1]. 17 | 18 | i ← j - 1 19 | 20 | while i > 0 and A[i] > key 21 | 22 | do A[i + 1] ← A[i] 23 | 24 | i ← i - 1 25 | 26 | A[i + 1] ← key 27 | 28 | ## Time and space compexity: 29 | Worst case time complexity: O(n^2) 30 | 31 | Average case time complexity: O(n^2) 32 | 33 | Best case time complexity: O(n) 34 | 35 | Worst case and average case time complexity is because of the two for loops and occurs when list is unsorted. 36 | 37 | Best case time complexity occurs when the list is sorted. 38 | 39 | 40 | 41 | ## Time : 42 | Number of elements: 1000 43 | 44 | A) Unsorted list: 993500 ns 45 | 46 | B) Sorted list: 4500 ns 47 | 48 | ## References 49 | Geeks for Geeks 50 | 51 | CLRS 52 | 53 | - Aditya Mandke 54 | -------------------------------------------------------------------------------- /Sorting/Merge Sort/Merge_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace std::chrono; 6 | # define MAX 100000 7 | void get_array(int *arr, int n) { 8 | cout << "Elements: " << endl; 9 | for (int i = 0; i < n; i++) { 10 | cin >> arr[i]; 11 | } 12 | } 13 | 14 | void merge_array(int *arr, int p, int q, int r) { 15 | int n1 = q - p + 1; 16 | int n2 = r - q; 17 | int *L = new int[n1], *R = new int[n2]; 18 | int i = 0, j = 0; 19 | while (i < n1) { 20 | L[i] = arr[p + i]; 21 | i++; 22 | } 23 | while (j < n2) { 24 | R[j] = arr[q + j + 1]; 25 | j++; 26 | } 27 | //L[n1] = MAX; 28 | //R[n2] = MAX; 29 | i = 0; j = 0; 30 | int k = p; 31 | while (i < n1 && j < n2) { 32 | if (L[i] <= R[j]) { 33 | arr[k] = L[i]; 34 | i++; 35 | } 36 | else { 37 | arr[k] = R[j]; 38 | j++; 39 | } 40 | k++; 41 | } 42 | 43 | while (i < n1) { 44 | arr[k] = L[i]; 45 | i++; k++; 46 | } 47 | while (j < n2) { 48 | arr[k] = R[j]; 49 | j++; k++; 50 | } 51 | delete[]L; 52 | delete[]R; 53 | } 54 | 55 | 56 | 57 | void merge_sort(int *arr, int l, int r) { 58 | if (l < r) { 59 | int m = (l+r)/ 2; 60 | 61 | merge_sort(arr, l, m); 62 | 63 | merge_sort(arr, m + 1, r); 64 | 65 | merge_array(arr, l, m, r); 66 | } 67 | } 68 | 69 | 70 | void display_array(int *arr, int n) { 71 | for (int i = 0; i < n; i++) { 72 | cout << arr[i] << " "; 73 | } 74 | } 75 | 76 | int main() 77 | { 78 | int n, *arr; 79 | cout << "Number of elements: "; 80 | cin >> n; 81 | arr = new int[n]; 82 | 83 | get_array(arr, n); 84 | 85 | auto start = high_resolution_clock::now(); 86 | merge_sort(arr, 0, n - 1); 87 | auto stop = high_resolution_clock::now(); 88 | auto duration = duration_cast(stop - start); 89 | cout << "\nRunning time: " << duration.count() << " microseconds" << endl; 90 | 91 | display_array(arr, n); 92 | 93 | delete[]arr; 94 | 95 | cout << "Press any key to exit. "; 96 | cin.get(); 97 | cin.ignore(); 98 | } 99 | 100 | -------------------------------------------------------------------------------- /Sorting/Merge Sort/README.md: -------------------------------------------------------------------------------- 1 | # Merge Sort 2 | 3 | - Aditya Mandke 4 | -------------------------------------------------------------------------------- /Sorting/Quicksort/Quicksort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "arrayio.h" 3 | #include 4 | using namespace std; 5 | using namespace std::chrono; 6 | void swap(int *xp, int *yp) { 7 | int temp = *xp; 8 | *xp = *yp; 9 | *yp = temp; 10 | } 11 | 12 | int partition(int *arr, int p, int r) { 13 | int x = arr[r]; 14 | int i = p - 1; 15 | int j = p; 16 | for (int j = p; j <= r - 1; j++) { 17 | if (arr[j] <= x) { 18 | i += 1; 19 | swap(&arr[i], &arr[j]); 20 | } 21 | } 22 | swap(&arr[i + 1], &arr[r]); 23 | return i + 1; 24 | } 25 | 26 | void quicksort(int *arr, int p, int r) { 27 | if (p < r) { 28 | int q = partition(arr, p, r); 29 | quicksort(arr, p, q - 1); 30 | quicksort(arr, q + 1, r); 31 | } 32 | } 33 | 34 | int main() 35 | { 36 | int *arr, n; 37 | cout << "Elements: "; 38 | cin >> n; 39 | arr = new int[n]; 40 | cout << "Enter elements: "; 41 | get_input(arr, n); 42 | 43 | auto start = high_resolution_clock::now(); 44 | quicksort(arr, 0, n - 1); 45 | auto stop = high_resolution_clock::now(); 46 | auto duration = duration_cast(stop - start); 47 | cout << "Running time of algorithm is: " << duration.count() << " microseconds. " << endl; 48 | 49 | cout << "Sorted array is: " << endl; 50 | display(arr, n); 51 | delete[]arr; 52 | 53 | cout << "Press any key to exit "; 54 | cin.get(); 55 | cin.ignore(); 56 | } 57 | -------------------------------------------------------------------------------- /Sorting/Quicksort/README.md: -------------------------------------------------------------------------------- 1 | # Quicksort 2 | -------------------------------------------------------------------------------- /Sorting/Quicksort/arrayio.cpp: -------------------------------------------------------------------------------- 1 | #include "arrayio.h" 2 | #include 3 | using namespace std; 4 | 5 | void get_input(int *arr, int n) { 6 | for (int i = 0; i < n; i++) { 7 | cin >> arr[i]; 8 | } 9 | } 10 | 11 | void display(int *arr, int n) { 12 | for (int i = 0; i < n; i++) { 13 | cout << arr[i] << " "; 14 | } 15 | cout << endl; 16 | } -------------------------------------------------------------------------------- /Sorting/Quicksort/arrayio.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef _ARRAYIO_H 3 | #define _ARRAYIO_H 4 | 5 | void get_input(int *arr, int n); 6 | 7 | void display(int *arr, int n); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /Sorting/README.md: -------------------------------------------------------------------------------- 1 | # Sorting 2 | 3 | - Aditya Mandke 4 | -------------------------------------------------------------------------------- /Sorting/Selection Sort/README.md: -------------------------------------------------------------------------------- 1 | # Selection Sort 2 | 3 | - Aditya Mandke 4 | -------------------------------------------------------------------------------- /Sorting/Selection Sort/Selection_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace std::chrono; 6 | 7 | /* 8 | The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) 9 | from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. 10 | 11 | 1) The subarray which is already sorted. 12 | 2) Remaining subarray which is unsorted. 13 | 14 | In every iteration of selection sort, the minimum element (considering ascending order) 15 | from the unsorted subarray is picked and moved to the sorted subarray. 16 | */ 17 | 18 | /* 19 | Begin to iterate through the array from index 0 to n - 1, iterator = i 20 | find the smallest element in this range, let the index of element be min_ind 21 | swap elements at indices i, and min_ind 22 | continue 23 | */ 24 | 25 | void selection_sort(int *arr, int n) { 26 | int i, j, min_ind = 0, ind = 0; 27 | for (i = 0; i < n; i++) { 28 | min_ind = arr[i]; 29 | ind = i; 30 | for (j = i + 1; j < n; j++) { 31 | if (min_ind > arr[j]) { 32 | min_ind = arr[j]; 33 | ind = j; 34 | } 35 | } 36 | int temp = arr[i]; 37 | arr[i] = min_ind; 38 | arr[ind] = temp; 39 | } 40 | } 41 | 42 | void input(int *arr, int n) { 43 | cout << "Elements: "; 44 | for (int i = 0; i < n; i++) { 45 | cin >> arr[i]; 46 | } 47 | } 48 | 49 | void display(int *arr, int n) { 50 | for (int i = 0; i < n; i++) { 51 | cout << arr[i] << " "; 52 | } 53 | } 54 | 55 | int main() 56 | { 57 | int *arr, n; 58 | cout << "Number of elements: " << endl; 59 | cin >> n; 60 | 61 | arr = new int[n]; 62 | 63 | input(arr, n); 64 | 65 | auto start = high_resolution_clock::now(); 66 | selection_sort(arr, n); 67 | auto stop = high_resolution_clock::now(); 68 | auto duration = duration_cast(stop - start); 69 | cout <<"\nRunning time of selection sort is: " << duration.count() <<" microseconds" << endl; 70 | 71 | cout << "\nSorted array is: " << endl; 72 | display(arr, n); 73 | 74 | cout << "Press any key to exit. "; 75 | cin.get(); 76 | cin.ignore(); 77 | } -------------------------------------------------------------------------------- /Sorting/input.txt: -------------------------------------------------------------------------------- 1 | 484 2 | 1954 3 | -8795 4 | -616 5 | -8600 6 | 6965 7 | -954 8 | -3773 9 | -5280 10 | -8423 11 | -9838 12 | -4129 13 | -4125 14 | 4739 15 | 8357 16 | 5719 17 | -7151 18 | 5250 19 | 5013 20 | 2750 21 | 1372 22 | 7191 23 | 2107 24 | -6511 25 | -9322 26 | 9367 27 | -6907 28 | 4540 29 | -8215 30 | 8393 31 | 7185 32 | 9971 33 | -6981 34 | 9552 35 | 9629 36 | -771 37 | -5774 38 | -9516 39 | -7990 40 | -2741 41 | -8758 42 | -620 43 | 7101 44 | -9416 45 | -277 46 | 6279 47 | 3000 48 | -9509 49 | 9236 50 | -5478 51 | -248 52 | 681 53 | -2030 54 | -1294 55 | -1702 56 | 3783 57 | 6889 58 | 3758 59 | -1244 60 | -8180 61 | 1567 62 | -8548 63 | 4477 64 | 6020 65 | -5453 66 | -3836 67 | 3955 68 | 6234 69 | -4202 70 | 2595 71 | 3725 72 | -2225 73 | -9633 74 | -3024 75 | 4429 76 | -9698 77 | 8500 78 | 6362 79 | -4959 80 | 4329 81 | 7784 82 | -3258 83 | -254 84 | -7960 85 | 5590 86 | -287 87 | 5657 88 | -4276 89 | -5645 90 | 6197 91 | -2250 92 | 3915 93 | -2144 94 | 3560 95 | 8291 96 | 4117 97 | -6951 98 | 7336 99 | -5528 100 | -1698 101 | -1929 102 | 6491 103 | -4850 104 | 932 105 | -2050 106 | 3941 107 | -6812 108 | -7567 109 | -6309 110 | 7642 111 | -2601 112 | -1991 113 | -424 114 | -178 115 | -2123 116 | 3163 117 | 9484 118 | 4056 119 | -775 120 | -6580 121 | 7301 122 | -8280 123 | 427 124 | -7575 125 | 6693 126 | 5992 127 | -5795 128 | -7243 129 | 7627 130 | 3144 131 | -2891 132 | 7798 133 | -5181 134 | -2699 135 | -8170 136 | 6526 137 | 7615 138 | -2233 139 | -1447 140 | 2457 141 | -1207 142 | 3563 143 | -289 144 | -7010 145 | 2758 146 | 2425 147 | 4247 148 | 5712 149 | 6680 150 | 4205 151 | -3030 152 | -5656 153 | -7554 154 | 6247 155 | -9075 156 | -9136 157 | 8233 158 | 5878 159 | -7822 160 | -8316 161 | -7673 162 | -675 163 | 5969 164 | 7251 165 | 7769 166 | -4331 167 | 4461 168 | -4733 169 | -2788 170 | 4538 171 | 7897 172 | 8098 173 | -7766 174 | 5725 175 | -6497 176 | 5815 177 | -6432 178 | -4225 179 | 3712 180 | -5799 181 | -1873 182 | -8140 183 | 4525 184 | -8491 185 | 8764 186 | -5126 187 | 4109 188 | -6491 189 | -5173 190 | 287 191 | 532 192 | -8797 193 | -1995 194 | 4589 195 | -3853 196 | 3401 197 | 5571 198 | -1038 199 | -1679 200 | -1117 201 | -5179 202 | 6329 203 | 7692 204 | -593 205 | 6341 206 | -8693 207 | 8984 208 | -8240 209 | 2755 210 | 2146 211 | 3673 212 | 4344 213 | 6685 214 | -1170 215 | 7808 216 | 422 217 | 4957 218 | -1735 219 | 3267 220 | -8257 221 | 4982 222 | 6454 223 | -8995 224 | -4774 225 | 7448 226 | 3942 227 | 4182 228 | -9470 229 | 8571 230 | 2695 231 | 4534 232 | 4648 233 | -7501 234 | 7259 235 | -2345 236 | -5105 237 | -508 238 | -5586 239 | 1725 240 | -8139 241 | 7817 242 | 6715 243 | 7227 244 | -1579 245 | 1113 246 | 500 247 | 1828 248 | 9566 249 | 7866 250 | -2757 251 | 2768 252 | 5971 253 | 5741 254 | 5766 255 | 5401 256 | 4504 257 | 8458 258 | -3598 259 | -40 260 | -9047 261 | -6251 262 | -809 263 | -9035 264 | -675 265 | 4654 266 | 4321 267 | -5898 268 | 3045 269 | -2411 270 | -7385 271 | 5897 272 | -8167 273 | -9797 274 | -6143 275 | -3505 276 | -9342 277 | -6609 278 | -7222 279 | -2050 280 | -4078 281 | 3196 282 | 293 283 | 1752 284 | -2334 285 | 8416 286 | -278 287 | 5852 288 | 244 289 | 4463 290 | -2158 291 | -6797 292 | 8765 293 | -7973 294 | 1972 295 | 3714 296 | 8512 297 | 2115 298 | 7243 299 | 3434 300 | -8383 301 | -9403 302 | -8792 303 | 4940 304 | -9779 305 | -8291 306 | -6429 307 | 493 308 | -1689 309 | 2494 310 | 199 311 | 1565 312 | -6159 313 | -7707 314 | -6617 315 | 8131 316 | 1777 317 | 9368 318 | -6731 319 | 8934 320 | 8849 321 | -4621 322 | -6342 323 | 1196 324 | -1425 325 | -3115 326 | 3755 327 | 1338 328 | -6001 329 | -8058 330 | -6778 331 | 350 332 | -4010 333 | -1935 334 | 8697 335 | -7905 336 | -5710 337 | -3512 338 | 3048 339 | 7873 340 | -9782 341 | 6421 342 | -2424 343 | 6112 344 | -7678 345 | 2772 346 | -5049 347 | 5013 348 | -6049 349 | -3651 350 | 7096 351 | 7218 352 | -4684 353 | -1706 354 | 7249 355 | -9350 356 | 7549 357 | -9350 358 | -9656 359 | -4238 360 | 9184 361 | 1616 362 | 3366 363 | 5069 364 | 7311 365 | -7407 366 | -3861 367 | 9960 368 | -3781 369 | -8192 370 | 5402 371 | -2377 372 | -7981 373 | 4052 374 | -7393 375 | 437 376 | -6102 377 | -1458 378 | 2328 379 | 4873 380 | -5421 381 | -940 382 | 3766 383 | 5723 384 | -381 385 | 5894 386 | 2014 387 | -6855 388 | -2721 389 | 8042 390 | 5505 391 | 3845 392 | 9764 393 | -7040 394 | 8869 395 | -2761 396 | -7501 397 | 5413 398 | 7175 399 | 7985 400 | 4791 401 | 3376 402 | -5723 403 | -802 404 | 4028 405 | 741 406 | 9385 407 | 7992 408 | -5491 409 | -9435 410 | 8495 411 | 8941 412 | 8318 413 | 9437 414 | -3240 415 | -3195 416 | 4363 417 | 6038 418 | -5357 419 | -7278 420 | -3892 421 | -2744 422 | 725 423 | -1541 424 | -9349 425 | -5101 426 | -1816 427 | 4521 428 | 9709 429 | 9538 430 | -8131 431 | 6911 432 | -6878 433 | 3625 434 | -5603 435 | 5220 436 | -6733 437 | -5821 438 | -1877 439 | -1905 440 | 2427 441 | 5922 442 | -4479 443 | 7463 444 | -2740 445 | 2123 446 | 1258 447 | -3408 448 | 8450 449 | -1345 450 | -9273 451 | -1290 452 | 97 453 | 3846 454 | -3880 455 | -3894 456 | -8329 457 | 6836 458 | 139 459 | -2310 460 | 2674 461 | -157 462 | 590 463 | 5069 464 | -2593 465 | 3714 466 | 576 467 | -2805 468 | -9579 469 | 8173 470 | 4585 471 | 4325 472 | 7850 473 | -2997 474 | 7118 475 | 3400 476 | 8929 477 | -295 478 | -772 479 | -9765 480 | -3228 481 | -3953 482 | 5442 483 | 5481 484 | -2808 485 | 5867 486 | 372 487 | 1821 488 | 9705 489 | 4273 490 | -7611 491 | -8933 492 | 350 493 | -1575 494 | 2039 495 | -5126 496 | 1242 497 | -699 498 | -1354 499 | 2061 500 | -1695 501 | -2567 502 | -1028 503 | -231 504 | 80 505 | 1895 506 | -5019 507 | 5581 508 | -6313 509 | 4129 510 | 7028 511 | -41 512 | -9183 513 | -9343 514 | 9496 515 | 28 516 | -1401 517 | -9098 518 | -4615 519 | -1763 520 | -7475 521 | -5817 522 | -6261 523 | -6864 524 | 8538 525 | 8160 526 | -9789 527 | 2743 528 | -4791 529 | -4040 530 | -995 531 | 7067 532 | -2670 533 | 5932 534 | 9230 535 | 6809 536 | -4592 537 | -8738 538 | -390 539 | -4937 540 | 1583 541 | -6463 542 | -2964 543 | 6996 544 | -2895 545 | 8064 546 | 7729 547 | 8900 548 | 4319 549 | 3681 550 | -207 551 | -1167 552 | 291 553 | 4373 554 | 390 555 | -444 556 | -2067 557 | -389 558 | 3586 559 | -4979 560 | 457 561 | 3911 562 | 3943 563 | -5619 564 | -4943 565 | 2093 566 | 9009 567 | -7722 568 | -9739 569 | -7305 570 | 9541 571 | 4496 572 | -2686 573 | 6678 574 | -2972 575 | -6275 576 | -5090 577 | -3254 578 | -3778 579 | -329 580 | 4232 581 | 4099 582 | 9780 583 | 2275 584 | -6931 585 | -6738 586 | -9838 587 | 8621 588 | -6362 589 | -1848 590 | 6478 591 | -9825 592 | 8736 593 | -6906 594 | 9809 595 | 1534 596 | -8215 597 | -3070 598 | -4937 599 | -8882 600 | 8021 601 | 1389 602 | -1441 603 | 4597 604 | 6006 605 | 4544 606 | -2386 607 | -1789 608 | 6612 609 | -1044 610 | -3171 611 | -9194 612 | -4556 613 | -1356 614 | 8061 615 | -8914 616 | 4101 617 | -1732 618 | -4473 619 | 7423 620 | 2513 621 | -4882 622 | 4862 623 | -9648 624 | 308 625 | -5227 626 | 9134 627 | 3519 628 | -1365 629 | -7064 630 | -9459 631 | -8874 632 | 6858 633 | 2429 634 | -1350 635 | 5393 636 | -3118 637 | -3563 638 | -2429 639 | 7645 640 | -2333 641 | -3954 642 | -8572 643 | 928 644 | -743 645 | 9483 646 | 3569 647 | -5445 648 | 378 649 | 340 650 | 8181 651 | -6783 652 | 6439 653 | -8145 654 | 22 655 | 463 656 | 3841 657 | -3224 658 | 2310 659 | -4468 660 | 1443 661 | 5598 662 | 2409 663 | 2543 664 | 7371 665 | -297 666 | -442 667 | -7168 668 | -4633 669 | 8547 670 | -4904 671 | -5525 672 | -6529 673 | 8745 674 | 8106 675 | 9104 676 | -3953 677 | -6692 678 | -5298 679 | -1516 680 | -3351 681 | 4251 682 | 8660 683 | 4652 684 | 5144 685 | -1937 686 | 7564 687 | -1778 688 | 7015 689 | -5997 690 | -5731 691 | -1710 692 | -124 693 | 1574 694 | -2341 695 | 1579 696 | 6241 697 | 820 698 | 7262 699 | -4530 700 | -2951 701 | -3592 702 | -5871 703 | -7523 704 | 1652 705 | 2621 706 | -5413 707 | -6484 708 | -9472 709 | 6747 710 | -5171 711 | 4652 712 | -7130 713 | 4106 714 | 2236 715 | -7961 716 | 3389 717 | 5816 718 | -8561 719 | 233 720 | -7967 721 | -5211 722 | -2068 723 | 9278 724 | -9843 725 | 1340 726 | -1002 727 | 5880 728 | 9036 729 | -1135 730 | -3735 731 | 3444 732 | 356 733 | 1602 734 | -4620 735 | -7295 736 | 2864 737 | -4768 738 | 7768 739 | -4453 740 | -3825 741 | 6801 742 | -5089 743 | 5499 744 | 5267 745 | -3387 746 | 3652 747 | -3749 748 | 7221 749 | 2945 750 | 7149 751 | 3352 752 | -7385 753 | -9020 754 | 9968 755 | 7294 756 | 2619 757 | -3414 758 | 2676 759 | 4920 760 | -8730 761 | -9972 762 | 4307 763 | 1779 764 | 801 765 | 221 766 | 5303 767 | 3948 768 | 1471 769 | 6095 770 | 9622 771 | -5913 772 | -3057 773 | -5867 774 | 9675 775 | -3812 776 | 3865 777 | -1949 778 | 7272 779 | 5926 780 | -6756 781 | 7039 782 | -7132 783 | 9040 784 | -4203 785 | -1443 786 | 4342 787 | -6291 788 | 1029 789 | 2211 790 | -3350 791 | -6072 792 | -8649 793 | -1630 794 | -144 795 | -6666 796 | -5212 797 | 3512 798 | -4698 799 | -6944 800 | 1060 801 | 6667 802 | -9811 803 | 6847 804 | -9579 805 | -8841 806 | 1346 807 | -5804 808 | -2755 809 | -4747 810 | -258 811 | 1650 812 | 4214 813 | 2755 814 | -2864 815 | 6669 816 | 7957 817 | -3871 818 | 141 819 | -5078 820 | -1690 821 | 762 822 | -6422 823 | 1341 824 | -9582 825 | 1042 826 | -7685 827 | 2979 828 | -5681 829 | -7349 830 | 2346 831 | -1711 832 | -1279 833 | -5141 834 | 3621 835 | 3608 836 | -7686 837 | -3996 838 | 4982 839 | 903 840 | -2869 841 | 1484 842 | -8757 843 | 9451 844 | -4968 845 | 144 846 | 7568 847 | -5959 848 | 1263 849 | 9258 850 | 9078 851 | -1236 852 | 4523 853 | 5370 854 | 5690 855 | 223 856 | -7694 857 | 9804 858 | 4479 859 | 9167 860 | -1235 861 | 8340 862 | 2493 863 | 1918 864 | 8818 865 | -7076 866 | 7842 867 | -9130 868 | 7748 869 | -1267 870 | 8767 871 | -6029 872 | 111 873 | -8287 874 | 3082 875 | 7311 876 | 2894 877 | 4163 878 | -6883 879 | -2364 880 | -4307 881 | 4770 882 | -4014 883 | 3345 884 | 8471 885 | 442 886 | -4416 887 | 6855 888 | -5135 889 | -3046 890 | -4597 891 | -1370 892 | -1600 893 | 8368 894 | -7017 895 | 1514 896 | -651 897 | -6608 898 | 2224 899 | 8123 900 | -9333 901 | -5336 902 | -6949 903 | -5628 904 | -6481 905 | -1288 906 | 4738 907 | 8378 908 | 5787 909 | -9556 910 | -9976 911 | -2010 912 | 5877 913 | -8488 914 | 1914 915 | -2416 916 | -1547 917 | 457 918 | -2630 919 | 2010 920 | -7566 921 | 4044 922 | -8847 923 | 6422 924 | -6550 925 | 8619 926 | 2137 927 | -1054 928 | 3354 929 | -672 930 | -9492 931 | 955 932 | -6991 933 | 3206 934 | -309 935 | -9312 936 | 8700 937 | 4205 938 | -1690 939 | 9161 940 | -6073 941 | -9340 942 | 929 943 | 1869 944 | 2173 945 | 984 946 | 5042 947 | -9551 948 | 141 949 | -2228 950 | -229 951 | 6354 952 | 491 953 | 6112 954 | -6422 955 | -9458 956 | 3089 957 | 5769 958 | 1755 959 | 6876 960 | -6480 961 | -7583 962 | 151 963 | 286 964 | 6129 965 | -3554 966 | 8149 967 | -7212 968 | -4859 969 | 7405 970 | -8241 971 | -5289 972 | 1445 973 | 9312 974 | -429 975 | 7732 976 | 3543 977 | 6389 978 | -9310 979 | 2082 980 | 5752 981 | 5257 982 | 7171 983 | 2660 984 | -1470 985 | 4234 986 | -4899 987 | -9023 988 | 8467 989 | 3421 990 | 3091 991 | 4176 992 | -2894 993 | -504 994 | -4705 995 | 2653 996 | 6390 997 | 4221 998 | 1121 999 | 7649 1000 | -3900 -------------------------------------------------------------------------------- /Sorting/test: -------------------------------------------------------------------------------- 1 | test 2 | -------------------------------------------------------------------------------- /images/algorithms.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ekdnam/Algorithms/22b9c893e154e322573597efb2c8ef0181abdae2/images/algorithms.jpg --------------------------------------------------------------------------------