├── .gitignore ├── .history └── array │ └── Addition_of_two_matrix │ ├── addition_of_two_matrix_20211010150211.cpp │ └── addition_of_two_matrix_20211010150216.cpp ├── Array ├── FACTORIAL OF LARGE NUM │ └── Python Solution.py ├── Factorial of Large Number │ ├── C++ │ │ └── solution.cpp │ └── JAVA │ │ └── solution.java ├── Kth Factor of N │ ├── C++ │ │ └── solution.cpp │ └── Java │ │ ├── solution.class │ │ └── solution.java ├── Kth Largest Element in Array │ └── C++ │ │ └── solution.cpp ├── Maximum Subarray │ └── C++ │ │ └── solution.cpp ├── Merge to Array │ └── C++ │ │ └── solution.cpp ├── Merge-2-array │ └── C │ │ └── solution.c ├── Reverse Array │ ├── C++ │ │ └── solution.cpp │ ├── C │ │ └── solution.c │ └── Java │ │ └── solution.java ├── Second Largest Element in array │ ├── C++ │ │ └── solution.cpp │ └── C │ │ └── solution.c ├── Shuffle the array │ └── C++ │ │ └── solution.cpp ├── Smallest Missing Number │ └── smallest_missing.cpp ├── Sort Array By Parity │ └── C++ │ │ └── solution.cpp └── reverse an array │ └── C │ └── Solution.c.c ├── Contributors.md ├── General Solution ├── First Unique Character │ └── C │ │ └── solution.c ├── Forgotten Language │ └── C++ │ │ └── solution.cpp ├── Lemonade Change │ └── C++ │ │ └── solution.cpp ├── Longest Common Prefix │ └── C++ │ │ └── solution.cpp ├── Palindrome │ ├── C++ │ │ └── solution.cpp │ ├── C │ │ └── solution.c │ └── Java │ │ └── solution.java ├── Roman To Integer │ ├── C++ │ │ └── solution.cpp │ ├── C │ │ └── solution.c │ ├── Java │ │ └── solution.java │ └── JavaScript │ │ └── solution.js ├── Sliding Window Maximum │ ├── C++ │ │ └── solution.cpp │ └── Java │ │ └── Solution.java └── Subtract Sum from Product │ ├── C++ │ └── solution.cpp │ └── C │ └── solution.c ├── Knapsack └── Fractionalknapsack.c ├── Kruskal's Algorithm └── Kruskal's algorithm.c ├── LICENSE ├── README.md ├── Searching & Sorting ├── Binary Search │ └── Java │ │ └── BinarySearch.java ├── Bubble Sort │ └── Python │ │ └── solution.py ├── Insertion Sort │ ├── C │ │ └── solution.c │ └── Python │ │ └── solution.py ├── Linear search │ └── C │ │ └── solution.c └── Quick Sort │ ├── C++ │ ├── .DS_Store │ └── solution.cpp │ └── C │ └── solution.c ├── Searching and Sorting ├── Binary Search.cpp ├── Bubble Sort │ └── JavaScript │ │ └── bubble_sort.js ├── Insertion Sort │ └── CPP │ │ └── solution.cpp ├── Recursive_bubblesort.cpp ├── bubble_sort.java └── java │ └── HeapSort.java ├── Searching └── linear search │ ├── CPP │ ├── BinarySearch.cpp │ ├── LinearSearch.cpp │ └── solution.cpp │ └── java │ └── LinearSearch.java ├── array ├── Addition_of_two_matrix │ └── addition_of_two_matrix.cpp ├── Factorial_of_large_number │ └── C │ │ └── C_factorial_of_large_number.c ├── Good Pairs │ └── c++ │ │ └── Solution.cpp ├── Kth Largest Element in Array │ ├── Java │ │ └── kth_largest_element.java │ └── java │ │ └── solution.java ├── Leader_in_an_array │ └── leader_in_an_array.cpp ├── LemonadeChange │ └── Lemonade_Change.java ├── Merge Array │ └── array.c ├── Merge-2-array │ └── java │ │ └── Merge2array.java ├── Missing Number │ └── c++ │ │ └── Solution.cpp ├── Sort Array By Parity │ └── java │ │ └── sortArrayByParity.java ├── maximum subarray │ └── cpp │ │ └── solution.cpp └── reverse an array │ └── java │ └── reverse.java ├── maths ├── StandardDeviation.cpp └── digits.c └── sorting ├── bubble_sort.cpp ├── comb sort └── comb_sort.cpp ├── insertion sort └── c │ └── insertionsort.cpp ├── mergeSort.cpp ├── radix_sort.cpp └── selection_sort.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /.history/array/Addition_of_two_matrix/addition_of_two_matrix_20211010150211.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HacktoberfestSIT/Problem-Solving/dea62ef75e414fd4e0a354acbc68a704a77fd8e1/.history/array/Addition_of_two_matrix/addition_of_two_matrix_20211010150211.cpp -------------------------------------------------------------------------------- /.history/array/Addition_of_two_matrix/addition_of_two_matrix_20211010150216.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int r, c, a[100][100], b[100][100], sum[100][100], i, j; 4 | printf("Enter the number of rows (between 1 and 100): "); 5 | scanf("%d", &r); 6 | printf("Enter the number of columns (between 1 and 100): "); 7 | scanf("%d", &c); 8 | 9 | printf("\nEnter elements of 1st matrix:\n"); 10 | for (i = 0; i < r; ++i) 11 | for (j = 0; j < c; ++j) { 12 | printf("Enter element a%d%d: ", i + 1, j + 1); 13 | scanf("%d", &a[i][j]); 14 | } 15 | 16 | printf("Enter elements of 2nd matrix:\n"); 17 | for (i = 0; i < r; ++i) 18 | for (j = 0; j < c; ++j) { 19 | printf("Enter element b%d%d: ", i + 1, j + 1); 20 | scanf("%d", &b[i][j]); 21 | } 22 | 23 | // adding two matrices 24 | for (i = 0; i < r; ++i) 25 | for (j = 0; j < c; ++j) { 26 | sum[i][j] = a[i][j] + b[i][j]; 27 | } 28 | 29 | // printing the result 30 | printf("\nSum of two matrices: \n"); 31 | for (i = 0; i < r; ++i) 32 | for (j = 0; j < c; ++j) { 33 | printf("%d ", sum[i][j]); 34 | if (j == c - 1) { 35 | printf("\n\n"); 36 | } 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Array/FACTORIAL OF LARGE NUM/Python Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def factorial(self, N): 3 | fact = 1 4 | while(N>=1): 5 | fact = fact * N 6 | N = N-1 7 | 8 | return str(fact) 9 | 10 | 11 | if __name__ == '__main__': 12 | t=int(input()) 13 | for _ in range(t): 14 | N = int(input()) 15 | ob = Solution() 16 | ans = ob.factorial(N) 17 | for i in ans: 18 | print(i,end="") 19 | print() 20 | -------------------------------------------------------------------------------- /Array/Factorial of Large Number/C++/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ln "\n" 3 | #define ll long long int 4 | #define vll vector 5 | using namespace std; 6 | 7 | void solve() 8 | { 9 | 10 | ll n; 11 | cin >> n; // for which number you want the factorial 12 | vll v; //declaring a vector to store a number in reverse order 13 | v.push_back(1); 14 | for (ll i = 2; i <= n; i++) 15 | { 16 | ll carry = 0; 17 | for (ll j = 0; j < v.size(); j++) 18 | { 19 | ll temp = v[j] * i + carry; 20 | v[j] = temp % 10; 21 | carry = temp / 10; 22 | } 23 | // push carry to end of the vector 24 | while (carry) 25 | { 26 | v.push_back(carry % 10); 27 | carry /= 10; 28 | } 29 | } 30 | 31 | // as the number are stored in end to begin, we have to print in this manner 32 | for (auto i = v.rbegin(); i != v.rend(); i++) 33 | { 34 | cout <<*i; 35 | } 36 | cout << ln; 37 | // v.clear(); 38 | 39 | } 40 | 41 | int main() 42 | { 43 | std::ios::sync_with_stdio(false); 44 | ll T =1; 45 | 46 | // cin >> T; // N test cases remove comment 47 | 48 | while (T--) 49 | { 50 | solve(); 51 | } 52 | return 0; 53 | } -------------------------------------------------------------------------------- /Array/Factorial of Large Number/JAVA/solution.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | class LF 5 | { 6 | public static void main(String arg[]) 7 | { 8 | BigInteger factorial=new BigInteger("1"); 9 | int num,i; 10 | Scanner sc=new Scanner(System.in); 11 | System.out.println("Enter a number:"); 12 | num=sc.nextInt(); 13 | for(i=2;i<=num;++i) 14 | { 15 | factorial=factorial.multiply(BigInteger.valueOf(i)); 16 | } 17 | System.out.println(factorial); 18 | } 19 | } -------------------------------------------------------------------------------- /Array/Kth Factor of N/C++/solution.cpp: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/the-kth-factor-of-n/ 2 | //MY SUBMISSION LINK --> https://leetcode.com/submissions/detail/564495189/ 3 | //100% correct 4 | /*Given two positive integers n and k. 5 | A factor of an integer n is defined as an integer i where n % i == 0. 6 | Consider a list of all factors of n sorted in ascending order, return the kth factor in this list or return -1 if n has less than k factors. 7 | Example 1: 8 | Input: n = 12, k = 3 9 | Output: 3 10 | Explanation: Factors list is [1, 2, 3, 4, 6, 12], the 3rd factor is 3. 11 | Example 2: 12 | Input: n = 7, k = 2 13 | Output: 7 14 | Explanation: Factors list is [1, 7], the 2nd factor is 7. 15 | Constraints: 16 | 17 | 1 <= k <= n <= 1000 */ 18 | 19 | 20 | class Solution { 21 | public: 22 | int kthFactor(int n, int k) { 23 | vector factors; // vector to take input factors 24 | for (int i = 1; i <= n; i++) { 25 | if (n % i == 0) { 26 | factors.push_back(i);// Calculating factors and inserting 27 | } 28 | } 29 | if (factors.size() < k) 30 | { return -1;} 31 | else { 32 | sort(begin(factors), end(factors)); 33 | // sort in ascending order 34 | return factors[k - 1]; 35 | } 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /Array/Kth Factor of N/Java/solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HacktoberfestSIT/Problem-Solving/dea62ef75e414fd4e0a354acbc68a704a77fd8e1/Array/Kth Factor of N/Java/solution.class -------------------------------------------------------------------------------- /Array/Kth Factor of N/Java/solution.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.*; 3 | 4 | class Kth_factor_of_N_solution { 5 | 6 | static int kthFactor(int n, int k) { 7 | int x = 0; 8 | int f[] = new int[n]; 9 | 10 | for (int i = 1; i <= n; i++) { 11 | if (n % i == 0) { 12 | f[x] = i; 13 | x++; 14 | } 15 | } 16 | 17 | if (k > x) { 18 | return -1; 19 | } else { 20 | return f[k - 1]; 21 | } 22 | 23 | } 24 | 25 | public static void main(String[] args) { 26 | 27 | Scanner sc = new Scanner(System.in); 28 | int n, k; 29 | n = sc.nextInt(); 30 | k = sc.nextInt(); 31 | 32 | System.out.println(kthFactor(n,k)); 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /Array/Kth Largest Element in Array/C++/solution.cpp: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/kth-largest-element-in-an-array/ 2 | //MY SUBMISSION LINK --> https://leetcode.com/submissions/detail/564388931/ 3 | // 100% correct 4 | /* 5 | Given an integer array nums and an integer k, return the kth largest element in the array. 6 | 7 | Note that it is the kth largest element in the sorted order, not the kth distinct element. 8 | 9 | Example 1: 10 | Input: nums = [3,2,1,5,6,4], k = 2 11 | Output: 5 12 | 13 | Example 2: 14 | Input: nums = [3,2,3,1,2,4,5,5,6], k = 4 15 | Output: 4 16 | 17 | 18 | Constraints 19 | 1 <= k <= nums.length <= 104 20 | -104 <= nums[i] <= 104 21 | 22 | */ 23 | 24 | class Solution { 25 | public: 26 | int findKthLargest(vector& nums, int k) { 27 | sort(nums.begin() , nums.end()); 28 | return nums[(int)nums.size()-k]; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Array/Maximum Subarray/C++/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 53. Maximum Subarray 3 | 4 | Given an integer array nums, find the contiguous subarray 5 | (containing at least one number) which has the largest sum and return its sum. 6 | A subarray is a contiguous part of an array. 7 | 8 | Example 1: 9 | Input: nums = [-2,1,-3,4,-1,2,1,-5,4] 10 | Output: 6 11 | Explanation: [4,-1,2,1] has the largest sum = 6. 12 | */ 13 | 14 | class Solution { 15 | public: 16 | int maxSubArray(vector& nums) { 17 | int cs=0; 18 | int largest=INT_MIN; 19 | 20 | for(int i=0;i 2 | using namespace std; 3 | 4 | struct Array 5 | { 6 | int A[10]; 7 | int size; 8 | int length; 9 | }; 10 | void display(struct Array *arr3) 11 | { 12 | for (int i = 0; i < (*arr3).length; i++) 13 | { 14 | cout << (*arr3).A[i] << " "; 15 | } 16 | } 17 | 18 | void merging(struct Array *arr1, struct Array *arr2, struct Array *arr3) 19 | { 20 | 21 | int i = 0, j = 0, k = 0; 22 | while (i < (*arr1).length && j < (*arr2).length) 23 | { 24 | if ((*arr1).A[i] < (*arr2).A[j]) 25 | { 26 | (*arr3).A[k] = (*arr1).A[i]; 27 | i++; 28 | k++; 29 | } 30 | else 31 | { 32 | (*arr3).A[k] = (*arr2).A[j]; 33 | j++; 34 | k++; 35 | } 36 | } 37 | 38 | for (; i < (*arr1).length; i++) 39 | { 40 | (*arr3).A[k] = (*arr1).A[i]; 41 | k++; 42 | } 43 | for (; j < (*arr2).length; j++) 44 | { 45 | (*arr3).A[k] = (*arr2).A[j]; 46 | k++; 47 | } 48 | (*arr3).length = (*arr1).length + (*arr2).length; 49 | } 50 | 51 | int main() 52 | { 53 | ios_base::sync_with_stdio(false); 54 | cin.tie(NULL); 55 | struct Array arr1 = {{3, 6, 9, 12,14}, 10, 5}; 56 | struct Array arr2 = {{4, 8, 12, 16,17}, 10, 5}; 57 | struct Array arr3 = {{0}, 30, 30}; 58 | merging(&arr1, &arr2, &arr3); 59 | display(&arr3); 60 | return 0; 61 | } -------------------------------------------------------------------------------- /Array/Merge-2-array/C/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int i,j, a[5], b[5], c[10]; 7 | 8 | printf("Enter the elements for First Array:\n"); 9 | for(i=0; i<5; i++) 10 | { 11 | scanf("%d",&a[i]); 12 | } 13 | 14 | printf("Enter the elements for Second Array:\n"); 15 | for(i=0; i<5; i++) 16 | { 17 | scanf("%d",&b[i]); 18 | } 19 | 20 | for(i=0; i<5; i++) 21 | { 22 | c[i] = a[i]; 23 | } 24 | 25 | for(i=0, j=5; j<10 && i<5; i++, j++) 26 | { 27 | c[j] = b[i]; 28 | } 29 | 30 | printf("Merged Array:\n"); 31 | for(i=0; i<10; i++) 32 | { 33 | printf("%d ",c[i]); 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Array/Reverse Array/C++/solution.cpp: -------------------------------------------------------------------------------- 1 | //Reverse an Array 2 | #include 3 | 4 | using namespace std; 5 | 6 | void print(int a[], int n) 7 | { 8 | for (int i = 0; i < n; i++) 9 | { 10 | cout << a[i] << " "; 11 | } 12 | cout << endl; 13 | } 14 | 15 | void swap(int *a, int *b) 16 | { 17 | int t = *a; 18 | *a = *b; 19 | *b = t; 20 | } 21 | 22 | void reverse(int a[], int n) 23 | { 24 | for (int i = 0; i < (n / 2); i++) 25 | { 26 | swap(a[i], a[n - 1 - i]); 27 | } 28 | } 29 | 30 | int main() 31 | { 32 | int a[5]; 33 | for (int i = 0; i < 5; i++) 34 | { 35 | cout << "Enter array element : "; 36 | cin >> a[i]; 37 | } 38 | cout << "Original Array : "; 39 | print(a, 5); 40 | reverse(a, 5); 41 | cout << "Reversed Array : "; 42 | print(a, 5); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Array/Reverse Array/C/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void reverse(int *a,int n){ 4 | for(int i=0;i maxSlidingWindow(vector& nums, int k) { 4 | dequedq; 5 | vectorres; 6 | int i; 7 | int n=nums.size(); 8 | for(i=0;i Remove from rear 11 | dq.push_back(i); // Add new element at rear of queue 12 | } 13 | for( ;i Remove from rear 23 | 24 | 25 | dq.push_back(i); // Add current element at the rear of dq 26 | 27 | 28 | } 29 | res.push_back(nums[dq.front()]); // Push the maximum element of last window 30 | 31 | return res; 32 | 33 | } 34 | }; 35 | 36 | //Note: int main is not given in this code 37 | -------------------------------------------------------------------------------- /Array/Second Largest Element in array/C/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int array[5]; 7 | printf("Enter the 5 elements: "); 8 | for(int i=0; i<5; i++){ 9 | scanf("%d",&array[i]); 10 | } 11 | int max = INT_MIN; 12 | int secondmax = INT_MIN; 13 | for(int i=0; i<5; i++){ 14 | if(array[i]>max){ 15 | secondmax = max; 16 | max = array[i]; 17 | } 18 | else if(array[i]secondmax){ 19 | secondmax = array[i]; 20 | } 21 | } 22 | 23 | printf("Second largest element is %d",secondmax); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Array/Shuffle the array/C++/solution.cpp: -------------------------------------------------------------------------------- 1 | vector shuffle(vector& nums, int n){ 2 | vector ans; 3 | for(int i = 0; i < n; ++i) 4 | { 5 | ans.push_back(nums[i]); 6 | ans.push_back(nums[n+i]); 7 | } 8 | return ans; 9 | } 10 | 11 | //Note: int main is not given in this code -------------------------------------------------------------------------------- /Array/Smallest Missing Number/smallest_missing.cpp: -------------------------------------------------------------------------------- 1 | //C++ program for the above approach 2 | #include 3 | 4 | using namespace std; 5 | 6 | // Program to find missing element 7 | int findFirstMissing(vector arr , int start , 8 | int end,int first) 9 | { 10 | 11 | if (start < end) 12 | { 13 | int mid = (start + end) / 2; 14 | 15 | /** Index matches with value 16 | at that index, means missing 17 | element cannot be upto that po*/ 18 | if (arr[mid] != mid+first) 19 | return findFirstMissing(arr, start, 20 | mid , first); 21 | else 22 | return findFirstMissing(arr, mid + 1, 23 | end , first); 24 | } 25 | return start + first; 26 | 27 | } 28 | 29 | // Program to find Smallest 30 | // Missing in Sorted Array 31 | int findSmallestMissinginSortedArray(vector arr) 32 | { 33 | 34 | // Check if 0 is missing 35 | // in the array 36 | if(arr[0] != 0) 37 | return 0; 38 | 39 | // Check is all numbers 0 to n - 1 40 | // are present in array 41 | if(arr[arr.size() - 1] == arr.size() - 1) 42 | return arr.size(); 43 | 44 | int first = arr[0]; 45 | 46 | return findFirstMissing(arr, 0, arr.size() - 1, first); 47 | } 48 | 49 | 50 | // Driver program to test the above function 51 | int main() 52 | { 53 | vector arr = {0, 1, 2, 3, 4, 5, 7}; 54 | int n = arr.size(); 55 | 56 | // Function Call 57 | cout<<"First Missing element is : "< sortArrayByParity(vector &nums) 5 | { 6 | int start = 0, end = nums.size() - 1; 7 | while (start < end) 8 | { 9 | if (nums[start] % 2 > nums[end] % 2) 10 | { 11 | nums[start] = nums[start] + nums[end]; 12 | nums[end] = nums[start] - nums[end]; 13 | nums[start] = nums[start] - nums[end]; 14 | } 15 | 16 | if (nums[start] % 2 == 0) 17 | start++; 18 | if (nums[end] % 2 == 1) 19 | end--; 20 | } 21 | 22 | return nums; 23 | } 24 | }; 25 | 26 | //Note: int main is not given in this code. -------------------------------------------------------------------------------- /Array/reverse an array/C/Solution.c.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int i,j,a[5],t; 7 | float n=2.5; 8 | printf("Input 5 integers:\n"); 9 | for(i=0; i<5; i++) 10 | { 11 | scanf("%d",&a[i]); 12 | } 13 | 14 | for(i=0; i
10 | 11 | ## Repo Owner and Admins: 12 | 13 | 1. [Deep Dhar](https://github.com/deepdhar) 14 | 2. [Aman Raza](https://github.com/aman-raza) 15 | 16 | ## General Contributors below: 17 | 18 | 1. [Ayra](https://github.com/BonkReaction) 19 | 2. [Gourav-78](https://github.com/Gourav-78) (Solved Issues #8,#18,#20,#21,#23,#24) 20 | 3. [Anoushka-Ghosh](https://github.com/Anoushka-Ghosh) 21 | 4. [Shivanshi](https://github.com/shivanshi-s) 22 | 5. [iamprofessor1](https://github.com/iamprofessor1)(Added Kth largest leetcode in cpp) 23 | 6. [dcod3r](https://github.com/dcod3r) 24 | 7. [Ayansh](https://github.com/badasschef) 25 | 8. [Deep](https://github.com/deep846) 26 | 9. [Amit](https://github.com/amitShindeGit) 27 | 10. [dee-Rajak](https://github.com/dee-Rajak) (Solved Issue #81, #14) 28 | 11. [Tanisha-creator](https://github.com/Tanisha-creator)(Added solution for Maximum SubArray) 29 | 12. [Fatema](https://github.com/Fatema110Git) (Added Factorial of Large Number in Python) 30 | 13. [Saransh](https://github.com/saranshkotnala) 31 | 14. [Eldrin](https://github.com/eldrin0)(Added Bubble Sort solution in JS) 32 | 15. [amankumar988](https://github.com/amankumar988)(Solved Issues #18, #16, #20, #25) 33 | 16. [kesher988](https://github.com/kesher988)(solved issue #9,#24,#54,#29) 34 | -------------------------------------------------------------------------------- /General Solution/First Unique Character/C/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define NO_OF_CHARS 256 4 | 5 | int* getCharCountArray(char* str) 6 | { 7 | int* count = (int*)calloc(sizeof(int), NO_OF_CHARS); 8 | int i; 9 | for (i = 0; *(str + i); i++) 10 | count[*(str + i)]++; 11 | return count; 12 | } 13 | 14 | int firstNonRepeating(char* str) 15 | { 16 | int* count = getCharCountArray(str); 17 | int index = -1, i; 18 | 19 | for (i = 0; *(str + i); i++) { 20 | if (count[*(str + i)] == 1) { 21 | index = i; 22 | break; 23 | } 24 | } 25 | 26 | free(count); 27 | return index; 28 | } 29 | 30 | int main() 31 | { 32 | printf("Input a sentence:\n"); 33 | char str[50]; 34 | gets(str); 35 | int index = firstNonRepeating(str); 36 | if (index == -1) 37 | printf("Either all characters are repeating or " 38 | "string is empty"); 39 | else 40 | printf("First non-repeating character is %c", 41 | str[index]); 42 | getchar(); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /General Solution/Forgotten Language/C++/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void solve() 5 | { 6 | int n, k, l; 7 | vector < string > phrases[52]; 8 | string forgotten[102]; 9 | 10 | cin >> n >> k; 11 | for (int i = 0; i < n; i++) { 12 | cin >> forgotten[i]; 13 | } 14 | 15 | for (int i = 0; i < k; i++) { 16 | cin >> l; 17 | for (int j = 0; j < l; j++) { 18 | string x; 19 | cin >> x; 20 | phrases[i].push_back(x); 21 | } 22 | } 23 | 24 | for (int i = 0; i < n; i++){ 25 | string answer = "NO"; 26 | for(int j = 0; j < k; j++){ 27 | for(int p = 0; p < phrases[j].size(); p++){ 28 | if(phrases[j][p] == forgotten[i]) 29 | answer = "YES"; 30 | } 31 | } 32 | cout << answer << (i==n-1 ? "\n" : " "); 33 | } 34 | } 35 | 36 | int main() { 37 | int t; 38 | cin >> t; 39 | while(t--) 40 | solve(); 41 | 42 | return 0; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /General Solution/Lemonade Change/C++/solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool lemonadeChange(vector &bills) 5 | { 6 | int D5 = 0, D10 = 0, D20 = 0; 7 | for (auto i = bills.begin(); i != bills.end(); i++) 8 | { 9 | if (*i == 5) 10 | { 11 | D5++; 12 | } 13 | else if (*i == 10) 14 | { 15 | if (D5 == 0) 16 | return false; 17 | else 18 | D5--; 19 | D10++; 20 | } 21 | else if (*i == 20) 22 | { 23 | if (D10 > 0 && D5 > 0) 24 | { 25 | D10--; 26 | D5--; 27 | } 28 | else if (D5 >= 3) 29 | { 30 | D5 -= 3; 31 | } 32 | else 33 | return false; 34 | } 35 | } 36 | return true; 37 | } 38 | }; 39 | 40 | //Note: int main is not given in this code. -------------------------------------------------------------------------------- /General Solution/Longest Common Prefix/C++/solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string longestCommonPrefix(vector& strs) { 4 | string first = strs[0]; 5 | for (int i=1; i rev) 12 | { 13 | rev = rev * 10 + x % 10; 14 | x /= 10; 15 | } 16 | return x == rev || x == rev / 10; 17 | } 18 | }; 19 | 20 | //Note: int main is not given in this code. -------------------------------------------------------------------------------- /General Solution/Palindrome/C/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int pal=0,x,tem,ld; 7 | printf("X="); 8 | scanf("%d",&x); 9 | tem=x; 10 | while(x>00) 11 | { 12 | ld=x%10; 13 | pal=(pal*10)+ld; 14 | x=x/10; 15 | } 16 | if(pal==tem) 17 | printf("true"); 18 | else 19 | printf("false"); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /General Solution/Palindrome/Java/solution.java: -------------------------------------------------------------------------------- 1 | 2 | public class Palindrome_Number { 3 | 4 | public static void main(String[] args) { 5 | int n = 10; 6 | int x = n; 7 | int z = 0; 8 | if (n >= 0) { 9 | while (x > 0) { 10 | int lt = x % 10; 11 | z = z * 10 + lt; 12 | x = x / 10; 13 | } 14 | 15 | if (z == n) { 16 | System.out.println("true"); 17 | } else { 18 | System.out.println("false"); 19 | } 20 | } else { 21 | System.out.println("false"); 22 | } 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /General Solution/Roman To Integer/C++/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //function to convert Valid Roman to Integer 5 | int romanToInt(string s) 6 | { 7 | int val = 0; 8 | map Rvalues = 9 | {{'I', 1}, 10 | {'V', 5}, 11 | {'X', 10}, 12 | {'L', 50}, 13 | {'C', 100}, 14 | {'D', 500}, 15 | {'M', 1000}}; 16 | for (int i = 0; i < s.length(); i++) 17 | { 18 | if (Rvalues[s[i]] < Rvalues[s[i + 1]]) 19 | { 20 | val -= Rvalues[s[i]]; 21 | } 22 | else 23 | { 24 | val += Rvalues[s[i]]; 25 | } 26 | } 27 | return val; 28 | } 29 | 30 | int main() 31 | { 32 | string s; 33 | cin >> s; 34 | cout << romanToInt(s); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /General Solution/Roman To Integer/C/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int digit(char); 5 | int main(){ 6 | char num[1000]; 7 | int i=0; 8 | long int number =0; 9 | printf("s="); 10 | scanf("%s",num); 11 | while(num[i]){ 12 | if(digit(num[i]) >= digit(num[i+1])) 13 | number = number + digit(num[i]); 14 | else{ 15 | number = number + (digit(num[i+1]) - digit(num[i])); 16 | i++; 17 | } 18 | i++; 19 | } 20 | printf("%ld",number); 21 | return 0; 22 | } 23 | int digit(char c){ 24 | int value=0; 25 | switch(c){ 26 | case 'I': 27 | value = 1; 28 | break; 29 | case 'V': 30 | value = 5; 31 | break; 32 | case 'X': 33 | value = 10; 34 | break; 35 | case 'L': 36 | value = 50; 37 | break; 38 | case 'C': 39 | value = 100; 40 | break; 41 | case 'D': 42 | value = 500; 43 | break; 44 | case 'M': 45 | value = 1000; 46 | break; 47 | default: 48 | value = -1; 49 | } 50 | return value; 51 | } 52 | -------------------------------------------------------------------------------- /General Solution/Roman To Integer/Java/solution.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.*; 3 | class Roman_TO_Intiger 4 | { 5 | // int I =1, V =5, X= 10, L= 50, C= 100, D= 500, M= 1000; 6 | 7 | static int value(char i) 8 | { 9 | if (i == 'I') 10 | { 11 | return 1; 12 | } 13 | else if (i == 'V') 14 | { 15 | return 5; 16 | } 17 | else if (i == 'X') 18 | { 19 | return 10; 20 | } 21 | else if (i == 'L') 22 | { 23 | return 50; 24 | } 25 | else if (i == 'C') 26 | { 27 | return 100; 28 | } 29 | else if (i == 'D') 30 | { 31 | return 500; 32 | } 33 | else if (i == 'M') 34 | { 35 | return 1000; 36 | } 37 | 38 | return -1; 39 | } 40 | 41 | static int romanToIntiger(String roman) 42 | { 43 | int sum = 0, i = 0; 44 | 45 | for (i = 0; i < roman.length() - 1; i++) // X I I I 46 | { 47 | 48 | if (value(roman.charAt(i)) >= value(roman.charAt(i + 1))) 49 | { 50 | sum = sum + value(roman.charAt(i)); 51 | } 52 | else if (value(roman.charAt(i)) < value(roman.charAt(i + 1))) 53 | { 54 | sum = sum - value(roman.charAt(i)); 55 | } 56 | } 57 | sum = sum + value(roman.charAt(i)); 58 | return sum; 59 | } 60 | 61 | public 62 | static void main(String[] args) 63 | { 64 | String roman; 65 | System.out.println("Enter a roman Number"); 66 | // Scanner sc = new Scanner(System.in); 67 | // roman = sc.nextLine(); 68 | roman = "XCIII"; 69 | System.out.println("The converted of roman " + roman + " in Int is " + romanToIntiger(roman)); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /General Solution/Roman To Integer/JavaScript/solution.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /General Solution/Sliding Window Maximum/C++/solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector maxSlidingWindow(vector& nums, int k) { 4 | dequedq; 5 | vectorres; 6 | int i; 7 | int n=nums.size(); 8 | for(i=0;i Remove from rear 11 | dq.push_back(i); // Add new element at rear of queue 12 | } 13 | for( ;i Remove from rear 23 | 24 | 25 | dq.push_back(i); // Add current element at the rear of dq 26 | 27 | 28 | } 29 | res.push_back(nums[dq.front()]); // Push the maximum element of last window 30 | 31 | return res; 32 | 33 | } 34 | }; 35 | 36 | //Note: int main is not given in this code 37 | -------------------------------------------------------------------------------- /General Solution/Sliding Window Maximum/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | // Method to find the maximum for 4 | // each and every contiguous 5 | // subarray of size k. 6 | static void printKMax(int arr[], int n, int k) 7 | { 8 | int j, max; 9 | 10 | for (int i = 0; i <= n - k; i++) { 11 | 12 | max = arr[i]; 13 | 14 | for (j = 1; j < k; j++) { 15 | if (arr[i + j] > max) 16 | max = arr[i + j]; 17 | } 18 | System.out.print(max + " "); 19 | } 20 | } 21 | 22 | 23 | public static void main(String args[]) 24 | { 25 | int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 26 | int k = 3; 27 | printKMax(arr, arr.length, k); 28 | } 29 | } 30 | 31 | 32 | -------------------------------------------------------------------------------- /General Solution/Subtract Sum from Product/C++/solution.cpp: -------------------------------------------------------------------------------- 1 | //Subtract the sum of digits from product of digits of a number 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n; 9 | cout << "Enter a Numebr : "; 10 | cin >> n; 11 | int s = 0, p = 1, d; 12 | while (n > 0) 13 | { 14 | d = n % 10; 15 | s += d; 16 | p *= d; 17 | n /= 10; 18 | } 19 | cout << "Result : " << (p - s) << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /General Solution/Subtract Sum from Product/C/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int num,sum=0,sub=0,tem,ld,rem, prod = 1; 7 | printf("n="); 8 | scanf("%d",&num); 9 | tem=num; 10 | while(num != 0) 11 | { 12 | ld = num % 10; 13 | sum += ld; 14 | num /= 10; 15 | } 16 | while(tem != 0) 17 | { 18 | rem = tem % 10; 19 | prod *= rem; 20 | tem /= 10; 21 | } 22 | sub=prod-sum; 23 | printf("%d",sub); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Knapsack/Fractionalknapsack.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int n = 5; 4 | int c[10] = {12, 1, 2, 1, 4}; 5 | int v[10] = {4, 2, 2, 1, 10}; 6 | int W = 15; 7 | 8 | void simple_fill() { 9 | int cur_w; 10 | float tot_v; 11 | int i, maxi; 12 | int used[10]; 13 | 14 | for (i = 0; i < n; ++i) 15 | used[i] = 0; 16 | 17 | cur_w = W; 18 | while (cur_w > 0) { 19 | 20 | maxi = -1; 21 | for (i = 0; i < n; ++i) 22 | if ((used[i] == 0) && 23 | ((maxi == -1) || ((float)v[i]/c[i] > (float)v[maxi]/c[maxi]))) 24 | maxi = i; 25 | used[maxi] = 1; 26 | cur_w -= c[maxi]; 27 | tot_v += v[maxi]; 28 | if (cur_w >= 0) 29 | printf("Added object %d (%d$, %dKg) completely in the bag. Space left: %d.\n", maxi + 1, v[maxi], c[maxi], cur_w); 30 | else { 31 | printf("Added %d%% (%d$, %dKg) of object %d in the bag.\n", (int)((1 + (float)cur_w/c[maxi]) * 100), v[maxi], c[maxi], maxi + 1); 32 | tot_v -= v[maxi]; 33 | tot_v += (1 + (float)cur_w/c[maxi]) * v[maxi]; 34 | } 35 | } 36 | 37 | printf("Filled the bag with objects worth %.2f$.\n", tot_v); 38 | } 39 | 40 | int main(int argc, char *argv[]) { 41 | simple_fill(); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Kruskal's Algorithm/Kruskal's algorithm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int i,j,k,a,b,u,v,n,ne=1; 5 | int min,mincost=0,cost[9][9],parent[9]; 6 | int find(int); 7 | int uni(int,int); 8 | void main() 9 | { 10 | 11 | printf("\n\n\tImplementation of Kruskal's algorithm\n\n"); 12 | printf("\nEnter the no. of vertices\n"); 13 | scanf("%d",&n); 14 | printf("\nEnter the cost adjacency matrix\n"); 15 | for(i=1;i<=n;i++) 16 | { 17 | for(j=1;j<=n;j++) 18 | { 19 | scanf("%d",&cost[i][j]); 20 | if(cost[i][j]==0) 21 | cost[i][j]=999; 22 | } 23 | } 24 | printf("\nThe edges of Minimum Cost Spanning Tree are\n\n"); 25 | while(ne Cyclic Sort -> Python -> solution.py ) 18 | - Add your name to the __Contributors.md__ file 19 | 20 | ### Step Two 21 | - After succesfully following the above steps, open Git Bash on your PC/ Mac. 22 | - Create a new branch by your Github username and checkout the branch: ```$ git checkout -b "username"``` (branch name should be your GitHub username strictly, ___Always try to make changes on a new branch rather than directly on the master branch, which can corrupt the codebase while working on some high profile, complicated program___) 23 | - Add files changed: ```$ git add .``` 24 | - Commit the changes: ```$ git commit -m "commit message"``` (commit message should be meaningful and precise, example: __git commit -m "added searching solution in Java"__) 25 | - Push the changes to make a successful PR: ```$ git push origin your-branch-name``` 26 | - That's it 🎉💥 27 | 28 | ## 📌Programming Languages to use 29 | - C 30 | - C++ 31 | - Python 32 | - Java 33 | - Go 34 | - JavaScript 35 | - Kotlin 36 | - Rubi 37 | - Swift 38 | - Scala 39 | - Any other language 40 | 41 | ## 🎭Make a Meaningful PR 42 | - Don't make a spam PR, if found you will be disqualified from Hacktoberfest 43 | - Try to understand the problem discussed in the issue 44 | - Make PR which is thoughtful 45 | - Don't forget to write your name on the Contributors.md file 46 | 47 | ### Don't forget to Star ⭐ this repo 😁 48 | 49 |

50 | ___Owned and managed by [Deep Dhar](https://github.com/deepdhar), [Aman Raza](https://github.com/aman-raza), [Shreyansh Singh](https://github.com/dumbbutgenius), [Aniruddha Das](https://github.com/OctoplusNinja) ❤✨___ 51 | -------------------------------------------------------------------------------- /Searching & Sorting/Binary Search/Java/BinarySearch.java: -------------------------------------------------------------------------------- 1 | package Java; 2 | 3 | public class BinarySearch { 4 | static int binary_search(int arr[], int x) { 5 | int low = 0, high = arr.length - 1; 6 | while(low<=high) { 7 | int mid = (low + high)/2; 8 | if (arr[mid] == x) { 9 | return mid; 10 | } 11 | if(arr[mid] < x){ 12 | low = mid + 1; 13 | } else { 14 | high = mid - 1; 15 | } 16 | } 17 | return -1; 18 | } 19 | public static void main(String[] args) { 20 | int[] arr = {10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60}; 21 | int x; 22 | x = 25; //item to be searched 23 | int res = binary_search(arr, x); 24 | 25 | if(res == -1){ 26 | System.out.println("Element is not present in the given array."); 27 | } else { 28 | System.out.println(" Element found at index : " + res); 29 | } 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Searching & Sorting/Bubble Sort/Python/solution.py: -------------------------------------------------------------------------------- 1 | # Bubble sort in Python 2 | 3 | def bubbleSort(array): 4 | for i in range(len(array)): 5 | swapped = False 6 | for j in range(0, len(array) - i - 1): 7 | if array[j] > array[j + 1]: 8 | temp = array[j] 9 | array[j] = array[j+1] 10 | array[j+1] = temp 11 | 12 | swapped = True 13 | if not swapped: 14 | break 15 | 16 | print('Enter Array: ') 17 | data = list(map(int, input().split())) 18 | 19 | bubbleSort(data) 20 | 21 | print('Sorted Array in Ascending Order: ') 22 | print(data) 23 | -------------------------------------------------------------------------------- /Searching & Sorting/Insertion Sort/C/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void input(int *a,int n){ 6 | for(int i=0;i= 0 and key < array[j]: 10 | array[j + 1] = array[j] 11 | j = j - 1 12 | 13 | array[j + 1] = key 14 | 15 | print('Enter Array: ') 16 | data = list(map(int, input().split())) 17 | insertionSort(data) 18 | print('Sorted Array in Ascending Order:') 19 | print(data) 20 | -------------------------------------------------------------------------------- /Searching & Sorting/Linear search/C/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a[10],i,n,k,flag,index; 5 | printf("enter no "); 6 | scanf("%d",&n); 7 | flag=0; 8 | 9 | for(i=0;i 3 | 4 | using namespace std; 5 | 6 | void swap(int *a, int *b) //function to swap elements 7 | { 8 | int t = *a; 9 | *a = *b; 10 | *b = t; 11 | } 12 | 13 | void print(int a[], int n) //function to print the array 14 | { 15 | for (int i = 0; i < n; i++) 16 | cout << a[i] << " "; 17 | cout << endl; 18 | } 19 | 20 | int partition(int a[], int low, int high) // function to find the partition point 21 | { 22 | 23 | // select the rightmost element as pivot 24 | int pivot = a[high]; 25 | 26 | // pointer for greater element 27 | int i = (low - 1); 28 | 29 | // traverse each element of the array 30 | // compare them with the pivot 31 | for (int j = low; j < high; j++) 32 | { 33 | if (a[j] <= pivot) 34 | { 35 | 36 | // if element smaller than pivot is found 37 | // swap it with the greater element pointed by i 38 | i++; 39 | 40 | // swap element at i with element at j 41 | swap(&a[i], &a[j]); 42 | } 43 | } 44 | 45 | // swap pivot with the greater element at i 46 | swap(&a[i + 1], &a[high]); 47 | 48 | // return the partition point 49 | return (i + 1); 50 | } 51 | 52 | void quickSort(int a[], int low, int high) 53 | { 54 | if (low < high) 55 | { 56 | 57 | // find the pivot element such that 58 | // elements smaller than pivot are on left of pivot 59 | // elements greater than pivot are on righ of pivot 60 | int pi = partition(a, low, high); 61 | 62 | // recursive call on the left of pivot 63 | quickSort(a, low, pi - 1); 64 | 65 | // recursive call on the right of pivot 66 | quickSort(a, pi + 1, high); 67 | } 68 | } 69 | 70 | // Driver code 71 | int main() 72 | { 73 | int data[] = {8, 7, 6, 1, 0, 9, 2}; 74 | int n = sizeof(data) / sizeof(data[0]); 75 | 76 | cout << "Unsorted Array: \n"; 77 | print(data, n); 78 | 79 | // perform quicksort on data 80 | quickSort(data, 0, n - 1); 81 | 82 | cout << "Sorted array in ascending order: \n"; 83 | print(data, n); 84 | } -------------------------------------------------------------------------------- /Searching & Sorting/Quick Sort/C/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int partition(int a[],int low,int high) 5 | { 6 | int pivot=a[low]; 7 | int i=low+1,c=0,j=high; 8 | do 9 | { 10 | while(pivot>=a[i]) 11 | i++; 12 | while(a[j]>pivot) 13 | j--; 14 | if(i 2 | 3 | using namespace std; 4 | 5 | int binarySearch(int arr[], int l, int r, int x) 6 | 7 | { 8 | 9 | if (r >= l) { int mid = l + (r - l) / 2; 10 | 11 | if (arr[mid] == x) 12 | 13 | return mid; 14 | 15 | if (arr[mid] > x) 16 | 17 | return binarySearch(arr, l, mid - 1, x); 18 | 19 | return binarySearch(arr, mid + 1, r, x); 20 | 21 | } 22 | 23 | return -1; 24 | 25 | } 26 | 27 | int main(void) 28 | 29 | { 30 | 31 | int arr[] = { 2, 3, 4, 10, 40 }; 32 | 33 | int x = 10; 34 | 35 | int n = sizeof(arr) / sizeof(arr[0]); 36 | 37 | int result = binarySearch(arr, 0, n - 1, x); 38 | 39 | (result == -1) ? cout << "Element is not present in array" 40 | 41 | : cout << "Element is present at index " << result; 42 | 43 | return 0; 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Searching and Sorting/Bubble Sort/JavaScript/bubble_sort.js: -------------------------------------------------------------------------------- 1 | //Created an array of n-elements and assigned them random values between (-100, 100) 2 | let n=10; 3 | let array = new Array(n); 4 | for(let i=0;iarr[j]){ 13 | let temp=arr[i]; 14 | arr[i]=arr[j]; 15 | arr[j]=temp; 16 | } 17 | } 18 | 19 | //Writing the Bubble Sort Algorithm 20 | function BubbleSort(arr) { 21 | for(let i=0;i 4 | 5 | using namespace std; 6 | 7 | void insertion__sort(int arr[], int n){ // function to sort the array 8 | int i,j, temp; 9 | for(i=1;i=0 && temp < arr[j]){ 13 | arr[j+1] = arr[j]; 14 | j--; 15 | } 16 | arr[j+1] = temp; 17 | } 18 | } 19 | 20 | int main(){ 21 | int n; 22 | cout<<"How many elements you want in array : "; 23 | cin>>n; 24 | int arr[n]; // array to hold n elements 25 | cout<<"Enter array elements : "; 26 | for(int i=0;i>arr[i]; // taking array elements from user 28 | } 29 | insertion__sort(arr,n); // calling function to sort entered array 30 | cout<<"\nSorted Array : "; 31 | for(int i=0;i 3 | using namespace std; 4 | 5 | // A function for bubble sort 6 | void Bubble_Sort(int array[], int N) 7 | { 8 | // First case 9 | if (N == 1) 10 | return; 11 | 12 | /* One pass of bubble sort. After 13 | this pass, the largest element 14 | is bubbled (or moved) to end.*/ 15 | for (int i=0; i array[i+1]) 17 | swap(array[i], array[i+1]); 18 | 19 | // Largest element is fixed, 20 | 21 | // recur for remaining array 22 | Bubble_Sort(array, N-1); 23 | } 24 | 25 | /* Function to print an array */ 26 | void Print_Array(int array[], int N) 27 | { 28 | for (int i=0; i < N; i++) 29 | printf("%d ", array[i]); 30 | printf("\n"); 31 | } 32 | 33 | // Driver function for testing the above. 34 | int main() 35 | { 36 | int array[] = {32,74,43,86,94,12,10,764}; 37 | int N = sizeof(array)/sizeof(array[0]); 38 | Bubble_Sort(array, N); 39 | printf("Sorted array : \n"); 40 | Print_Array(array, N); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Searching and Sorting/bubble_sort.java: -------------------------------------------------------------------------------- 1 | public class BubbleSortExample { 2 | static void bubbleSort(int[] arr) { 3 | int n = arr.length; 4 | int temp = 0; 5 | for(int i=0; i < n; i++){ 6 | for(int j=1; j < (n-i); j++){ 7 | if(arr[j-1] > arr[j]){ 8 | //swap elements 9 | temp = arr[j-1]; 10 | arr[j-1] = arr[j]; 11 | arr[j] = temp; 12 | } 13 | 14 | } 15 | } 16 | 17 | } 18 | public static void main(String[] args) { 19 | int arr[] ={3,60,35,2,45,320,5}; 20 | 21 | System.out.println("Array Before Bubble Sort"); 22 | for(int i=0; i < arr.length; i++){ 23 | System.out.print(arr[i] + " "); 24 | } 25 | System.out.println(); 26 | 27 | bubbleSort(arr);//sorting array elements using bubble sort 28 | 29 | System.out.println("Array After Bubble Sort"); 30 | for(int i=0; i < arr.length; i++){ 31 | System.out.print(arr[i] + " "); 32 | } 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /Searching and Sorting/java/HeapSort.java: -------------------------------------------------------------------------------- 1 | // Heap Sort 2 | public class HeapSort { 3 | public void sort(int arr[]) 4 | { 5 | int n = arr.length; 6 | 7 | // Build heap (rearrange array) 8 | for (int i = n / 2 - 1; i >= 0; i--) 9 | heapify(arr, n, i); 10 | 11 | // One by one extract an element from heap 12 | for (int i = n - 1; i > 0; i--) { 13 | // Move current root to end 14 | int temp = arr[0]; 15 | arr[0] = arr[i]; 16 | arr[i] = temp; 17 | 18 | // call max heapify on the reduced heap 19 | heapify(arr, i, 0); 20 | } 21 | } 22 | 23 | // To heapify a subtree rooted with node i which is 24 | // an index in arr[]. n is size of heap 25 | void heapify(int arr[], int n, int i) 26 | { 27 | int largest = i; // Initialize largest as root 28 | int l = 2 * i + 1; // left = 2*i + 1 29 | int r = 2 * i + 2; // right = 2*i + 2 30 | 31 | // If left child is larger than root 32 | if (l < n && arr[l] > arr[largest]) 33 | largest = l; 34 | 35 | // If right child is larger than largest so far 36 | if (r < n && arr[r] > arr[largest]) 37 | largest = r; 38 | 39 | // If largest is not root 40 | if (largest != i) { 41 | int swap = arr[i]; 42 | arr[i] = arr[largest]; 43 | arr[largest] = swap; 44 | 45 | // Recursively heapify the affected sub-tree 46 | heapify(arr, n, largest); 47 | } 48 | } 49 | 50 | /* A utility function to print array of size n */ 51 | static void printArray(int arr[]) 52 | { 53 | int n = arr.length; 54 | for (int i = 0; i < n; ++i) 55 | System.out.print(arr[i] + " "); 56 | System.out.println(); 57 | } 58 | 59 | // Driver code 60 | public static void main(String args[]) 61 | { 62 | int arr[] = { 12, 11, 13, 5, 6, 7 }; 63 | int n = arr.length; 64 | 65 | HeapSort ob = new HeapSort(); 66 | ob.sort(arr); 67 | 68 | System.out.println("Sorted array is"); 69 | printArray(arr); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Searching/linear search/CPP/BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int BinarySearch(int a[],int n,int data){ 6 | int l,r; 7 | int mid=0; 8 | l = 0; 9 | r = n -1; 10 | while(l < r){ 11 | mid = (l + r)/2; 12 | if(data == a[mid]) 13 | return mid; 14 | else if(data < a[mid]) 15 | r = mid - 1; 16 | else 17 | l = mid + 1; 18 | } 19 | return -1; 20 | } 21 | 22 | int main(){ 23 | int arraySize, data, result; 24 | cout<<"\nEnter Array Size :- "; 25 | cin>>arraySize; 26 | int a[arraySize]; 27 | cout<<"\nEnter Array Elements :- "; 28 | for(int i = 0; i < arraySize; i++){ 29 | cin>>a[i]; 30 | } 31 | cout<<"\nYour Array elements are :- \n"; 32 | for(int i = 0; i < arraySize; i++){ 33 | cout<<" "<>data; 38 | result = BinarySearch(a,arraySize,data); 39 | cout<<"\n"< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int arraySize, data,j; 7 | cout<<"\nEnter Array Size :- "; 8 | cin>>arraySize; 9 | int a[arraySize]; 10 | cout<<"\nEnter Array Elements :- "; 11 | for(int i = 0; i < arraySize; i++){ 12 | cin>>a[i]; 13 | } 14 | cout<<"\nYour Array elements are :- \n"; 15 | for(int i = 0; i < arraySize; i++){ 16 | cout<<" "<>data; 21 | for(j = 0; j < arraySize; j++){ 22 | if(a[j] == data){ 23 | cout<<"\nElement found at index :- "< 7 | using namespace std; 8 | 9 | int Lsearch(int arr[], int n, int ele){ // function for the linear search 10 | for(int i=0;i>n; 22 | int arr[n]; 23 | for(int i=0;i>arr[i]; // taking input elements from the user 25 | } 26 | cout<<"Enter the element to be searched for : "; 27 | cin>>ele; 28 | ind = Lsearch(arr,n,ele); // caliing function to search ele 29 | if(ind == -1) 30 | cout<<"Element not present in the array !!"; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Searching/linear search/java/LinearSearch.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class LinearSearch 4 | { 5 | public static void main(String args[]) 6 | { 7 | int c, n, search, array[]; 8 | 9 | Scanner sc = new Scanner(System.in); 10 | System.out.println("Enter number of elements"); 11 | n = sc.nextInt(); 12 | array = new int[n]; 13 | 14 | System.out.println("Enter those " + n + " elements"); 15 | 16 | for (c = 0; c < n; c++) 17 | array[c] = sc.nextInt(); 18 | 19 | System.out.println("Enter value to find"); 20 | search = sc.nextInt(); 21 | 22 | for (c = 0; c < n; c++) 23 | { 24 | if (array[c] == search) 25 | { 26 | System.out.println(search + " is present at location " + (c + 1) + "."); 27 | break; 28 | } 29 | 30 | } 31 | if (c == n) 32 | System.out.println(search + " isn't present in array."); 33 | } 34 | } -------------------------------------------------------------------------------- /array/Addition_of_two_matrix/addition_of_two_matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int r, c, a[100][100], b[100][100], sum[100][100], i, j; 4 | printf("Enter the number of rows (between 1 and 100): "); 5 | scanf("%d", &r); 6 | printf("Enter the number of columns (between 1 and 100): "); 7 | scanf("%d", &c); 8 | 9 | printf("\nEnter elements of 1st matrix:\n"); 10 | for (i = 0; i < r; ++i) 11 | for (j = 0; j < c; ++j) { 12 | printf("Enter element a%d%d: ", i + 1, j + 1); 13 | scanf("%d", &a[i][j]); 14 | } 15 | 16 | printf("Enter elements of 2nd matrix:\n"); 17 | for (i = 0; i < r; ++i) 18 | for (j = 0; j < c; ++j) { 19 | printf("Enter element b%d%d: ", i + 1, j + 1); 20 | scanf("%d", &b[i][j]); 21 | } 22 | 23 | // adding two matrices 24 | for (i = 0; i < r; ++i) 25 | for (j = 0; j < c; ++j) { 26 | sum[i][j] = a[i][j] + b[i][j]; 27 | } 28 | 29 | // printing the result 30 | printf("\nSum of two matrices: \n"); 31 | for (i = 0; i < r; ++i) 32 | for (j = 0; j < c; ++j) { 33 | printf("%d ", sum[i][j]); 34 | if (j == c - 1) { 35 | printf("\n\n"); 36 | } 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /array/Factorial_of_large_number/C/C_factorial_of_large_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int N, n, i, carry, size, mul; 5 | int res[4000]; // Let the array have capacity to store up to 4000 digits 6 | 7 | printf("\nEnter number to calculate factorial: "); 8 | scanf("%d", &N); 9 | 10 | res[0] = 1; 11 | carry = 0; 12 | size = 1; //No. of digit in the array 13 | 14 | for(n = 2; n <= N; n++) 15 | { 16 | for(i = 0; i < size; i++) // Loop for multiplying numbers n and res[] 17 | { 18 | mul = (res[i] * n) + carry; // Product of n with each digit + carry from previous operation 19 | res[i] = mul % 10; // Put first digit of product with ith digit at position i itself 20 | carry = mul / 10; // Remaining carry value that will be stored in later indexes 21 | } 22 | 23 | while(carry > 0) // Loop for storing the remaining carry value to the array 24 | { 25 | res[size++] = carry % 10; 26 | carry /= 10; 27 | } 28 | } 29 | 30 | printf("\nFactorial = "); // Printing the result 31 | for(i = size-1; i >= 0; i--) // Array is stored in reverse so printing in reverse 32 | printf("%d", res[i]); 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /array/Good Pairs/c++/Solution.cpp: -------------------------------------------------------------------------------- 1 | //Find No. of Good Pairs 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n, c = 0; 9 | cout << "Enter no. of elements : "; 10 | cin >> n; 11 | int *a = new int[n]; 12 | cout << "Enter array elements..." << endl; 13 | for (int i = 0; i < n; i++) 14 | cin >> a[i]; 15 | for (int i = 0; i < n - 1; i++) 16 | { 17 | for (int j = i + 1; j < n; j++) 18 | { 19 | if (a[i] == a[j]) 20 | c++; 21 | } 22 | } 23 | cout << "Total No. of Good Pairs : " << c << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /array/Kth Largest Element in Array/Java/kth_largest_element.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HacktoberfestSIT/Problem-Solving/dea62ef75e414fd4e0a354acbc68a704a77fd8e1/array/Kth Largest Element in Array/Java/kth_largest_element.java -------------------------------------------------------------------------------- /array/Kth Largest Element in Array/java/solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class GFG 3 | { 4 | 5 | 6 | class MinHeap 7 | { 8 | int[] harr; 9 | int capacity; 10 | int heap_size; 11 | 12 | int parent(int i) { return (i - 1) / 2; } 13 | int left(int i) { return ((2 * i )+ 1); } 14 | int right(int i) { return ((2 * i) + 2); } 15 | int getMin() { return harr[0]; } 16 | 17 | // to replace root with new node x and heapify() new root 18 | void replaceMax(int x) 19 | { 20 | this.harr[0] = x; 21 | minHeapify(0); 22 | } 23 | MinHeap(int a[], int size) 24 | { 25 | heap_size = size; 26 | harr = a; // store address of array 27 | int i = (heap_size - 1) / 2; 28 | while (i >= 0) 29 | { 30 | minHeapify(i); 31 | i--; 32 | } 33 | } 34 | 35 | // Method to remove maximum element (or root) from min heap 36 | int extractMin() 37 | { 38 | if (heap_size == 0) 39 | return Integer.MAX_VALUE; 40 | 41 | // Store the maximum vakue. 42 | int root = harr[0]; 43 | 44 | 45 | if (heap_size > 1) 46 | { 47 | harr[0] = harr[heap_size - 1]; 48 | minHeapify(0); 49 | } 50 | heap_size--; 51 | return root; 52 | } 53 | 54 | 55 | void minHeapify(int i) 56 | { 57 | int l = left(i); 58 | int r = right(i); 59 | int smallest = i; 60 | if (l < heap_size && harr[l] < harr[i]) 61 | smallest = l; 62 | if (r < heap_size && harr[r] < harr[smallest]) 63 | smallest = r; 64 | if (smallest != i) 65 | { 66 | int t = harr[i]; 67 | harr[i] = harr[smallest]; 68 | harr[smallest] = t; 69 | minHeapify(smallest); 70 | } 71 | } 72 | }; 73 | 74 | // Function to return k'th largest element in a given array 75 | int kthSmallest(int arr[], int n, int k) 76 | { 77 | 78 | // Build a heap of first k elements: O(k) time 79 | MinHeap mh = new MinHeap(arr, n); 80 | 81 | 82 | for (int i = 0; i < k - 1; i++) 83 | mh.extractMin(); 84 | 85 | // Return root 86 | return mh.getMin(); 87 | } 88 | 89 | 90 | public static void main(String[] args) 91 | { 92 | 93 | Scanner scn = new Scanner(System.in); 94 | int n = scn.nextInt() 95 | int[] arr = new int[n]; 96 | for(int i=0;i 2 | using namespace std; 3 | 4 | /*C++ Function to print leaders in an array */ 5 | void printLeaders(int arr[], int size) 6 | { 7 | for (int i = 0; i < size; i++) 8 | { 9 | int j; 10 | for (j = i+1; j < size; j++) 11 | { 12 | if (arr[i] <=arr[j]) 13 | break; 14 | } 15 | if (j == size) // the loop didn't break 16 | cout << arr[i] << " "; 17 | } 18 | } 19 | 20 | /* Driver program to test above function */ 21 | int main() 22 | { 23 | int arr[] = {16, 17, 4, 3, 5, 2}; 24 | int n = sizeof(arr)/sizeof(arr[0]); 25 | printLeaders(arr, n); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /array/LemonadeChange/Lemonade_Change.java: -------------------------------------------------------------------------------- 1 | 2 | /*Given an integer array bills where bills[i] is the bill the ith customer pays 3 | return true if you can provide every customer with correct change, or false otherwise.*/ 4 | class Solution { 5 | public boolean lemonadeChange(int[] bills) { 6 | int five = 0, ten = 0; 7 | for (int bill: bills) { 8 | if (bill == 5) 9 | five++; 10 | else if (bill == 10) { 11 | if (five == 0) return false; 12 | five--; 13 | ten++; 14 | } else { 15 | if (five > 0 && ten > 0) { 16 | five--; 17 | ten--; 18 | } else if (five >= 3) { 19 | five -= 3; 20 | } else { 21 | return false; 22 | } 23 | } 24 | } 25 | 26 | return true; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /array/Merge Array/array.c: -------------------------------------------------------------------------------- 1 | #include 2 | void marge(int result[],int *resultLength, int array1[],int length1,int array2[],int length2){ 3 | int i; 4 | for(i=0;i 3 | 4 | using namespace std; 5 | 6 | void check(int *a, int *b, int n) 7 | { 8 | for (int i = 0; i <= n; i++) 9 | { 10 | int c = 0; 11 | for (int j = 0; j < n; j++) 12 | { 13 | if (b[i] == a[j]) 14 | c++; 15 | } 16 | if (c == 0) 17 | { 18 | cout << "Missing No. : " << b[i] << endl; 19 | exit(0); 20 | } 21 | } 22 | } 23 | 24 | int main() 25 | { 26 | int n; 27 | cout << "Enter no. of elements : "; 28 | cin >> n; 29 | int *a = new int[n]; 30 | cout << "Enter elements into the array :" << endl; 31 | cout << "NOTE : Each element should be unique and in [0,n]" 32 | << endl; 33 | for (int i = 0; i < n; i++) 34 | cin >> a[i]; 35 | int *b = new int[n + 1]; 36 | for (int i = 0; i <= n; i++) 37 | b[i] = i; 38 | check(a, b, n); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /array/Sort Array By Parity/java/sortArrayByParity.java: -------------------------------------------------------------------------------- 1 | 2 | /* Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers.*/ 3 | 4 | class Solution { 5 | public int[] sortArrayByParity(int[] A) { 6 | Integer[] B = new Integer[A.length]; 7 | for (int t = 0; t < A.length; ++t) 8 | B[t] = A[t]; 9 | 10 | Arrays.sort(B, (a, b) -> Integer.compare(a%2, b%2)); 11 | 12 | for (int t = 0; t < A.length; ++t) 13 | A[t] = B[t]; 14 | return A; 15 | 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /array/maximum subarray/cpp/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an integer array nums, find the contiguous subarray 3 | (containing at least one number) which has the largest sum and return its sum. 4 | A subarray is a contiguous part of an array. 5 | 6 | Link to problem: https://leetcode.com/problems/maximum-subarray/ 7 | 8 | Example 1: 9 | Input: nums = [-2,1,-3,4,-1,2,1,-5,4] 10 | Output: 6 11 | Explanation: [4,-1,2,1] has the largest sum = 6. 12 | 13 | We will use Kadane's Algorithm which is an optimal approach. 14 | time complexity O(n) 15 | space complexity O(1) 16 | So let's do it 😎*/ 17 | 18 | class Solution { 19 | public: 20 | int maxSubArray(vector& nums) { 21 | int max_subA = INT_MIN, sum=0; // initialise max_subA with INT_MIN or nums[0] & sum =0 22 | for(auto i: nums){ // iterating over each element of array 23 | sum += i; 24 | 25 | // if any time sum exceeds the max_subA, change the value of max_subA to sum 26 | if(sum > max_subA){ 27 | max_subA = sum; // OR: max_subA = max(max_subA,sum); 28 | } 29 | 30 | // if any time sum gives a negative sum, change the value of sum to 0 31 | if(sum<0){ 32 | sum=0; 33 | } 34 | } 35 | return max_subA; 36 | } 37 | }; -------------------------------------------------------------------------------- /array/reverse an array/java/reverse.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main{ 5 | public static void display(int[] a){ 6 | StringBuilder sb = new StringBuilder(); 7 | 8 | for(int val: a){ 9 | sb.append(val + " "); 10 | } 11 | System.out.println(sb); 12 | } 13 | 14 | public static void reverse(int[] a){ 15 | int li = 0; 16 | int ri = a.length - 1; 17 | 18 | while(li < ri){ 19 | int temp = a[li]; 20 | a[li]= a[ri]; 21 | a[ri] = temp; 22 | 23 | li++; 24 | ri--; 25 | } 26 | } 27 | 28 | public static void main(String[] args) throws Exception { 29 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 30 | 31 | int n = Integer.parseInt(br.readLine()); 32 | int[] a = new int[n]; 33 | for(int i = 0; i < n; i++){ 34 | a[i] = Integer.parseInt(br.readLine()); 35 | } 36 | 37 | reverse(a); 38 | display(a); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /maths/StandardDeviation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | float calculateSD(float data[]); 6 | int main(){ 7 | int i; 8 | float data[10]; 9 | cout << "Enter 10 elements: "; 10 | for(i = 0; i < 10; ++i) 11 | cin >> data[i]; 12 | cout << endl << "Standard Deviation = " << calculateSD(data); 13 | return 0; 14 | } 15 | float calculateSD(float data[]){ 16 | 17 | float sum = 0.0, mean, standardDeviation = 0.0; 18 | int i; 19 | for(i = 0; i < 10; ++i){ 20 | sum += data[i]; 21 | } 22 | mean = sum/10; 23 | for(i = 0; i < 10; ++i) 24 | standardDeviation += pow(data[i] - mean, 2); 25 | return sqrt(standardDeviation / 10); 26 | } 27 | -------------------------------------------------------------------------------- /maths/digits.c: -------------------------------------------------------------------------------- 1 | #include 2 | int sumOfDigit(int number){ 3 | int result=0; 4 | for(;number!=0;number/=10){ 5 | result+=(number%10); 6 | } 7 | return result; 8 | } 9 | int productOfDigit(int number){ 10 | int result=1; 11 | for(;number!=0;number/=10){ 12 | result*=(number%10); 13 | } 14 | return result; 15 | } 16 | void main(){ 17 | unsigned long int number; 18 | int sum,product; 19 | printf("Enter a number : "); 20 | scanf("%d",&number); 21 | sum=sumOfDigit(number); 22 | product=productOfDigit(number); 23 | printf("Sum of Digit = %d \n",sum); 24 | printf("Product of Digit = %d\n",product); 25 | printf("Result %d \n",product-sum); 26 | } 27 | -------------------------------------------------------------------------------- /sorting/bubble_sort.cpp: -------------------------------------------------------------------------------- 1 | /* BUBBLE SORT */ 2 | 3 | /* Repeatedly swap two adjacent elements if they are in wrong order */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int n; 11 | cout << "Enter the size of the array : "; 12 | cin >> n; 13 | int arr[n]; 14 | cout << "Enter the elements of the array : " << endl; 15 | for (int k = 0; k < n; k++) 16 | { 17 | cin >> arr[k]; 18 | } 19 | for(int i =0; iarr[j]) 24 | { 25 | int temp; 26 | temp = arr[j]; 27 | arr[j] = arr[j+1]; 28 | arr[j+1] = temp; 29 | } 30 | } 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /sorting/comb sort/comb_sort.cpp: -------------------------------------------------------------------------------- 1 | // C++ implementation of Comb Sort 2 | #include 3 | using namespace std; 4 | 5 | // To find gap between elements 6 | int getNextGap(int gap) 7 | { 8 | // Shrink gap by Shrink factor 9 | gap = (gap*10)/13; 10 | 11 | if (gap < 1) 12 | return 1; 13 | return gap; 14 | } 15 | 16 | // Function to sort a[0..n-1] using Comb Sort 17 | void combSort(int a[], int n) 18 | { 19 | // Initialize gap 20 | int gap = n; 21 | 22 | // Initialize swapped as true to make sure that 23 | // loop runs 24 | bool swapped = true; 25 | 26 | // Keep running while gap is more than 1 and last 27 | // iteration caused a swap 28 | while (gap != 1 || swapped == true) 29 | { 30 | // Find next gap 31 | gap = getNextGap(gap); 32 | 33 | // Initialize swapped as false so that we can 34 | // check if swap happened or not 35 | swapped = false; 36 | 37 | // Compare all elements with current gap 38 | for (int i=0; i a[i+gap]) 41 | { 42 | swap(a[i], a[i+gap]); 43 | swapped = true; 44 | } 45 | } 46 | } 47 | } 48 | 49 | // Driver program 50 | int main() 51 | { 52 | int a[] = {8, 4, 1, 56, 3, -44, 23, -6, 28, 0}; 53 | int n = sizeof(a)/sizeof(a[0]); 54 | 55 | combSort(a, n); 56 | 57 | printf("Sorted array: \n"); 58 | for (int i=0; i 3 | using namespace std; 4 | void insertion_sort(int arr[],int n){ 5 | for(int i=0;i=0;j--){ 7 | if (arr[j] < arr[j-1] ){ 8 | 9 | int temp = arr[j]; 10 | arr[j] = arr[j-1]; 11 | arr[j-1] = temp; 12 | } 13 | } 14 | } 15 | cout<<"sorted array"<>a[i]; 25 | } 26 | insertion_sort(a,5); 27 | 28 | return 0; 29 | 30 | } -------------------------------------------------------------------------------- /sorting/mergeSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int n; 4 | printf("Enter the size of array: "); 5 | scanf("%d",&n); 6 | int arr[n]; 7 | printf("Enter the elements of array: "); 8 | for(int i=0;i 3 | using namespace std; 4 | 5 | // A utility function to get maximum value in arr[] 6 | int getMax(int arr[], int n) 7 | { 8 | int mx = arr[0]; 9 | for (int i = 1; i < n; i++) 10 | if (arr[i] > mx) 11 | mx = arr[i]; 12 | return mx; 13 | } 14 | 15 | // A function to do counting sort of arr[] according to 16 | // the digit represented by exp. 17 | void countSort(int arr[], int n, int exp) 18 | { 19 | int output[n]; // output array 20 | int i, count[10] = { 0 }; 21 | 22 | // Store count of occurrences in count[] 23 | for (i = 0; i < n; i++) 24 | count[(arr[i] / exp) % 10]++; 25 | 26 | // Change count[i] so that count[i] now contains actual 27 | // position of this digit in output[] 28 | for (i = 1; i < 10; i++) 29 | count[i] += count[i - 1]; 30 | 31 | // Build the output array 32 | for (i = n - 1; i >= 0; i--) { 33 | output[count[(arr[i] / exp) % 10] - 1] = arr[i]; 34 | count[(arr[i] / exp) % 10]--; 35 | } 36 | 37 | // Copy the output array to arr[], so that arr[] now 38 | // contains sorted numbers according to current digit 39 | for (i = 0; i < n; i++) 40 | arr[i] = output[i]; 41 | } 42 | 43 | // The main function to that sorts arr[] of size n using 44 | // Radix Sort 45 | void radixsort(int arr[], int n) 46 | { 47 | // Find the maximum number to know number of digits 48 | int m = getMax(arr, n); 49 | 50 | // Do counting sort for every digit. Note that instead 51 | // of passing digit number, exp is passed. exp is 10^i 52 | // where i is current digit number 53 | for (int exp = 1; m / exp > 0; exp *= 10) 54 | countSort(arr, n, exp); 55 | } 56 | 57 | // A utility function to print an array 58 | void print(int arr[], int n) 59 | { 60 | for (int i = 0; i < n; i++) 61 | cout << arr[i] << " "; 62 | } 63 | 64 | // Driver Code 65 | int main() 66 | { 67 | int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 }; 68 | int n = sizeof(arr) / sizeof(arr[0]); 69 | 70 | // Function Call 71 | radixsort(arr, n); 72 | print(arr, n); 73 | return 0; 74 | } 75 | 76 | 77 | //Output: 6 12 25 161 415 573 78 | -------------------------------------------------------------------------------- /sorting/selection_sort.cpp: -------------------------------------------------------------------------------- 1 | /* SELECTION SORT */ 2 | 3 | /* Find the minimum elementin unsorted array and swap it with element at beginning */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int n; 11 | cout << "Enter the size of the array : "; 12 | cin >> n; 13 | int arr[n]; 14 | cout << "Enter the elements of the array : " << endl; 15 | for (int k = 0; k < n; k++) 16 | { 17 | cin >> arr[k]; 18 | } 19 | 20 | for (int i = 0; i < n-1; i++) 21 | { 22 | for(int j = (i+1); jarr[j]) 25 | { 26 | int temp; 27 | temp = arr[j]; 28 | arr[j] = arr[i]; 29 | arr[i] = temp; 30 | } 31 | } 32 | } 33 | cout << "Elements of array after sorting are : "; 34 | for (int i = 0; i < n; i++) 35 | { 36 | cout << arr[i] << " "; 37 | } 38 | return 0; 39 | } 40 | --------------------------------------------------------------------------------