├── Recursion ├── factorial.cpp └── recursiveBinarySeach.cpp ├── Sorting ├── selectionSort.cpp ├── bubbleSort.cpp ├── quickSort.cpp ├── insertionSort.cpp ├── mergeSort.cpp └── countingSort.cpp ├── Searching ├── linearSearch.cpp └── binarySearch.cpp ├── DynamicProgramming └── matrixMultiplication.cpp ├── Greedy ├── fractionalKnapsack.cpp ├── countingMoney.cpp ├── activitySelection.cpp └── jobSequencing.cpp └── README.md /Recursion/factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int factorial(int x) { 5 | int fact; 6 | if(x<=1) { 7 | return 1; 8 | } 9 | fact = x * factorial(x-1); 10 | 11 | return fact; 12 | } 13 | 14 | int main() { 15 | 16 | int n; 17 | cout<< "Please enter a non-negative integer value: "; 18 | cin>> n; 19 | 20 | while(n<0) { 21 | cout<< "The value is negative.\nPlease enter a non-negative integer value: "; 22 | cin>> n; 23 | } 24 | 25 | int fact = factorial(n); 26 | 27 | cout<< "Factorial of "<< n<< ": "<< fact<< endl; 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Sorting/selectionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void selectionSort(int arr[], int n) 5 | { 6 | int i, j, minIndex; 7 | 8 | for (i = 0; i < n - 1; i++) { 9 | 10 | minIndex = i; 11 | for (j=i+1; j 2 | using namespace std; 3 | 4 | int linearSearch(int arr[], int n, int element) { 5 | for(int i=0; i> n; 18 | 19 | int arr[n]; 20 | cout<< "Enter array elements: "; 21 | for(int i=0; i> arr[i]; 23 | } 24 | 25 | int value; 26 | cout<< "Enter a value to search: "; 27 | cin>> value; 28 | 29 | int foundIndex = linearSearch(arr, n, value); 30 | if(foundIndex == -1) { 31 | cout<< "Element not found!"<< endl; 32 | } else { 33 | cout<< "Element found at index "<< foundIndex<< "!"<< endl; 34 | } 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Sorting/bubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void bubbleSort(int arr[], int n) { 5 | bool swapped; 6 | for(int i=0; i arr[j+1]) { 10 | swap(arr[j], arr[j+1]); 11 | swapped = true; 12 | } 13 | } 14 | if(swapped == false) 15 | break; 16 | } 17 | } 18 | 19 | int main() { 20 | 21 | int n; 22 | cout<< "Please enter array size: "; 23 | cin>> n; 24 | 25 | int arr[n]; 26 | cout<< "Please enter array elements: "; 27 | for(int i=0; i> arr[i]; 29 | } 30 | 31 | bubbleSort(arr, n); 32 | 33 | cout<< "Sorted array: "; 34 | for(int i=0; i 2 | using namespace std; 3 | 4 | void swap(int arr[], int i, int j) { 5 | int temp = arr[i]; 6 | arr[i] = arr[j]; 7 | arr[j] = temp; 8 | } 9 | 10 | int partition(int arr[], int l, int r) { 11 | 12 | int pivot = arr[r]; 13 | 14 | int i = l-1; 15 | for(int j=l; j 2 | using namespace std; 3 | 4 | void insertionSort(int arr[], int n) { 5 | 6 | for(int i=1; i=0 && arr[j]>key) { 11 | arr[j+1] = arr[j]; 12 | j = j-1; 13 | } 14 | 15 | arr[j+1] = key; 16 | } 17 | 18 | } 19 | 20 | void insertElements(int arr[], int n) { 21 | 22 | for(int i=0; i> arr[i]; 24 | } 25 | } 26 | 27 | void displayArray(int arr[], int n) { 28 | 29 | for(int i=0; i> n; 40 | 41 | int arr[n]; 42 | cout<< "Enter array elements: "; 43 | insertElements(arr, n); 44 | 45 | insertionSort(arr, n); 46 | cout<< "Sorted array: "; 47 | displayArray(arr, n); 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Sorting/mergeSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void merge(int arr[], int l, int mid, int r) { 5 | 6 | int n1 = mid-l+1; 7 | int n2 = r-mid; 8 | 9 | int a[n1]; 10 | int b[n2]; 11 | 12 | for(int i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | #define vi vector 10 | #define pii pair 11 | #define vii vector 12 | #define rep(i,a,b) for(int i=a; i=b; i--) 14 | #define for_each(x,y) for(auto &x : y) 15 | #define pb push_back 16 | #define ff first 17 | #define ss second 18 | 19 | bool binarySearch(vi v, int val, int l, int r) 20 | { 21 | if(r >= l) { 22 | // int mid = l + (r - l) / 2; 23 | int mid = ( l + r) / 2; 24 | 25 | if(v[mid] == val) { 26 | return true; 27 | } 28 | 29 | if(v[mid] > val) { 30 | return binarySearch(v, val, l, mid - 1); 31 | } 32 | 33 | if(v[mid] < val) { 34 | return binarySearch(v, val, mid + 1, r); 35 | } 36 | } 37 | 38 | return false; 39 | } 40 | 41 | int main() 42 | { 43 | cout<< "Input array size: "; 44 | int n; cin>> n; 45 | 46 | vi v(n); 47 | cout<< "Input array elements: "; 48 | rep(i,0,v.size()) 49 | { 50 | cin>> v[i]; 51 | } 52 | 53 | cout<< "Enter value to search: "; 54 | int val; cin>> val; 55 | 56 | cout<< "Sorted array: "; 57 | sort(v.begin(), v.end()); 58 | for_each(element, v) 59 | { 60 | cout<< element<< " "; 61 | } 62 | cout<< endl; 63 | 64 | bool result = binarySearch(v, val, 0, v.size()-1); 65 | 66 | cout<< "Element found: "<< result<< endl; 67 | 68 | return 0; 69 | } -------------------------------------------------------------------------------- /DynamicProgramming/matrixMultiplication.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | #define vi vector 10 | #define pii pair 11 | #define vii vector 12 | #define rep(i,a,b) for(int i=a; i=b; i--) 14 | #define for_each(x,y) for(auto &x : y) 15 | #define pb push_back 16 | #define ff first 17 | #define ss second 18 | 19 | int main() { 20 | int row1, col1; 21 | cin >> row1 >> col1; 22 | 23 | int A[row1][col1]; 24 | for (int i = 0; i < row1; i++) { 25 | for (int j = 0; j < col1; j++) { 26 | cin >> A[i][j]; 27 | } 28 | } 29 | 30 | int row2, col2; 31 | cin >> row2 >> col2; 32 | 33 | int B[row2][col2]; 34 | for (int i = 0; i < row2; i++) { 35 | for (int j = 0; j < col2; j++) { 36 | cin >> B[i][j]; 37 | } 38 | } 39 | 40 | if (col1 != row2) { 41 | cout << "Matrix multiplication not possible" << endl; 42 | return 0; 43 | } 44 | 45 | int C[row1][col2]; // Resultant matrix 46 | 47 | // Multiplying matrices A and B 48 | for (int i = 0; i < row1; i++) { 49 | for (int j = 0; j < col2; j++) { 50 | C[i][j] = 0; 51 | for (int k = 0; k < col1; k++) { 52 | C[i][j] += A[i][k] * B[k][j]; 53 | } 54 | } 55 | } 56 | 57 | // Displaying the resultant matrix C 58 | cout << "Resultant matrix C:" << endl; 59 | for (int i = 0; i < row1; i++) { 60 | for (int j = 0; j < col2; j++) { 61 | cout << C[i][j] << " "; 62 | } 63 | cout << endl; 64 | } 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Searching/binarySearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int binarySearch(int arr[], int n, int value) { 5 | 6 | int low = 0, high = n-1; 7 | while(low value) { 15 | high = mid - 1; 16 | } 17 | 18 | if(value > arr[mid]) { 19 | low = mid + 1; 20 | } 21 | } 22 | 23 | return -1; 24 | } 25 | 26 | void bubbleSort(int arr[], int n) { 27 | 28 | bool swapped; 29 | for(int i=0; i arr[j+1]) { 33 | swap(arr[j], arr[j+1]); 34 | swapped = true; 35 | } 36 | } 37 | if(swapped == false) 38 | break; 39 | } 40 | } 41 | 42 | void insertElements(int arr[], int n) { 43 | 44 | for(int i=0; i> arr[i]; 46 | } 47 | } 48 | 49 | void displayArray(int arr[], int n) { 50 | 51 | for(int i=0; i> n; 62 | 63 | int arr[n]; 64 | cout<< "Enter array elements: "; 65 | insertElements(arr, n); 66 | 67 | int value; 68 | cout<< "Enter a value to search: "; 69 | cin>> value; 70 | 71 | bubbleSort(arr, n); 72 | cout<< "Sorted array: "; 73 | displayArray(arr, n); 74 | 75 | int foundIndex = binarySearch(arr, n, value); 76 | if(foundIndex == -1) { 77 | cout<< "Element not found!"<< endl; 78 | } else { 79 | cout<< "Element found at index "<< foundIndex<< "!"<< endl; 80 | } 81 | 82 | return 0; 83 | } -------------------------------------------------------------------------------- /Greedy/fractionalKnapsack.cpp: -------------------------------------------------------------------------------- 1 | // If a set of items are given, each with a weight and a value, 2 | // the goal is to select a subset of the items that maximises the value while keeping the total weight below or equal to a given limit. 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | #define vi vector 11 | #define pii pair 12 | #define vii vector 13 | #define rep(i,a,b) for(int i=a; i r2; 31 | } 32 | 33 | int main() 34 | { 35 | int n; 36 | cin>>n; 37 | vii v(n); 38 | 39 | rep(i, 0, v.size()) { 40 | cin>> v[i].ff; 41 | cin>> v[i].ss; 42 | } 43 | 44 | int w; 45 | cin>> w; 46 | 47 | sort(v.begin(), v.end(), compare); 48 | 49 | cout<< "Sorted (value/weight): "<< endl; 50 | rep(i, 0, v.size()) { 51 | cout<< v[i].ff<< " "<< v[i].ss<< endl; 52 | } 53 | cout<< endl; 54 | 55 | int curr_cap = w, res = 0; 56 | 57 | rep(i, 0, v.size()) 58 | { 59 | if(v[i].ss <= curr_cap) { 60 | res += v[i].ff; 61 | curr_cap -= v[i].ss; 62 | continue; 63 | } 64 | double vw = (double) v[i].ff / v[i].ss; 65 | res += vw * curr_cap; 66 | break; 67 | } 68 | 69 | cout<< "Result: "<< res<< endl; 70 | 71 | return 0; 72 | } -------------------------------------------------------------------------------- /Greedy/countingMoney.cpp: -------------------------------------------------------------------------------- 1 | // You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. 2 | // Return the fewest number of coins that you need to make up that amount. 3 | // If that amount of money cannot be made up by any combination of the coins, return -1. 4 | // You may assume that you have an infinite number of each kind of coin. 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | #define vi vector 13 | #define pii pair 14 | #define vii vector 15 | #define rep(i,a,b) for(int i=a; i> n; 32 | vi coins(n); 33 | 34 | rep(i, 0, coins.size()) 35 | { 36 | cin>> coins[i]; 37 | } 38 | int amount; 39 | cout<< "Enter amount: "; 40 | cin>> amount; 41 | 42 | sort(coins.begin(), coins.end(), greater()); 43 | 44 | cout<< "Sorted coins: "; 45 | rep(i, 0, coins.size()) 46 | { 47 | cout<< coins[i]<< " "; 48 | } 49 | cout<< endl; 50 | 51 | vi ans; 52 | rep(i, 0, coins.size()) 53 | { 54 | while(amount >= coins[i]) { 55 | amount -= coins[i]; 56 | ans.pb(coins[i]); 57 | } 58 | } 59 | 60 | cout<< "Minimum Coins needed: "; 61 | if(ans.size() != 0 && amount == 0) { 62 | rep(i, 0, ans.size()) 63 | { 64 | cout<< ans[i]<< " "; 65 | } 66 | cout<< endl; 67 | } else { 68 | cout<< -1<< endl; 69 | } 70 | 71 | return 0; 72 | } -------------------------------------------------------------------------------- /Greedy/activitySelection.cpp: -------------------------------------------------------------------------------- 1 | // select the maximum number of activities that can be performed by a single person or machine, 2 | // assuming that a person can only work on a single activity at a time. 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | #define vi vector 11 | #define pii pair 12 | #define vii vector 13 | #define rep(i,a,b) for(int i=a; i=b; i--) 15 | #define for_each(x,y) for(auto &x : y) 16 | #define pb push_back 17 | #define ff first 18 | #define ss second 19 | 20 | // steps 21 | // 1. Sort the activities according to their finishing time 22 | // 2. Select the first activity from the sorted array and print it 23 | // 3. Do the following for the remaining activities in the sorted array 24 | // -> If the start time of this activity is greater than or equal to the finish time of the previously selected activity 25 | // -> then select this activity and print it 26 | 27 | struct Activity 28 | { 29 | char id; 30 | int start; 31 | int end; 32 | }; 33 | 34 | bool compare(Activity a, Activity b) 35 | { 36 | return a.end < b.end; 37 | } 38 | 39 | int main() 40 | { 41 | int n; 42 | cin>> n; 43 | vector activities(n); 44 | 45 | rep(i,0,activities.size()) 46 | { 47 | cin>> activities[i].id; 48 | cin>> activities[i].start; 49 | cin>> activities[i].end; 50 | } 51 | 52 | sort(activities.begin(), activities.end(), compare); 53 | 54 | cout<< "Sorted activies according to finish time: \n"; 55 | rep(i,0,activities.size()) 56 | { 57 | cout<= prev_end) { 68 | cout<< activity.id<< " "; 69 | prev_end = activity.end; 70 | } 71 | } 72 | cout<< endl; 73 | 74 | return 0; 75 | } -------------------------------------------------------------------------------- /Sorting/countingSort.cpp: -------------------------------------------------------------------------------- 1 | // counting sort 2 | // non comparison-based sorting algorithm 3 | // works well when there is limited number of input values 4 | // efficient when the range of input values is small compared to the number of elements to be sorted 5 | 6 | // steps -> 7 | // step 1 : find out the maximum element from given array 8 | // step 2 : initialize a count array of length max+1 with all elements as 0 9 | // step 3 : store the count of each unique element of the input array at their respective indices in count array 10 | // step 4 : store the commulative sum of the elements of the count array 11 | // step 5 : iterate from the end of input array. update output array 12 | // outputArray[countArray[inputArray[i]]-1] = inputArray[i] 13 | // update count array 14 | // countArray[inputArray[i]] = countArray[inputArray[i]] - 1 15 | 16 | #include 17 | using namespace std; 18 | 19 | void inputArray(int arr[], int n) { 20 | for(int i=0; i> arr[i]; 22 | } 23 | } 24 | 25 | void inputArray(int arr[], int n, int value) { 26 | for(int i=0; i maxValue) { 42 | maxValue = arr[i]; 43 | } 44 | } 45 | 46 | return maxValue; 47 | } 48 | 49 | int* countingSort(int arr[], int n, int maxValue) { 50 | int* sortedArray = new int[n]; 51 | int countArray[maxValue+1]; 52 | inputArray(countArray, maxValue+1, 0); 53 | 54 | for(int i=0; i=0; i--) { 63 | sortedArray[countArray[arr[i]]-1] = arr[i]; 64 | countArray[arr[i]] = countArray[arr[i]] - 1; 65 | } 66 | 67 | return sortedArray; 68 | } 69 | 70 | int main() { 71 | 72 | int n; 73 | cout<< "Please enter array size: "; 74 | cin>> n; 75 | 76 | int A[n]; 77 | cout<< "Please input array elements: "; 78 | inputArray(A, n); 79 | 80 | cout<< "Input array: "; 81 | printArray(A, n); 82 | 83 | int max = findMax(A, n); 84 | 85 | int* sortedArray = countingSort(A, n, max); 86 | cout<< "Sorted array: "; 87 | printArray(sortedArray, n); 88 | 89 | return 0; 90 | } -------------------------------------------------------------------------------- /Greedy/jobSequencing.cpp: -------------------------------------------------------------------------------- 1 | // Given an array of jobs where every job has a deadline and associated profit 2 | // if the job is finished before the deadline. 3 | // It is also given that every job takes a single unit of time, 4 | // so the minimum possible deadline for any job is 1. 5 | // Maximize the total profit if only one job can be scheduled at a time. 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | #define vi vector 14 | #define pii pair 15 | #define vii vector 16 | #define rep(i,a,b) for(int i=a; i=b; i--) 18 | #define pb push_back 19 | #define ff first 20 | #define ss second 21 | 22 | // Steps 23 | // 1. Sort all jobs in decreasing order of profit. 24 | // 2. Iterate on jobs in decreasing order of profit.For each job , do the following : 25 | // -> Find a time slot i, such that slot is empty and i < deadline and i is greatest. 26 | // Put the job in this slot and mark this slot filled. 27 | // -> If no such i exists, then ignore the job. 28 | 29 | struct Job 30 | { 31 | char id; 32 | int deadline; 33 | int profit; 34 | }; 35 | 36 | bool compare(Job j1, Job j2) 37 | { 38 | return j1.profit > j2.profit; 39 | } 40 | 41 | int main() 42 | { 43 | cout<< "Input jobs: "; 44 | int n; cin>> n; 45 | vector jobs(n); 46 | 47 | rep(i, 0, jobs.size()) 48 | { 49 | cin>> jobs[i].id; 50 | cin>> jobs[i].deadline; 51 | cin>> jobs[i].profit; 52 | } 53 | 54 | sort(jobs.begin(), jobs.end(), compare); 55 | 56 | cout<< "Sorted jobs (decreasing order by profit)\n"; 57 | rep(i, 0, jobs.size()) 58 | { 59 | cout<< jobs[i].id<< " "<< jobs[i].deadline<< " "<< jobs[i].profit<< endl; 60 | } 61 | 62 | int maxDeadLine=0; 63 | rep(i, 0, jobs.size()) 64 | { 65 | maxDeadLine = max(maxDeadLine, jobs[i].deadline); 66 | } 67 | cout<< "Maximum deadline: "<< maxDeadLine<< endl; 68 | 69 | vector slot(maxDeadLine, 0); 70 | vector schedule; 71 | 72 | rep(i, 0, jobs.size()) 73 | { 74 | rrep(j, jobs[i].deadline-1, 0) 75 | { 76 | if(slot[j]==false) { 77 | slot[j] = true; 78 | schedule.pb(jobs[i]); 79 | break; 80 | } 81 | } 82 | } 83 | 84 | cout<< "Scheduled jobs: "<< endl; 85 | // rep(i,0,schedule.size()) 86 | // { 87 | // cout<< schedule[i].id<< " "; 88 | // } 89 | for(auto &s : schedule) { 90 | cout<< s.id<< " "; 91 | } 92 | cout<< endl; 93 | 94 | 95 | return 0; 96 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Algorithms-CPP 3 | 4 | This repository contains C++ implementations of various algorithms. It serves as a collection of algorithms I've learned, through my university coursework and from additional resources. 5 | 6 | ## Table of Contents 7 | 8 | - [Installation](#installation) 9 | - [Implemented Algorithms](#implemented-algorithms) 10 | - [Contributing](#contributing) 11 | - [Contact Information](#contact-information) 12 | - [Authors](#authors) 13 | 14 | --- 15 | ## Installation 16 | 17 | To use the C++ algorithms in this repository, follow these steps: 18 | 19 | 1. **Clone the Repository:** 20 | - Clone this repository to your local machine using Git: 21 | 22 | ```shell 23 | git clone https://github.com/WasikAhmed/Algorithms-CPP.git 24 | ``` 25 | 2. **Compile and Run Algorithms:** 26 | - Navigate to the directory where you cloned the repository: 27 | 28 | ```bash 29 | cd Algorithms-CPP 30 | ``` 31 | - Compile and run the C++ algorithms as needed. For example: 32 | 33 | ```bash 34 | cd Sorting 35 | g++ countingSort.cpp -o countingSort 36 | ./countingSort 37 | ``` 38 | 39 | ## Implemented Algorithms 40 | 41 | This repository contains the following algorithms: 42 | 43 | **Searching** 44 | 45 | - [Binary Search](https://github.com/WasikAhmed/Algorithms-CPP/blob/main/Searching/binarySearch.cpp) 46 | 47 | 48 | - [Linear Search](https://github.com/WasikAhmed/Algorithms-CPP/blob/main/Searching/linearSearch.cpp) 49 | 50 | **Sorting** 51 | 52 | - [Bubble Sort](https://github.com/WasikAhmed/Algorithms-CPP/blob/main/Sorting/bubbleSort.cpp) 53 | 54 | - [Counting Sort](https://github.com/WasikAhmed/Algorithms-CPP/blob/main/Sorting/countingSort.cpp) 55 | 56 | - [Insertion Sort](https://github.com/WasikAhmed/Algorithms-CPP/blob/main/Sorting/insertionSort.cpp) 57 | 58 | - [Merge Sort](https://github.com/WasikAhmed/Algorithms-CPP/blob/main/Sorting/mergeSort.cpp) 59 | 60 | - [Quick Sort](https://github.com/WasikAhmed/Algorithms-CPP/blob/main/Sorting/quickSort.cpp) 61 | 62 | - [Selection Sort](https://github.com/WasikAhmed/Algorithms-CPP/blob/main/Sorting/selectionSort.cpp) 63 | 64 | ## Contributing 65 | 66 | Contributions are always welcome! If you have additional algorithms to implement, improvements, or bug fixes, please follow these guidelines: 67 | 68 | 1. Fork this repository. 69 | 2. Create a new branch for your feature or fix. 70 | 3. Implement your changes. 71 | 4. Test thoroughly. 72 | 5. Submit a pull request. 73 | 74 | 75 | 76 | 77 | ## Contact Information 78 | 79 | If you have any questions, suggestions, or feedback feel free to reach out: 80 | 81 | - Email: 22-47698-2@student.aiub.edu 82 | 83 | 84 | ## Authors 85 | 86 | - [@WasikAhmed](https://github.com/WasikAhmed) 87 | 88 | --------------------------------------------------------------------------------