├── .github ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── Asymptotic notation ├── Basic Questions │ └── basicquestions.md ├── Medium-Hard Questions │ └── medium-hard-questions.md └── Notes │ └── notes.md ├── Binary Search ├── Basic Questions │ └── basicquestions.md ├── Medium-Hard Questions │ ├── 01. MinInRotatedSortedArray.cpp │ └── medium-hard-questions.md └── Notes │ ├── Binary Search Notes.pdf │ └── notes.md ├── Breadth-first search ├── Basic Questions │ ├── 01. Breadth-first-search.cpp │ └── basicquestions.md ├── Medium-Hard Questions │ └── medium-hard-questions.md └── Notes │ └── notes.md ├── Depth-First Search ├── DFS.cpp └── dfs.md ├── Fibonacci ├── Basic Questions │ ├── basicquestions.md │ └── fibonacci.hs ├── Medium-Hard Questions │ └── medium-hard-questions.md └── Notes │ └── notes.md ├── Graph representation ├── Basic Questions │ └── basicquestions.md ├── Medium-Hard Questions │ └── medium-hard-questions.md └── Notes │ └── notes.md ├── Header.png ├── Insertion sort ├── Basic Questions │ ├── 01. Insertion-sort.cpp │ └── basicquestions.md ├── Medium-Hard Questions │ └── medium-hard-questions.md └── Notes │ ├── Insertion sort notes.pdf │ └── notes.md ├── LICENSE.md ├── Merge sort ├── Basic Questions │ ├── 01. merge-two-sorted-arrays.cpp │ ├── 02. merge-sort-algorithm.cpp │ ├── 03. merge-two-sorted-arrays.py │ └── basicquestions.md ├── Medium-Hard Questions │ └── medium-hard-questions.md └── Notes │ ├── Merge sort algorithm notes.pdf │ └── notes.md ├── Quick sort ├── Basic Questions │ ├── Sorting an array using Quick Sort.cpp │ └── basicquestions.md ├── Medium-Hard Questions │ └── medium-hard-questions.md └── Notes │ └── notes.md ├── README.md ├── Recursive algorithms ├── Basic Questions │ └── basicquestions.md ├── Medium-Hard Questions │ └── medium-hard-questions.md └── Notes │ └── notes.md ├── Selection sort ├── Basic Questions │ ├── 01. Selection-sort.cpp │ └── basicquestions.md ├── Medium-Hard Questions │ └── medium-hard-questions.md └── Notes │ ├── Selection Sort Notes.pdf │ └── notes.md ├── Towers of Hanoi ├── Basic Questions │ ├── 01. towerofhanoi.py │ └── basicquestions.md ├── Medium-Hard Questions │ └── medium-hard-questions.md └── Notes │ ├── Tower of Hanoi Notes.pdf │ └── notes.md ├── contributing.md └── logo-light.png /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | # Issue Alert. 2 | 3 | Kindly adhere to the template! 4 | 5 | ## Prerequisites 6 | 7 | Before we work on your issue, can you help us a bit more but completing this checklist? 8 | 9 | - [ ] Can you reproduce the problem in safe mode? 10 | - [ ] Are you running the latest version? 11 | - [ ] Did you check the debugging guide? 12 | - [ ] Did you check the FAQs on Discuss? 13 | - [ ] Are you reporting to the correct repository? 14 | - [ ] Did you perform a cursory search? 15 | 16 | ## Description 17 | 18 | [Description of the bug or feature] 19 | 20 | ## Steps to Reproduce 21 | 22 | 1. [First Step] 23 | 2. [Second Step] 24 | 3. [You get it ;-) ...] 25 | 26 | **Expected behavior:** [What you expected to happen] 27 | 28 | **Actual behavior:** [What actually happened] 29 | 30 | ## Sugggested Fix 31 | 32 | [Insert a fix that you think might work. If you can work on it, fix the bug and generate a PR, again following our *thoughtful* template] 33 | 34 | ## Versions 35 | 36 | You can get this information from executing `npm version`. 37 | 38 | --- 39 | 40 | ### Thank you for your support! 41 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | # Pull Request. 2 | 3 | Kindly adhere to the template! 4 | 5 | Generate a Pull Request if the code fixes an **issue**, or if you feel that the change is **important** and the world would stop spinning without it. 6 | 7 | ## Description 8 | 9 | Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. 10 | List any dependencies that are required for this change. 11 | 12 | Fixes #(issue) 13 | 14 | ## Types of changes 15 | 16 | - [ ] Bug fix (non-breaking change which fixes an issue). 17 | - [ ] New feature (non-breaking change which adds functionality). 18 | - [ ] Breaking change (fix or feature that would cause existing functionality to change). 19 | - [ ] This change requires a documentation update. 20 | - [ ] The code follows the code style of this project. 21 | - [ ] Documentation has been updated accordingly. 22 | - [ ] Tests have been added to cover the changes. 23 | - [ ] All new and existing tests passed. 24 | 25 | ## How Has This Been Tested? 26 | 27 | Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration 28 | 29 | - [ ] Test A 30 | - [ ] Test B 31 | 32 | **Test Configuration**: 33 | 34 | - Firmware version: 35 | - Hardware: 36 | - Toolchain: 37 | - SDK: 38 | 39 | ## Checklist: 40 | 41 | - [ ] My code follows the style guidelines of this project 42 | - [ ] I have performed a self-review of my own code 43 | - [ ] I have commented on my code, particularly in hard-to-understand areas 44 | - [ ] I have made corresponding changes to the documentation 45 | - [ ] My changes generate no new warnings 46 | - [ ] I have added tests that prove my fix is effective or that my feature works 47 | - [ ] New and existing unit tests pass locally with my changes 48 | - [ ] Any dependent changes have been merged and published in downstream modules 49 | 50 | --- 51 | 52 | ### Thank you for your contribution! 53 | 54 | -------------------------------------------------------------------------------- /Asymptotic notation/Basic Questions/basicquestions.md: -------------------------------------------------------------------------------- 1 | ## basic questions 2 | 3 | ### Question 1 4 | 5 | #### Problem Statement 6 | 7 | Find the asymptotic complexity of sum in terms of n. 8 | 9 | ``` 10 | int sum(int n) { 11 | int result = 0; 12 | for (int i = 1; i <= n; i++) { 13 | result += i; 14 | } 15 | return result; 16 | } 17 | ``` 18 | 19 | ### Solution 20 | 21 | **Asymptotic Complexity** (Big O): O(n) 22 | 23 | The asymptotic complexity of the `sum` function in terms of \( n \) can be expressed using Big O notation. In this case, the function involves a loop that iterates from 1 to \( n \), performing a constant-time operation (addition) in each iteration. 24 | 25 | The number of iterations in the loop is directly proportional to \( n \), as the loop runs from 1 to \( n \) inclusive. Therefore, the time complexity of the `sum` function is \( O(n) \), indicating a linear time complexity with respect to \( n \). 26 | 27 | ### Question 2 28 | 29 | #### Problem Statement 30 | 31 | What is the asymptotic complexity of the printNumbers function in terms of n? 32 | 33 | ``` 34 | void printNumbers(int n) { 35 | for (int i = 0; i < n; i++) { 36 | for (int j = 0; j < n; j++) { 37 | cout << i << " " << j << endl; 38 | } 39 | } 40 | } 41 | ``` 42 | 43 | ### Solution 44 | 45 | **Asymptotic Complexity** (Big O): O(n^2) 46 | 47 | The `printNumbers` function contains a nested loop where both loops iterate from 0 to \( n - 1 \) inclusive. In Big O notation, we express this as \( O(n^2) \) because the number of iterations is proportional to \( n \times n \), resulting in a quadratic relationship between the input size (\( n \)) and the number of operations. Thus, the asymptotic complexity of the `printNumbers` function in terms of \( n \) is \( O(n^2) \). 48 | 49 | ### Question 3 50 | 51 | #### Problem Statement 52 | 53 | What is the asymptotic complexity of the factorial function in terms of n? 54 | 55 | ``` 56 | int factorial(int n) { 57 | if (n <= 1) 58 | return 1; 59 | else 60 | return n * factorial(n - 1); 61 | } 62 | ``` 63 | 64 | ### Solution 65 | 66 | **Asymptotic Complexity** (Big O): O(n) 67 | 68 | The `factorial` function calculates the factorial of \( n \) recursively by multiplying \( n \) with the factorial of \( n-1 \), until \( n \) reaches 1. The algorithm's time complexity can be analyzed by examining the number of recursive calls made. 69 | 70 | In each recursive call, the function performs a constant-time operation (the multiplication) and makes a recursive call with \( n-1 \). The number of recursive calls is directly proportional to \( n \) because it decreases by 1 in each recursion until it reaches 1. 71 | 72 | Therefore, the time complexity of the `factorial` function in terms of \( n \) is \( O(n) \) since the number of recursive calls and operations is linearly proportional to \( n \). 73 | -------------------------------------------------------------------------------- /Asymptotic notation/Medium-Hard Questions/medium-hard-questions.md: -------------------------------------------------------------------------------- 1 | ## medium or hard level questions 2 | 3 | ### Question 1 4 | 5 | #### Problem Statement 6 | 7 | What is the asymptotic complexity of the selectionSort function in terms of n? 8 | 9 | ``` 10 | void selectionSort(int arr[], int n) { 11 | for (int i = 0; i < n - 1; i++) { 12 | int minIndex = i; 13 | for (int j = i + 1; j < n; j++) { 14 | if (arr[j] < arr[minIndex]) 15 | minIndex = j; 16 | } 17 | swap(arr[i], arr[minIndex]); 18 | } 19 | } 20 | 21 | ``` 22 | 23 | ### Solution 24 | 25 | **Asymptotic Complexity** (Big O): O(n^2) 26 | 27 | The `selectionSort` function implements the selection sort algorithm, which sorts an array by repeatedly finding the minimum element from the unsorted part of the array and swapping it with the first unsorted element. 28 | 29 | Let's analyze the time complexity of the `selectionSort` function: 30 | 31 | - The outer loop runs \(n-1\) times (from 0 to \(n-2\)), as it iterates through the array. 32 | - The inner loop runs \(n - i - 1\) times for each iteration of the outer loop, where \(i\) is the current iteration of the outer loop. 33 | 34 | The number of operations can be approximated as the sum of the series: 35 | 36 | \[ 37 | (n-1) + (n-2) + (n-3) + \ldots + 1 = \frac{n \times (n-1)}{2} 38 | \] 39 | 40 | In Big O notation, we simplify this to \(O(n^2)\), indicating that the time complexity of the `selectionSort` function is quadratic with respect to \(n\). 41 | 42 | ### Question 2 43 | 44 | #### Problem Statement 45 | 46 | What is the asymptotic complexity of the fibonacci function in terms of n? 47 | 48 | ``` 49 | int fibonacci(int n) { 50 | if (n <= 1) 51 | return n; 52 | else 53 | return fibonacci(n - 1) + fibonacci(n - 2); 54 | } 55 | 56 | ``` 57 | 58 | ### Solution 59 | 60 | **Asymptotic Complexity** (Big O): O(2^n) 61 | 62 | The `fibonacci` function uses recursion to calculate Fibonacci numbers. It computes the Fibonacci number for \(n\) by recursively computing the Fibonacci numbers for \(n-1\) and \(n-2\) until it reaches the base cases where \(n\) is either 0 or 1. 63 | 64 | To analyze the time complexity of the `fibonacci` function, consider the number of recursive calls and operations performed: 65 | 66 | - In each recursive call, two more recursive calls are made (one for \(n-1\) and one for \(n-2\)). 67 | - The number of recursive calls forms a binary tree with a depth of \(n\). 68 | 69 | The number of nodes in this binary tree (i.e., the number of recursive calls) grows exponentially with \(n\). Specifically, it follows the Fibonacci sequence itself. This leads to an exponential time complexity. 70 | 71 | Therefore, the asymptotic complexity of the `fibonacci` function in terms of \(n\) is \(O(2^n)\), indicating exponential time complexity. 72 | 73 | ### Question 3 74 | 75 | #### Problem Statement 76 | 77 | What is the asymptotic complexity of the quickSort function in terms of n? 78 | 79 | ``` 80 | void quickSort(int arr[], int low, int high) { 81 | if (low < high) { 82 | int pi = partition(arr, low, high); 83 | 84 | quickSort(arr, low, pi - 1); 85 | quickSort(arr, pi + 1, high); 86 | } 87 | } 88 | 89 | ``` 90 | 91 | ### Solution 92 | 93 | **Asymptotic Complexity** (Big O): O(n log n) 94 | 95 | Sure, let's discuss the asymptotic complexity of the `quickSort` function in terms of \(n\), with reference to the provided code. 96 | 97 | The `quickSort` function implements the quicksort algorithm, a widely used comparison-based sorting algorithm. The algorithm follows a divide-and-conquer approach, where it selects a "pivot" element from the array and partitions the elements such that elements smaller than the pivot are on the left and elements greater than the pivot are on the right. 98 | 99 | The time complexity of quicksort primarily depends on how well the pivot is chosen and how well the array is divided during each recursion. The key operation is the partitioning step, which runs in \(O(n)\) time, where \(n\) is the size of the subarray being partitioned. 100 | 101 | In the best and average cases, the array is divided roughly in half at each recursion level, leading to (O(log n)) levels of recursion, and \(O(n)\) work at each level due to partitioning. Therefore, the average and best-case time complexity is (O(n log n)) 102 | -------------------------------------------------------------------------------- /Asymptotic notation/Notes/notes.md: -------------------------------------------------------------------------------- 1 | ## Notes 2 | 3 | # Asymptotic Notation Overview 4 | 5 | Asymptotic Notation is crucial for describing the running time of an algorithm, providing insights into algorithm efficiency. There are three main notations: 6 | 7 | - **Big O**: Represents the worst-case running time. 8 | - **Big Theta (Θ)**: Represents the average-case running time. 9 | - **Big Omega (Ω)**: Represents the best-case running time. 10 | 11 | ## Asymptotic Complexity Hierarchy 12 | 13 | The hierarchy of asymptotic complexities, listed from the most efficient to the least, is as follows: 14 | 15 | `O(1) < O(log n) < O(n) < O(n log n) < O(n^2) < O(n^k) < O(2^n) < O(n!)` 16 | 17 | ## Importance of Asymptotic Notation 18 | 19 | 1. **Efficiency Characterization**: Provides a simple characterization of an algorithm's efficiency. 20 | 2. **Algorithm Comparison**: Allows for easy comparison of the performance of various algorithms. 21 | 22 | ## Steps to Determine Asymptotic Notations 23 | 24 | ### Omega (Ω) Notation 25 | 26 | 1. Break the program into smaller segments. 27 | 2. Calculate the number of operations for each segment in terms of the input size, assuming the input that minimizes the program's execution time. 28 | 3. Simplify the total number of operations to obtain a function in terms of n, denoted as f(n). 29 | 4. Remove constants and choose the term with the least order or any other function that is always less than f(n) as g(n). Omega notation of f(n) is then represented as Ω(g(n)). 30 | 31 | ### Theta (Θ) Notation 32 | 33 | 1. Break the program into smaller segments. 34 | 2. Calculate the number of operations for each segment based on various inputs, ensuring a representative distribution of cases. 35 | 3. Obtain a function of n, denoted as g(n), by summing the calculated values and dividing by the total number of inputs. 36 | 4. In Θ notation, represent the function as Θ(g(n)). 37 | 38 | ### Big O (O) Notation 39 | 40 | 1. Break the program into smaller segments. 41 | 2. Calculate the number of operations for each segment in terms of the input size, assuming the input that maximizes the program's execution time (worst-case scenario). 42 | 3. Simplify the total number of operations to obtain a function in terms of n, denoted as f(n). 43 | 4. Remove constants and choose the term with the highest order, as it becomes significant for n approaching infinity. Represent the function as g(n). Big O notation of f(n) is then represented as O(g(n)). 44 | 45 | ## Example: Determining Time Complexity 46 | 47 | ### Problem Statement 48 | 49 | Given a positive integer n and a function f(n), determine the time complexity of f(n), expressed as a polynomial. 50 | 51 | ### Solution 52 | 53 | The function can be represented as f(n) = a0 + a1n + a2n^2 + ... + amn^m, where am is greater than zero. Simplifying, we get f(n) = 1 + n + n^2 + ... + n^m. Therefore, the time complexity is Θ(n^m). 54 | 55 | --- 56 | -------------------------------------------------------------------------------- /Binary Search/Basic Questions/basicquestions.md: -------------------------------------------------------------------------------- 1 | Problem Statement:- 2 | On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost ai Egyptian pounds. If Sagheer buys k items with indices x1, x2, ..., xk, then the cost of item xj is axj + xj·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k. 3 | 4 | Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task? 5 | 6 | Input 7 | The first line contains two integers n and S (1 ≤ n ≤ 105 and 1 ≤ S ≤ 109) — the number of souvenirs in the market and Sagheer's budget. 8 | 9 | The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 105) — the base costs of the souvenirs. 10 | 11 | Output 12 | On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs. 13 | 14 | Problem Link:https://codeforces.com/problemset/problem/812/C 15 | 16 | Solution/Code: 17 | #include 18 | #define ll long long int 19 | #define ul unsigned long long int 20 | #define pb push_back 21 | 22 | using namespace std; 23 | 24 | int main() 25 | { 26 | ll n,d; 27 | std::vector v; 28 | ll A[100000]; 29 | cin>>n>>d; 30 | for(int i=0;i>A[i]; 33 | } 34 | ll start=0; 35 | ll end=n; 36 | ll mid,sum=0; 37 | while(start<=end){ 38 | mid=(start+end)/2; 39 | for(ll i=0;i=sum){ 47 | start=mid+1; 48 | } 49 | else{ 50 | end=mid-1; 51 | } 52 | v.clear(); 53 | sum=0; 54 | } 55 | cout< 18 | using namespace std; 19 | 20 | int findMin(vector& arr, int low, int high){ 21 | // if array is not rotated 22 | if (arr[low] <= arr[high]){ 23 | return arr[low]; 24 | } 25 | // binary search starts herev 26 | while(low <= high){ 27 | int mid = (low + high) / 2; 28 | // Check if mid is the minimum element 29 | if(mid-1 >= 0 && arr[mid] < arr[mid - 1]){ 30 | return arr[mid]; 31 | } 32 | // If the left half is sorted, the minimum element must be in the right half 33 | if(arr[mid] > arr[high]){ 34 | low = mid + 1; 35 | } 36 | // If the right half is sorted, the minimum element must be in the left half 37 | else{ 38 | high = mid - 1; 39 | } 40 | } 41 | return -1; 42 | } 43 | 44 | int main(){ 45 | vector arr = { 5, 6, 1, 2, 3, 4 }; 46 | int N = arr.size(); 47 | cout << "The minimum element is "<< findMin(arr, 0, N - 1) << endl; 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Binary Search/Medium-Hard Questions/medium-hard-questions.md: -------------------------------------------------------------------------------- 1 | ## medium or hard level questions 2 | - ##### [Implement Binary Search to find minimum in Rotated Sorted Array](<01. MinInRotatedSortedArray.cpp>) 3 | -------------------------------------------------------------------------------- /Binary Search/Notes/Binary Search Notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Data-Science-Community-SRM/Algorithms-for-Programming/1669ed8a5ed3e4deaa2b378ad722d5d4f426f343/Binary Search/Notes/Binary Search Notes.pdf -------------------------------------------------------------------------------- /Binary Search/Notes/notes.md: -------------------------------------------------------------------------------- 1 | ## Notes 2 | 3 | Binary search is the most popular Search algorithm.It is efficient and also one of the most commonly used techniques that is used to solve problems. 4 | 5 | If all the names in the world are written down together in order and you want to search for the position of a specific name, binary search will accomplish this in a maximum of iterations. 6 | 7 | Binary search works only on a sorted set of elements. To use binary search on a collection, the collection must first be sorted. 8 | 9 | When binary search is used to perform operations on a sorted set, the number of iterations can always be reduced on the basis of the value that is being searched. 10 | 11 | Let us consider the following array: 12 | 13 | enter image description here 14 | 15 | By using linear search, the position of element 8 will be determined in the iteration. 16 | 17 | Let's see how the number of iterations can be reduced by using binary search. Before we start the search, we need to know the start and end of the range. Lets call them Low and High. 18 | 19 | Low = 0 20 | High = n-1 21 | Now, compare the search value with the element located at the median of the lower and upper bounds. If the value is greater, increase the lower bound, else decrease the upper bound. 22 | 23 | enter image description here 24 | 25 | Referring to the image above, the lower bound is and the upper bound is . The median of the lower and upper bounds is (lower_bound + upper_bound) / 2 = 4. Here a[4] = 4. The value 4>2, which is the value that you are searching for. Therefore, we do not need to conduct a search on any element beyond 4 as the elements beyond it will obviously be greater than 2. 26 | 27 | Therefore, we can always drop the upper bound of the array to the position of element 4. Now, we follow the same procedure on the same array with the following values: 28 | 29 | Low: 0 30 | High: 3 31 | Repeat this procedure recursively until Low > High. If at any iteration, we get , we return value of . This is the position of in the array. If is not present in the array, we return . 32 | 33 | Implementation: 34 | 35 | ``` 36 | int binarySearch(int low,int high,int key) 37 | { 38 | while(low<=high) 39 | { 40 | int mid=(low+high)/2; 41 | if(a[mid]key) 46 | { 47 | high=mid-1; 48 | } 49 | else 50 | { 51 | return mid; 52 | } 53 | } 54 | return -1; //key not found 55 | } 56 | ``` 57 | 58 | Time complexity 59 | 60 | As we dispose off one part of the search case during every step of binary search, and perform the search operation on the other half, this results in a worst case time complexity of . 61 | -------------------------------------------------------------------------------- /Breadth-first search/Basic Questions/01. Breadth-first-search.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | C++ program to run BFS on a Undirected Graph 3 | Functional for both weighted and unweighted graphs 4 | Contributed to Data-Science-Community-SRM on Github 5 | Contributor: Santhosh-Vardhan 6 | */ 7 | #include 8 | using namespace std; 9 | class graph 10 | { 11 | int v; 12 | vector> *adj;//Adjacency list that holds both vertex and weights 13 | public: 14 | graph(int ver)//When a graph is created, memory is allocated 15 | { 16 | v = ver; 17 | adj = new vector>[v+1]; 18 | } 19 | void addEdge(int st, int nd, int wt); 20 | void BFS(int st); 21 | }; 22 | void graph::addEdge(int st, int nd, int wt = 0) 23 | { 24 | //If an unweighted graph is required, don't pass in third argument while adding edges 25 | adj[st].push_back(make_pair(nd,wt));//Adds the edges to the adjacency list 26 | adj[nd].push_back(make_pair(st,wt)); 27 | } 28 | void graph::BFS(int st)//this bfs only prints the traversal, st - starting vertex 29 | { 30 | deque dq;//Queue for the BFS 31 | bool visited[v+1] = {false};//setting all visited to false 32 | dq.push_back(st); 33 | cout<<"--BFS--"< 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | class Graph 8 | { 9 | int V; 10 | list *adj; 11 | void DFS_visit(int v, bool visited[]); 12 | public: 13 | Graph(int V); 14 | void addEdge(int v, int w); 15 | void DFS(int v); 16 | }; 17 | 18 | Graph::Graph(int V) 19 | { 20 | this->V = V; 21 | adj = new list[V]; 22 | } 23 | 24 | void Graph::addEdge(int v, int w) 25 | { 26 | adj[v].push_back(w); 27 | } 28 | 29 | void Graph::DFS_visit(int v, bool visited[]) 30 | { 31 | 32 | visited[v] = true; 33 | cout << v << " "; 34 | list::iterator i; 35 | for (i = adj[v].begin(); i != adj[v].end();i++) 36 | { 37 | if (!visited[*i]) 38 | DFS_visit(*i, visited); 39 | } 40 | } 41 | 42 | 43 | void Graph::DFS(int v) 44 | { 45 | // Mark all the vertices as not visited 46 | bool *visited = new bool[V]; 47 | for (int i = 0; i < V; i++) 48 | visited[i] = false; 49 | 50 | 51 | DFS_visit(v, visited); 52 | } 53 | 54 | int main() 55 | { 56 | Graph g(4); 57 | g.addEdge(0, 1); 58 | g.addEdge(0, 2); 59 | g.addEdge(1, 2); 60 | g.addEdge(2, 0); 61 | g.addEdge(2, 3); 62 | g.addEdge(3, 3); 63 | 64 | cout << "Following is Depth First Traversal \n"; 65 | int n; 66 | cin>>n; 67 | g.DFS(n); 68 | 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /Depth-First Search/dfs.md: -------------------------------------------------------------------------------- 1 | Here goes the info -------------------------------------------------------------------------------- /Fibonacci/Basic Questions/basicquestions.md: -------------------------------------------------------------------------------- 1 | ## basic questions 2 | -------------------------------------------------------------------------------- /Fibonacci/Basic Questions/fibonacci.hs: -------------------------------------------------------------------------------- 1 | -- Function to calculate the n-th number in the Fibonacci sequence 2 | fibonacci :: Integer -> Integer 3 | fibonacci 0 = 0 4 | fibonacci 1 = 1 5 | fibonacci n = fibonacci (n - 1) + fibonacci (n - 2) 6 | 7 | -- Example of usage: 8 | main = do 9 | let n = 10 10 | putStrLn ("The " ++ show n ++ "th number in the Fibonacci sequence is " ++ show (fibonacci n)) 11 | -------------------------------------------------------------------------------- /Fibonacci/Medium-Hard Questions/medium-hard-questions.md: -------------------------------------------------------------------------------- 1 | ## medium or hard level questions 2 | -------------------------------------------------------------------------------- /Fibonacci/Notes/notes.md: -------------------------------------------------------------------------------- 1 | ## Notes 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Graph representation/Basic Questions/basicquestions.md: -------------------------------------------------------------------------------- 1 | ## basic questions 2 | -------------------------------------------------------------------------------- /Graph representation/Medium-Hard Questions/medium-hard-questions.md: -------------------------------------------------------------------------------- 1 | ## medium or hard level questions 2 | -------------------------------------------------------------------------------- /Graph representation/Notes/notes.md: -------------------------------------------------------------------------------- 1 | ## Notes 2 | -------------------------------------------------------------------------------- /Header.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Data-Science-Community-SRM/Algorithms-for-Programming/1669ed8a5ed3e4deaa2b378ad722d5d4f426f343/Header.png -------------------------------------------------------------------------------- /Insertion sort/Basic Questions/01. Insertion-sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | C++ program to demonstrate the implementation of Insertion Sort 3 | Contributed to Data-Science-Community-SRM on Github 4 | Contributor: Santhosh-Vardhan 5 | */ 6 | #include 7 | using namespace std; 8 | void insertionSort(int arr[], int n) //two arguments - the array(pointer) and the number of elements 9 | { 10 | for (int i = 1; i < n; i++) 11 | { 12 | int cEle = arr[i]; //Storing the current value in cEle 13 | int j = i - 1; 14 | while (j >= 0 && arr[j] > cEle) //Comparing with the sorted side of the array 15 | { 16 | arr[j + 1] = arr[j];//Changing the value 17 | j--; 18 | } 19 | arr[j + 1] = cEle; //Setting the value of cEle at the place where the loop stopped 20 | } 21 | } 22 | int main() 23 | { 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Insertion sort/Basic Questions/basicquestions.md: -------------------------------------------------------------------------------- 1 | ## basic questions 2 | - ##### [Implement Insertion sort and sort an array using it.](01.%20Insertion-sort.cpp) 3 | -------------------------------------------------------------------------------- /Insertion sort/Medium-Hard Questions/medium-hard-questions.md: -------------------------------------------------------------------------------- 1 | ## medium or hard level questions 2 | -------------------------------------------------------------------------------- /Insertion sort/Notes/Insertion sort notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Data-Science-Community-SRM/Algorithms-for-Programming/1669ed8a5ed3e4deaa2b378ad722d5d4f426f343/Insertion sort/Notes/Insertion sort notes.pdf -------------------------------------------------------------------------------- /Insertion sort/Notes/notes.md: -------------------------------------------------------------------------------- 1 | ## Notes 2 | 3 | 4 | Insertion sort is based on the idea that one element from the input elements is consumed in each iteration to find its correct position i.e, the position to which it belongs in a sorted array. 5 | 6 | It iterates the input elements by growing the sorted array at each iteration. It compares the current element with the largest value in the sorted array. If the current element is greater, then it leaves the element in its place and moves on to the next element else it finds its correct position in the sorted array and moves it to that position. This is done by shifting all the elements, which are larger than the current element, in the sorted array to one position ahead 7 | 8 | Implementation 9 | ``` 10 | void insertion_sort ( int A[ ] , int n) 11 | { 12 | for( int i = 0 ;i < n ; i++ ) { 13 | /*storing current element whose left side is checked for its 14 | correct position .*/ 15 | 16 | int temp = A[ i ]; 17 | int j = i; 18 | 19 | /* check whether the adjacent element in left side is greater or 20 | less than the current element. */ 21 | 22 | while( j > 0 && temp < A[ j -1]) { 23 | 24 | // moving the left side element to one position forward. 25 | A[ j ] = A[ j-1]; 26 | j= j - 1; 27 | 28 | } 29 | // moving current element to its correct position. 30 | A[ j ] = temp; 31 | } 32 | } 33 | ``` 34 | 35 | Pseudocode for insertion sort 36 | 37 | Now that you know how to insert a value into a sorted subarray, you can implement insertion sort: 38 | Call insert to insert the element that starts at index 1 into the sorted subarray in index 0. 39 | Call insert to insert the element that starts at index 2 into the sorted subarray in indices 0 through 1. 40 | Call insert to insert the element that starts at index 3 into the sorted subarray in indices 0 through 2. 41 | … 42 | Finally, call insert to insert the element that starts at index n-1n−1n, minus, 1 into the sorted subarray in indices 0 through n-2n−2n, minus, 2. 43 | As a reminder, here's the visualization that steps through the algorithm on a deck of cards: 44 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Data Science Community SRM 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Merge sort/Basic Questions/01. merge-two-sorted-arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Naive Solution 5 | // O((m + n) * log(m + n)) 6 | void merge(int a[], int b[], int m, int n) 7 | { 8 | int c[m + n]; 9 | for (int i = 0; i < m; i++) 10 | c[i] = a[i]; 11 | for (int j = 0; j < n; j++) 12 | c[j + m] = a[j]; 13 | sort(c, c + m + n); 14 | for (int i = 0; i < m + n; i++) 15 | cout << c[i] << " "; 16 | } 17 | 18 | // Efficient Solution 19 | // O(m + n) 20 | void merge_one(int a[], int b[], int m, int n) 21 | { 22 | int i = 0, j = 0; 23 | while (i < m && j < n) 24 | { 25 | if (a[i] < b[j]) 26 | cout << a[i++] << " "; 27 | else 28 | cout << b[j++]; 29 | } 30 | while (i < m) 31 | cout << a[i++] << " "; 32 | while (j < n) 33 | cout << b[j++] << " "; 34 | } 35 | 36 | int main() 37 | { 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Merge sort/Basic Questions/02. merge-sort-algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void merge(int arr[], int l, int m, int r); 5 | 6 | // Merge sort Algorithm 7 | void mergeSort(int arr[], int l, int r) 8 | { 9 | if (r > l) // atleast 2 elements 10 | { 11 | int m = l + (r - l) / 2; // same as (l + r) / 2 12 | 13 | // sort 1st & 2nd halves 14 | mergeSort(arr, l, m); 15 | mergeSort(arr, m + 1, r); 16 | 17 | // merge the two sorted halves 18 | merge(arr, l, m, r); 19 | } 20 | } 21 | 22 | // elements from l to m are sorted 23 | // elements form m to h are sorted 24 | // we need to merge the both parts of array 25 | // l <= m < h 26 | // time : O(n1 + n2) = O(n) 27 | // spce : O(n) 28 | void merge(int arr[], int l, int m, int r) 29 | { 30 | int n1 = m - l + 1, n2 = r - m; 31 | int left[n1], right[n2]; 32 | 33 | // copying data 34 | for (int i = 0; i < n1; i++) 35 | left[i] = arr[l + i]; 36 | for (int j = 0; j < n2; j++) 37 | right[j] = arr[m + 1 + j]; 38 | 39 | // merging 40 | int i = 0, j = 0, k = l; 41 | while (i < n1 && j < n2) 42 | { 43 | if (left[i] <= right[j]) 44 | arr[k++] = left[i++]; 45 | else 46 | arr[k++] = right[j++]; 47 | } 48 | 49 | // filling remaining elements 50 | while (i < n1) 51 | arr[k++] = left[i++]; 52 | while (j < n2) 53 | arr[k++] = right[j++]; 54 | } 55 | 56 | int main() 57 | { 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Merge sort/Basic Questions/03. merge-two-sorted-arrays.py: -------------------------------------------------------------------------------- 1 | ## Problem : Given two sorted arrays arr1[] of size N and arr2[] of size M. 2 | ## Each array is sorted in non-decreasing order. 3 | ## Merge the two arrays into one sorted array in non-decreasing order without using any extra space. 4 | 5 | ## Input: T(number of testcases),N,M and elements of sorted arrays arr1,arr2 6 | 7 | ## Program: 8 | 9 | 10 | t = int(input()) 11 | for i in range(t): 12 | n1,n2 = map(int,input().split()) 13 | arr1 = list(map(int,input().split())) 14 | arr2 = list(map(int,input().split())) 15 | arr1 = arr1 + arr2 16 | arr1.sort() 17 | for i in arr1: 18 | print(i,end=" ") 19 | print() 20 | t = t -1 21 | -------------------------------------------------------------------------------- /Merge sort/Basic Questions/basicquestions.md: -------------------------------------------------------------------------------- 1 | ## Basic questions 2 | 3 | - ##### [Given two sorted arrays. You have to merge them such that resultant array is also sorted.](01.%20merge-two-sorted-arrays.cpp) 4 | 5 | - ##### [Merge Sort Algorithm](02.%20merge-sort-algorithm.cpp) 6 | 7 | - ##### [Python Program for Merging two arrays sorted in non-descending order such that resultant array is also sorted in the same manner](03.%20merge-two-sorted-arrays.py) 8 | -------------------------------------------------------------------------------- /Merge sort/Medium-Hard Questions/medium-hard-questions.md: -------------------------------------------------------------------------------- 1 | ## medium or hard level questions 2 | -------------------------------------------------------------------------------- /Merge sort/Notes/Merge sort algorithm notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Data-Science-Community-SRM/Algorithms-for-Programming/1669ed8a5ed3e4deaa2b378ad722d5d4f426f343/Merge sort/Notes/Merge sort algorithm notes.pdf -------------------------------------------------------------------------------- /Merge sort/Notes/notes.md: -------------------------------------------------------------------------------- 1 | ## Notes 2 | 3 | Merge sort is a divide-and-conquer algorithm based on the idea of breaking down a list into several sub-lists until each sublist consists of a single element and merging those sublists in a manner that results into a sorted list. 4 | 5 | Idea: 6 | 7 | Divide the unsorted list into N sublists, each containing 1 element. 8 | Take adjacent pairs of two singleton lists and merge them to form a list of 2 elements.N will now convert into N/2 lists of size 2. 9 | Repeat the process till a single sorted list of obtained. 10 | While comparing two sublists for merging, the first element of both lists is taken into consideration. While sorting in ascending order, the element that is of a lesser value becomes a new element of the sorted list. This procedure is repeated until both the smaller sublists are empty and the new combined sublist comprises all the elements of both the sublists. 11 | 12 | 13 | An implementation has been provided below : 14 | ``` 15 | void merge(int A[ ] , int start, int mid, int end) { 16 | //stores the starting position of both parts in temporary variables. 17 | int p = start ,q = mid+1; 18 | 19 | int Arr[end-start+1] , k=0; 20 | 21 | for(int i = start ;i <= end ;i++) { 22 | if(p > mid) //checks if first part comes to an end or not . 23 | Arr[ k++ ] = A[ q++] ; 24 | 25 | else if ( q > end) //checks if second part comes to an end or not 26 | Arr[ k++ ] = A[ p++ ]; 27 | 28 | else if( A[ p ] < A[ q ]) //checks which part has smaller element. 29 | Arr[ k++ ] = A[ p++ ]; 30 | 31 | else 32 | Arr[ k++ ] = A[ q++]; 33 | } 34 | for (int p=0 ; p< k ;p ++) { 35 | /* Now the real array has elements in sorted manner including both 36 | parts.*/ 37 | A[ start++ ] = Arr[ p ] ; 38 | } 39 | } 40 | ``` 41 | Here, in merge function, we will merge two parts of the arrays where one part has starting and ending positions from start to mid respectively and another part has positions from mid+1 to the end. 42 | 43 | A beginning is made from the starting parts of both arrays. i.e. p and q. Then the respective elements of both the parts are compared and the one with the smaller value will be stored in the auxiliary array (Arr[ ]). If at some condition ,one part comes to end ,then all the elements of another part of array are added in the auxiliary array in the same order they exist. 44 | 45 | Now consider the following 2 branched recursive function : 46 | ``` 47 | void merge_sort (int A[ ] , int start , int end ) 48 | { 49 | if( start < end ) { 50 | int mid = (start + end ) / 2 ; // defines the current array in 2 parts . 51 | merge_sort (A, start , mid ) ; // sort the 1st part of array . 52 | merge_sort (A,mid+1 , end ) ; // sort the 2nd part of array. 53 | 54 | // merge the both parts by comparing elements of both the parts. 55 | merge(A,start , mid , end ); 56 | } 57 | } 58 | ``` 59 | -------------------------------------------------------------------------------- /Quick sort/Basic Questions/Sorting an array using Quick Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void swap(int* a, int* b) 4 | { 5 | int t = *a; 6 | *a = *b; 7 | *b = t; 8 | } 9 | int partition (int arr[], int low, int high) 10 | { 11 | int pivot = arr[high]; 12 | int i = (low - 1); 13 | 14 | for (int j = low; j <= high - 1; j++) 15 | { 16 | if (arr[j] < pivot) 17 | { 18 | i++; 19 | swap(&arr[i], &arr[j]); 20 | } 21 | } 22 | swap(&arr[i + 1], &arr[high]); 23 | return (i + 1); 24 | } 25 | void quickSort(int arr[], int low, int high) 26 | { 27 | if (low < high) 28 | { 29 | int pi = partition(arr, low, high); 30 | quickSort(arr, low, pi - 1); 31 | quickSort(arr, pi + 1, high); 32 | } 33 | } 34 | void printArray(int arr[], int size) 35 | { 36 | int i; 37 | for (i = 0; i < size; i++) 38 | cout << arr[i] << " "; 39 | cout << endl; 40 | } 41 | int main() 42 | { 43 | int arr[] = {3, 25, 8, 19, 10, 5}; 44 | int n = sizeof(arr) / sizeof(arr[0]); 45 | quickSort(arr, 0, n - 1); 46 | cout << "Sorted array: \n"; 47 | printArray(arr, n); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Quick sort/Basic Questions/basicquestions.md: -------------------------------------------------------------------------------- 1 | ## basic questions 2 | 3 | - #### [Sorting an array using Quick Sort](Sorting%20an%20array%20using%20Quick%20Sort.cpp) 4 | -------------------------------------------------------------------------------- /Quick sort/Medium-Hard Questions/medium-hard-questions.md: -------------------------------------------------------------------------------- 1 | ## medium or hard level questions 2 | -------------------------------------------------------------------------------- /Quick sort/Notes/notes.md: -------------------------------------------------------------------------------- 1 | ## Notes 2 | 3 | Quick sort is based on the divide-and-conquer approach based on the idea of choosing one element as a pivot element and partitioning the array around it such that: Left side of pivot contains all the elements that are less than the pivot element Right side contains all elements greater than the pivot 4 | 5 | It reduces the space complexity and removes the use of the auxiliary array that is used in merge sort. Selecting a random pivot in an array results in an improved time complexity in most of the cases. 6 | ``` 7 | int partition ( int A[],int start ,int end) { 8 | int i = start + 1; 9 | int piv = A[start] ; //make the first element as pivot element. 10 | for(int j =start + 1; j <= end ; j++ ) { 11 | /*rearrange the array by putting elements which are less than pivot 12 | on one side and which are greater that on other. */ 13 | 14 | if ( A[ j ] < piv) { 15 | swap (A[ i ],A [ j ]); 16 | i += 1; 17 | } 18 | } 19 | swap ( A[ start ] ,A[ i-1 ] ) ; //put the pivot element in its proper place. 20 | return i-1; //return the position of the pivot 21 | } 22 | ``` 23 | Now, let us see the recursive function Quick_sort : 24 | ``` 25 | void quick_sort ( int A[ ] ,int start , int end ) { 26 | if( start < end ) { 27 | //stores the position of pivot element 28 | int piv_pos = partition (A,start , end ) ; 29 | quick_sort (A,start , piv_pos -1); //sorts the left side of pivot. 30 | quick_sort ( A,piv_pos +1 , end) ; //sorts the right side of pivot. 31 | } 32 | } 33 | ``` 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [![HitCount](http://hits.dwyl.com/nehanagle/Algorithms-for-Programming.svg)](http://hits.dwyl.com/nehanagle/Algorithms-for-Programming) 3 | 4 | 5 |

6 | 7 | 8 | 9 |

Algorithms for Programming

10 |

Hey folks!, Here is the Repo to kickstart your programming journey.

11 |

12 | 13 | --- 14 | [![C++ Docs](https://img.shields.io/badge/Documentation-see%20docs-green?style=flat-square&logo=appveyor)](https://devdocs.io/cpp/) 15 | [![Complier](https://img.shields.io/badge/User%20Interface-Link%20to%20UI-orange?style=flat-square&logo=appveyor)](https://www.onlinegdb.com/online_c++_compiler) 16 | 17 | ## Preview 18 | - A curated list of algorithms to learn and/or practice algorithms. Inspired by the curiosity for learning programming from scratch 19 | 20 | If you want to contribute, please read the contribution guidelines. 21 | ## Topics 22 | - [ ] [Binary search](https://github.com/Data-Science-Community-SRM/Algorithms-for-Programming/tree/master/Binary%20Search) 23 | - [ ] [Asymptotic notation](https://github.com/Data-Science-Community-SRM/Algorithms-for-Programming/tree/master/Asymptotic%20notation) 24 | - [ ] [Selection sort](https://github.com/Data-Science-Community-SRM/Algorithms-for-Programming/tree/master/Selection%20sort) 25 | - [ ] [Insertion sort](https://github.com/Data-Science-Community-SRM/Algorithms-for-Programming/tree/master/Insertion%20sort) 26 | - [ ] [Recursive algorithms](https://github.com/Data-Science-Community-SRM/Algorithms-for-Programming/tree/master/Recursive%20algorithms) 27 | - [ ] [Towers of Hanoi](https://github.com/Data-Science-Community-SRM/Algorithms-for-Programming/tree/master/Towers%20of%20Hanoi) 28 | - [ ] [Merge sort](https://github.com/Data-Science-Community-SRM/Algorithms-for-Programming/tree/master/Merge%20sort) 29 | - [ ] [Quick sort](https://github.com/Data-Science-Community-SRM/Algorithms-for-Programming/tree/master/Quick%20sort) 30 | - [ ] [Graph representation](https://github.com/Data-Science-Community-SRM/Algorithms-for-Programming/tree/master/Graph%20representation) 31 | - [ ] [Breadth-first search](https://github.com/Data-Science-Community-SRM/Algorithms-for-Programming/tree/master/Breadth-first%20search) 32 | - [ ] [Depth-First Search](https://github.com/aryanraj2713/Algorithms-for-Programming/tree/master/Depth-First%20Search) 33 | - [ ] Further learning 34 | 35 | 36 | 37 |
38 | 39 | 40 | ## Resources 41 | 42 | * Pre-requisites: 43 | - Learn Basic Language C, C++ or Python 44 | - Practice on online coding platforms (hackerrank, spoj, codechef, codeforces) 45 | 46 | * Online courses 47 | 48 | - [ ] [Algorithms: Divide and Conquer, Sorting and Searching, and Randomized Algorithms](https://www.coursera.org/learn/algorithms-divide-conquer) - The primary topics are: asymptotic ("Big-oh") notation, sorting and searching, divide and conquer, and randomized algorithms. 49 | - [ ] [Algorithms: Graph Search, Shortest Paths, and Data Structures](https://www.coursera.org/learn/algorithms-graphs-data-structures) - The primary topics are: data structures, graph primitives, and their applications. 50 | - [ ] [Algorithms: Greedy Algorithms, Minimum Spanning Trees, and Dynamic Programming](https://www.coursera.org/learn/algorithms-greedy) - The primary topics are: greedy algorithms and dynamic programming. 51 | - [ ] [Algorithms: Shortest Paths Revisited, NP-Complete Problems and What To Do About Them](https://www.coursera.org/learn/algorithms-npcomplete) - The primary topics are: shortest paths, NP-completeness and what it means for the algorithm designer, and strategies for coping with computationally intractable problems. 52 | - [ ] [Algorithms, Part I](https://www.coursera.org/learn/algorithms-part1/home/welcome) - This course covers the essential information that every serious programmer needs to know about algorithms and data structures.Part I covers elementary data structures, sorting, and searching algorithms. 53 | - [ ] [Algorithms, Part II](https://www.coursera.org/learn/algorithms-part2) - Part II focuses on graph- and string-processing algorithms.. 54 | - [ ] [MIT - 6-006](http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/) - Well explained algorithms. 55 | - [ ] [MIT - 6-046j](http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-046j-introduction-to-algorithms-sma-5503-fall-2005/video-lectures/) - Similar to the previous one, but with different algorithms. 56 | - [ ] [MIT - 6-00sc](http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/index.htm) - An easy and well explained introduction to algorithms. 57 | - [ ] [Udacity Intro to Algorithms](https://www.udacity.com/course/intro-to-algorithms--cs215) - Python-based Algorithms course. 58 | - [ ] [Algorithms in Motion](https://www.manning.com/livevideo/algorithms-in-motion) - Beginner's algorithms course with fun illustrations, based on the book Grokking Algorithms 59 | 60 | 61 | ## File structure 62 | 63 | ``` 64 | ├── Algorithm Name 65 | │ ├── Notes 66 | │ ├── notes.md 67 | | ├──Simple Questions 68 | | ├──basicquestion.md 69 | | ├──Medium or Hard Questions 70 | | ├──medium-hard-questions.md 71 | └── README.md 72 | ``` 73 | 74 | ## Blogs 75 | 76 | *Awesome list of blogs, mainly for competitive programming but you can refer to these when learning a new topic/algorithm* 77 | 78 | - [ ] [An awesome list for competitive programming!](https://codeforces.com/blog/entry/23054) - Awesome blog for all the resources and list of books and algorithms. 79 | - [ ] [Algorithms Weekly](https://petr-mitrichev.blogspot.com/) - A good blog by Petr Mitrichev, mainly in Java. 80 | - [ ] [Sport of Programming](https://www.hackerearth.com/practice/notes/getting-started-with-the-sport-of-programming/) - Really informative blog for starting with the sport of programming. 81 | - [ ] [Algorithms and Data Structures](http://www.allisons.org/ll/AlgDS/) - For getting deeper knowledge of algorithms and how to think in right direction. 82 | 83 | ## Tools 84 | 85 | *Some tools that can help you in the learning of algorithms* 86 | 87 | - [ ] [interactive-coding-challenges](https://github.com/donnemartin/interactive-coding-challenges) - Interactive, test-driven coding challenges (algorithms and data structures). 88 | 89 | ## License 90 | [![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](https://github.com/Data-Science-Community-SRM/Algorithms-for-Programming/blob/master/LICENSE.md) 91 | 92 | 93 |

94 | Made with :heart: by DS Community SRM 95 |

96 | 97 | -------------------------------------------------------------------------------- /Recursive algorithms/Basic Questions/basicquestions.md: -------------------------------------------------------------------------------- 1 | ## basic questions 2 | -------------------------------------------------------------------------------- /Recursive algorithms/Medium-Hard Questions/medium-hard-questions.md: -------------------------------------------------------------------------------- 1 | ## medium or hard level questions 2 | -------------------------------------------------------------------------------- /Recursive algorithms/Notes/notes.md: -------------------------------------------------------------------------------- 1 | ## Notes 2 | 3 | 4 | # Definition 5 | Recursion refers to making a function call within the same function until a specific condition is satisfied. 6 | This algorithm is very useful for programs that require you to do the same thing for different parameters. It can make code shorter and more readable. 7 | 8 | It usually results in a bit higher complexity so it should be used only when no other options seem viable. It can also result in crashes if not used properly i.e no proper condition is applied for stopping the function call. 9 | 10 | 11 | ## Some examples where recursion can be used are: 12 | 13 | 1. Factorial: 14 | 15 | ```c 16 | int factorial(int a){ 17 | return (a == 1 || a == 0) ? 1 : a * factorial(a - 1); 18 | } 19 | 20 | ``` 21 | 22 | 2. Fibonacci Sequence: 23 | 24 | ```c 25 | int fibonacci(int a){ 26 | return (n == 0 || n == 1) ? n : fibonacci(n - 1) + fibonacci(n - 2); 27 | } 28 | ``` 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /Selection sort/Basic Questions/01. Selection-sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | C++ program to demonstrate the implementation of Selection sort 3 | Contributed to Data-Science-Community-SRM on Github 4 | Contributor: Santhosh-Vardhan 5 | */ 6 | #include 7 | using namespace std; 8 | void selectionSort(int arr[], int n) //two arguments - the array(pointer) and the number of elements 9 | { 10 | for (int i = 0; i < n-1; i++) 11 | { 12 | int minLoc = i; 13 | for (int j = i+1; j < n; j++)//traverse through the array and find the location of the minimum index 14 | if (arr[j] < arr[minLoc]) 15 | minLoc = j; 16 | swap(arr[minLoc],arr[i]);//Swap the values of the minimum element right now and the current location 17 | } 18 | } 19 | int main() 20 | { 21 | } 22 | -------------------------------------------------------------------------------- /Selection sort/Basic Questions/basicquestions.md: -------------------------------------------------------------------------------- 1 | ## basic questions 2 | - ##### [Implement Selection sort and sort an array using it.](01.%20Selection-sort.cpp) 3 | -------------------------------------------------------------------------------- /Selection sort/Medium-Hard Questions/medium-hard-questions.md: -------------------------------------------------------------------------------- 1 | ## medium or hard level questions 2 | -------------------------------------------------------------------------------- /Selection sort/Notes/Selection Sort Notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Data-Science-Community-SRM/Algorithms-for-Programming/1669ed8a5ed3e4deaa2b378ad722d5d4f426f343/Selection sort/Notes/Selection Sort Notes.pdf -------------------------------------------------------------------------------- /Selection sort/Notes/notes.md: -------------------------------------------------------------------------------- 1 | ## Notes 2 | -------------------------------------------------------------------------------- /Towers of Hanoi/Basic Questions/01. towerofhanoi.py: -------------------------------------------------------------------------------- 1 | ## Tower of Hanoi 2 | ## move n pieces from source to destination 3 | ## using a temporary location 4 | 5 | ## Program: 6 | 7 | def tower(n,start,end,middle): 8 | if n==1: 9 | print("Move %i from tower %s to tower %s" %(n,start,end)) 10 | else: 11 | tower(n-1,start,middle,end) 12 | print("Move %i from tower %s to tower %s" %(n,start,end)) 13 | tower(n-1,middle,end,start) 14 | 15 | tower(4,"A","C","B") 16 | -------------------------------------------------------------------------------- /Towers of Hanoi/Basic Questions/basicquestions.md: -------------------------------------------------------------------------------- 1 | ## Basic questions 2 | 3 | - ##### [Using recursive strategy in Python to solve the Tower of Hanoi problem.](01.%20towerofhanoi.py) 4 | -------------------------------------------------------------------------------- /Towers of Hanoi/Medium-Hard Questions/medium-hard-questions.md: -------------------------------------------------------------------------------- 1 | ## medium or hard level questions 2 | -------------------------------------------------------------------------------- /Towers of Hanoi/Notes/Tower of Hanoi Notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Data-Science-Community-SRM/Algorithms-for-Programming/1669ed8a5ed3e4deaa2b378ad722d5d4f426f343/Towers of Hanoi/Notes/Tower of Hanoi Notes.pdf -------------------------------------------------------------------------------- /Towers of Hanoi/Notes/notes.md: -------------------------------------------------------------------------------- 1 | ## Notes 2 | -------------------------------------------------------------------------------- /contributing.md: -------------------------------------------------------------------------------- 1 | Contributing 2 | ============ 3 | If you want to contribute to this project and make it better, your help is very welcome. 4 | Contributing is also a great way to learn more about social coding on Github, new technologies and and their ecosystems and how to make constructive, helpful bug reports, feature requests and the noblest of all contributions: a good, clean pull request. 5 | 6 | ### How to make a clean pull request 7 | 8 | The project's contribution instructions are as follows. 9 | 10 | - Create a personal fork of this project on Github. 11 | - Clone the fork on your local machine. Your remote repo on Github is called `origin`. 12 | - Add the original repository as a remote called `upstream`. 13 | - If you created your fork a while ago be sure to pull upstream changes into your local repository. 14 | - Create a new branch to work on! Branch from `develop` if it exists, else from `master`. 15 | - Implement/fix your feature, comment your code. 16 | - Follow the writing style of the project, including indentation. 17 | - Write as needed. 18 | - Add or change the documentation as needed. 19 | - Squash your commits into a single commit with git's [interactive rebase](https://gist.github.com/todgru/7cff3962e76e1360cc2c). Create a new branch if necessary. 20 | - Push your branch to your fork on Github, the remote `origin`. 21 | - From your fork open a pull request in the correct branch. Target the project's `develop` branch if there is one, else go for `master`! 22 | - If the maintainer requests further changes just push them to your branch. The PR will be updated automatically. 23 | - Once the pull request is approved and merged you can pull the changes from `upstream` to your local repo and delete 24 | your extra branch(es). 25 | 26 | And last but not least: Always write your commit messages in the present tense. Your commit message should describe what the commit is. 27 | -------------------------------------------------------------------------------- /logo-light.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Data-Science-Community-SRM/Algorithms-for-Programming/1669ed8a5ed3e4deaa2b378ad722d5d4f426f343/logo-light.png --------------------------------------------------------------------------------