├── Bubble Sort.cpp ├── C++ ├── Basic_Calculator.cpp ├── BubbleSort.cpp ├── Merge_Sort.cpp ├── Path_Parity.cpp ├── Prime_Number.cpp ├── Quick_Sort.cpp ├── Sort 0's,1's,2's.cpp ├── Towerofhanoi.cpp ├── binary_searching.cpp ├── cube root of a number ├── fibonacci_series.cpp ├── insertion.cpp ├── linear_searching.cpp └── selection_sort.cpp ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── Leetcode 2. Add Two Numbers ├── QuickSort.cpp ├── README.md ├── c ├── AmstrgFrom1ToN.c ├── ArraySort.c ├── Binary_Search.c ├── Bogo Sort.c ├── Celsius_to_Fahrenheit.c ├── CircularQueue.c ├── EiffelTowerProg.c ├── Factorial.c ├── FibonacciSeries.c ├── Sorting │ ├── bubbleSort.c │ ├── countingSort.c │ └── insertionSort.c ├── SumOfNaturalNumber.c ├── bankers_algorithm.c ├── binarysearch.c ├── circularlinkedlist.c ├── factorial.c ├── priority.c ├── radix_sort.c └── sorting │ ├── Heapsort.c │ ├── SortStringLexicographically.cpp │ ├── add_number.c │ ├── bubble_sort.c │ ├── countSort.c │ ├── insertion_sort.c │ ├── merge_sort.c │ ├── quicksort.c │ └── selection_sort.c ├── floyd warshall algorithm dp ├── java └── sorting │ ├── LinkedList.java │ ├── insertion_sort.java │ └── using_loop.java ├── knapsack 01 ├── python ├── InsertionSort.py ├── Merge_sort.py ├── Number_Guessing.py ├── Quick_sort.py ├── Zip_code.py ├── bubble_sort.py └── string.py └── strassen matrix /Bubble Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void Bubble_Sort(int N, int Ar[]) 3 | { 4 | int i,j,k,t; 5 | for(i=1;iAr[j+1]) 10 | { 11 | t=Ar[j]; 12 | Ar[j]=Ar[j+1]; 13 | Ar[j+1]=t; 14 | } 15 | } 16 | printf("\nThe array after iteration %d is: ",i); 17 | for(k=0;k 2 | 3 | int main() 4 | { 5 | int num1,num2; 6 | float result; 7 | char ch; //to store operator choice 8 | 9 | printf("Enter first number: "); 10 | scanf("%d",&num1); 11 | printf("Enter second number: "); 12 | scanf("%d",&num2); 13 | 14 | printf("Choose operation to perform (+,-,*,/,%): "); 15 | scanf(" %c",&ch); 16 | 17 | result=0; 18 | switch(ch) 19 | { 20 | case '+': 21 | result=num1+num2; 22 | break; 23 | 24 | case '-': 25 | result=num1-num2; 26 | break; 27 | 28 | case '*': 29 | result=num1*num2; 30 | break; 31 | 32 | case '/': 33 | result=(float)num1/(float)num2; 34 | break; 35 | 36 | case '%': 37 | result=num1%num2; 38 | break; 39 | default: 40 | printf("Invalid operation.\n"); 41 | } 42 | 43 | printf("Result: %d %c %d = %f\n",num1,ch,num2,result); 44 | return 0; 45 | } 46 | 47 | /* 48 | Output 49 | First run: 50 | Enter first number: 10 51 | Enter second number: 20 52 | Choose operation to perform (+,-,*,/,%): + 53 | Result: 10 + 20 = 30.000000 54 | 55 | Second run: 56 | Enter first number: 10 57 | Enter second number: 3 58 | Choose operation to perform (+,-,*,/,%): / 59 | Result: 10 / 3 = 3.333333 60 | 61 | Third run: 62 | Enter first number: 10 63 | Enter second number: 3 64 | Choose operation to perform (+,-,*,/,%): > 65 | Invalid operation. 66 | Result: 10 > 3 = 0.000000 67 | */ 68 | -------------------------------------------------------------------------------- /C++/BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | // Bubble Sort 2 | 3 | #include 4 | using namespace std; 5 | void BubbleSort(int arr[], int n) 6 | { 7 | int round, i; 8 | for (round = 0; round < n - 1; round++) 9 | { 10 | int flag = 0; // for modification of time complexity. This is not a part of actual algorithm. 11 | for (i = 0; i < n - 1 - round; i++) 12 | { 13 | if (arr[i] > arr[i + 1]) 14 | { 15 | flag = 1; 16 | int temp = arr[i + 1]; 17 | arr[i + 1] = arr[i]; 18 | arr[i] = temp; 19 | } 20 | } 21 | if (flag == 0) 22 | return; 23 | } 24 | } 25 | int main() 26 | { 27 | int n; 28 | cout << "Enter array size : "; 29 | cin >> n; 30 | int arr[n]; 31 | int i, j; 32 | cout << "Enter the elements of the array :" << endl; 33 | for (i = 0; i < n; i++) 34 | cin >> arr[i]; 35 | BubbleSort(arr, n); 36 | cout << "After sorting the array in accending order by Bubble sorting algorithm, we get : " << endl; 37 | for (i = 0; i < n; i++) 38 | cout << arr[i] << " "; 39 | return 0; 40 | } 41 | 42 | /* 43 | Output: 44 | Enter array size : 5 45 | Enter the elements of the array : 46 | 10 59 39 85 57 47 | After sorting the array in accending order by Bubble sorting algorithm, we get : 48 | 10 39 57 59 85 49 | */ 50 | -------------------------------------------------------------------------------- /C++/Merge_Sort.cpp: -------------------------------------------------------------------------------- 1 | // Merge Sorting 2 | #include 3 | using namespace std; 4 | void merge(int *,int, int , int ); 5 | void merge_sort(int *arr, int low, int high) 6 | { 7 | int mid; 8 | if (low < high){ 9 | //divide the array at mid and sort independently using merge sort 10 | mid=(low+high)/2; 11 | merge_sort(arr,low,mid); 12 | merge_sort(arr,mid+1,high); 13 | //merge or conquer sorted arrays 14 | merge(arr,low,high,mid); 15 | } 16 | } 17 | // Merge sort 18 | void merge(int *arr, int low, int high, int mid) 19 | { 20 | int i, j, k, c[50]; 21 | i = low; 22 | k = low; 23 | j = mid + 1; 24 | while (i <= mid && j <= high) { 25 | if (arr[i] < arr[j]) { 26 | c[k] = arr[i]; 27 | k++; 28 | i++; 29 | } 30 | else { 31 | c[k] = arr[j]; 32 | k++; 33 | j++; 34 | } 35 | } 36 | while (i <= mid) { 37 | c[k] = arr[i]; 38 | k++; 39 | i++; 40 | } 41 | while (j <= high) { 42 | c[k] = arr[j]; 43 | k++; 44 | j++; 45 | } 46 | for (i = low; i < k; i++) { 47 | arr[i] = c[i]; 48 | } 49 | } 50 | // read input array and call mergesort 51 | int main() 52 | { 53 | int myarray[30], num; 54 | cout<<"Enter number of elements to be sorted:"; 55 | cin>>num; 56 | cout<<"Enter "<>myarray[i]; 58 | } 59 | merge_sort(myarray, 0, num-1); 60 | cout<<"Sorted array\n"; 61 | for (int i = 0; i < num; i++) 62 | { 63 | cout< 2 | using namespace std; 3 | typedef long long ll; 4 | 5 | int main(){ 6 | int test; 7 | cin>>test; 8 | 9 | while(test--){ 10 | 11 | ll ps,prx,pry; 12 | 13 | ll ptk,ptok=0; 14 | 15 | cin>>ps>>ptk; 16 | 17 | if(ps%2==0) cout<<"Yes"< 2 | using namespace std; 3 | int main() 4 | { 5 | int n, i, m=0, flag=0; 6 | cout << "Enter the Number to check Prime: "; 7 | cin >> n; 8 | m=n/2; 9 | for(i = 2; i <= m; i++) 10 | { 11 | if(n % i == 0) 12 | { 13 | cout<<"Number is not Prime."< 3 | using namespace std; 4 | 5 | int partition(int arr[], int start, int end) 6 | { 7 | 8 | int pivot = arr[start]; 9 | 10 | int count = 0; 11 | for (int i = start + 1; i <= end; i++) { 12 | if (arr[i] <= pivot) 13 | count++; 14 | } 15 | 16 | // Giving pivot element its correct position 17 | int pivotIndex = start + count; 18 | swap(arr[pivotIndex], arr[start]); 19 | 20 | // Sorting left and right parts of the pivot element 21 | int i = start, j = end; 22 | 23 | while (i < pivotIndex && j > pivotIndex) { 24 | 25 | while (arr[i] <= pivot) { 26 | i++; 27 | } 28 | 29 | while (arr[j] > pivot) { 30 | j--; 31 | } 32 | 33 | if (i < pivotIndex && j > pivotIndex) { 34 | swap(arr[i++], arr[j--]); 35 | } 36 | } 37 | 38 | return pivotIndex; 39 | } 40 | 41 | void quickSort(int arr[], int start, int end) 42 | { 43 | 44 | // base case 45 | if (start >= end) 46 | return; 47 | 48 | // partitioning the array 49 | int p = partition(arr, start, end); 50 | 51 | // Sorting the left part 52 | quickSort(arr, start, p - 1); 53 | 54 | // Sorting the right part 55 | quickSort(arr, p + 1, end); 56 | } 57 | 58 | int main() 59 | { 60 | 61 | int arr[] = { 9, 3, 4, 2, 1, 8 }; 62 | int n = 6; 63 | 64 | quickSort(arr, 0, n - 1); 65 | 66 | for (int i = 0; i < n; i++) { 67 | cout << arr[i] << " "; 68 | } 69 | 70 | return 0; 71 | } 72 | /* 73 | Output 74 | 1 2 3 4 8 9 75 | */ 76 | -------------------------------------------------------------------------------- /C++/Sort 0's,1's,2's.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | void dutchFlagAlgorithm(vector &arr, int n) 7 | { 8 | int low = 0; 9 | int mid = 0; 10 | int high = n-1; 11 | 12 | while(mid <= high) 13 | { 14 | if(arr[mid] == 0) 15 | { 16 | swap(arr[mid], arr[low]); 17 | mid++; 18 | low++; 19 | } 20 | 21 | else if(arr[mid] == 1) 22 | { 23 | mid++; 24 | } 25 | 26 | else if(arr[mid] == 2) 27 | { 28 | swap(arr[mid], arr[high]); 29 | high--; 30 | } 31 | } 32 | } 33 | 34 | 35 | int main() 36 | { 37 | int n; 38 | cin >> n; 39 | 40 | vector v; 41 | 42 | for(int i = 0; i < n; i++) 43 | { 44 | int x; 45 | cin >> x; 46 | 47 | v.push_back(x); 48 | } 49 | 50 | dutchFlagAlgorithm(v,n); 51 | 52 | for(int i = 0; i < n; i++) 53 | { 54 | cout << v[i] << endl; 55 | } 56 | 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /C++/Towerofhanoi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //tower of HANOI function implementation 5 | void TOH(int n,char Sour, char Aux,char Des) 6 | { 7 | if(n==1) 8 | { 9 | cout<<"Move Disk "<>n; 25 | //calling the TOH 26 | TOH(n,'A','B','C'); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /C++/binary_searching.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int binary_search(vector arr,int x) 5 | { 6 | int l=0,e=arr.size()-1; 7 | while(l>n; 29 | 30 | cout<<"Enter the elements of array in sorted order\n"; 31 | 32 | vector arr(n); 33 | for(int i=0;i>arr[i]; 35 | 36 | int x; 37 | cout<<"Enter the elements you want to search in sorted array\n"; 38 | cin>>x; 39 | 40 | // function to search the number in log(n) time 41 | 42 | int index = binary_search(arr,x); 43 | 44 | 45 | if(index!=-1) 46 | cout<<"Number Found at index "< 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int num; 10 | double ans; 11 | 12 | cout << "Enter number which u want to find cube root :: "; 13 | cin >> num; 14 | 15 | ans = (double)pow((double)num, (double)1 / (double)3); 16 | 17 | cout << "\nCube Root of [ " << num << " ] is :: " << ans << "\n"; 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /C++/fibonacci_series.cpp: -------------------------------------------------------------------------------- 1 | // Fibonnaci series using recursion in C++ 2 | #include 3 | using namespace std; 4 | void printFibonacci(int n){ 5 | static int n1=0, n2=1, n3; 6 | if(n>0){ 7 | n3 = n1 + n2; 8 | n1 = n2; 9 | n2 = n3; 10 | cout<>n; 18 | cout<<"Fibonacci Series: "; 19 | cout<<"0 "<<"1 "; 20 | printFibonacci(n-2); //n-2 because 2 numbers are already printed 21 | return 0; 22 | } 23 | /* 24 | Output: 25 | 26 | Enter the number of elements: 15 27 | Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 28 | */ 29 | -------------------------------------------------------------------------------- /C++/insertion.cpp: -------------------------------------------------------------------------------- 1 | // C++ program for insertion sort 2 | 3 | #include 4 | using namespace std; 5 | 6 | // Function to sort an array using 7 | // insertion sort 8 | void insertionSort(int arr[], int n) 9 | { 10 | int i, key, j; 11 | for (i = 1; i < n; i++) 12 | { 13 | key = arr[i]; 14 | j = i - 1; 15 | 16 | // Move elements of arr[0..i-1], 17 | // that are greater than key, to one 18 | // position ahead of their 19 | // current position 20 | while (j >= 0 && arr[j] > key) 21 | { 22 | arr[j + 1] = arr[j]; 23 | j = j - 1; 24 | } 25 | arr[j + 1] = key; 26 | } 27 | } 28 | 29 | // A utility function to print an array 30 | // of size n 31 | void printArray(int arr[], int n) 32 | { 33 | int i; 34 | for (i = 0; i < n; i++) 35 | cout << arr[i] << " "; 36 | cout << endl; 37 | } 38 | 39 | // Driver code 40 | int main() 41 | { 42 | int arr[] = { 12, 11, 13, 5, 6 }; 43 | int N = sizeof(arr) / sizeof(arr[0]); 44 | 45 | insertionSort(arr, N); 46 | printArray(arr, N); 47 | 48 | return 0; 49 | } 50 | // This is code is contributed by rathbhupendra 51 | -------------------------------------------------------------------------------- /C++/linear_searching.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(NULL); 7 | 8 | int n; 9 | cin>>n; 10 | vector arr(n); 11 | for(int i=0;i>arr[i]; 13 | 14 | int x; 15 | cin>>x; 16 | bool flag =false; 17 | for(int i=0;i 4 | using namespace std; 5 | 6 | //Swap function 7 | void swap(int *xp, int *yp) 8 | { 9 | int temp = *xp; 10 | *xp = *yp; 11 | *yp = temp; 12 | } 13 | 14 | void selectionSort(int arr[], int n) 15 | { 16 | int i, j, min_idx; 17 | 18 | // One by one move boundary of 19 | // unsorted subarray 20 | for (i = 0; i < n-1; i++) 21 | { 22 | 23 | // Find the minimum element in 24 | // unsorted array 25 | min_idx = i; 26 | for (j = i+1; j < n; j++) 27 | if (arr[j] < arr[min_idx]) 28 | min_idx = j; 29 | 30 | // Swap the found minimum element 31 | // with the first element 32 | if(min_idx!=i) 33 | swap(&arr[min_idx], &arr[i]); 34 | } 35 | } 36 | 37 | //Function to print an array 38 | void printArray(int arr[], int size) 39 | { 40 | int i; 41 | for (i=0; i < size; i++) 42 | cout << arr[i] << " "; 43 | cout << endl; 44 | } 45 | 46 | // Driver program to test above functions 47 | int main() 48 | { 49 | int arr[] = {64, 25, 12, 22, 11}; 50 | int n = sizeof(arr)/sizeof(arr[0]); 51 | selectionSort(arr, n); 52 | cout << "Sorted array: \n"; 53 | printArray(arr, n); 54 | return 0; 55 | } 56 | // This is code is contributed by rathbhupendra 57 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, religion, or sexual identity 10 | and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | * Demonstrating empathy and kindness toward other people 21 | * Being respectful of differing opinions, viewpoints, and experiences 22 | * Giving and gracefully accepting constructive feedback 23 | * Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | * Focusing on what is best not just for us as individuals, but for the 26 | overall community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | * The use of sexualized language or imagery, and sexual attention or 31 | advances of any kind 32 | * Trolling, insulting or derogatory comments, and personal or political attacks 33 | * Public or private harassment 34 | * Publishing others' private information, such as a physical or email 35 | address, without their explicit permission 36 | * Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement. 63 | All complaints will be reviewed and investigated promptly and fairly. 64 | 65 | All community leaders are obligated to respect the privacy and security of the 66 | reporter of any incident. 67 | 68 | ## Enforcement Guidelines 69 | 70 | Community leaders will follow these Community Impact Guidelines in determining 71 | the consequences for any action they deem in violation of this Code of Conduct: 72 | 73 | ### 1. Correction 74 | 75 | **Community Impact**: Use of inappropriate language or other behavior deemed 76 | unprofessional or unwelcome in the community. 77 | 78 | **Consequence**: A private, written warning from community leaders, providing 79 | clarity around the nature of the violation and an explanation of why the 80 | behavior was inappropriate. A public apology may be requested. 81 | 82 | ### 2. Warning 83 | 84 | **Community Impact**: A violation through a single incident or series 85 | of actions. 86 | 87 | **Consequence**: A warning with consequences for continued behavior. No 88 | interaction with the people involved, including unsolicited interaction with 89 | those enforcing the Code of Conduct, for a specified period of time. This 90 | includes avoiding interactions in community spaces as well as external channels 91 | like social media. Violating these terms may lead to a temporary or 92 | permanent ban. 93 | 94 | ### 3. Temporary Ban 95 | 96 | **Community Impact**: A serious violation of community standards, including 97 | sustained inappropriate behavior. 98 | 99 | **Consequence**: A temporary ban from any sort of interaction or public 100 | communication with the community for a specified period of time. No public or 101 | private interaction with the people involved, including unsolicited interaction 102 | with those enforcing the Code of Conduct, is allowed during this period. 103 | Violating these terms may lead to a permanent ban. 104 | 105 | ### 4. Permanent Ban 106 | 107 | **Community Impact**: Demonstrating a pattern of violation of community 108 | standards, including sustained inappropriate behavior, harassment of an 109 | individual, or aggression toward or disparagement of classes of individuals. 110 | 111 | **Consequence**: A permanent ban from any sort of public interaction within 112 | the community. 113 | 114 | ## Attribution 115 | 116 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 117 | version 2.0, available at 118 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 119 | 120 | Community Impact Guidelines were inspired by [Mozilla's code of conduct 121 | enforcement ladder](https://github.com/mozilla/diversity). 122 | 123 | [homepage]: https://www.contributor-covenant.org 124 | 125 | For answers to common questions about this code of conduct, see the FAQ at 126 | https://www.contributor-covenant.org/faq. Translations are available at 127 | https://www.contributor-covenant.org/translations. 128 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contribution Guidelines 2 | 3 | _Welcome hackers! It is **awesome** to have you here! Before you start contributing with this project make sure you read our [Code Of Conduct](https://github.com/OtacilioN/awesome-hacktoberfest-2018/blob/master/CODE_OF_CONDUCT.md), it is really important to make this inclusive and open to everyone, otherwise, it would not be awesome_ 4 | This repository was created to help developers get started with Open Source during [Hacktoberfest](https://hacktoberfest.digitalocean.com/). 5 | 6 | ## Have you found some awesome repositories participating in Hacktoberfest? 7 | 8 | Add them to this list so others can find it too and start contributing, however, make sure that these repositories are beginner-friendly! 9 | > NOTE: Check out `issues` labelled as `Hacktoberfest` and start contributing! 10 | 11 | ## Submitting a pull request 12 | 13 | 1. [Fork](https://github.com/OtacilioN/awesome-hacktoberfest-2018/fork) and clone the repository. 14 | 1. Create a new branch: `git checkout -b my-branch-name`. 15 | 1. Add your item in alphabetical order in a section or create a new section, if you are not sure about the appropriate section you can open an `issue` to discuss with the community. 16 | 1. Push to your fork and [submit a pull request](https://github.com/OtacilioN/awesome-hacktoberfest-2018/compare). 17 | > Tips: Make sure that you are submitting and committing things that are of quality substance, otherwise maintainers can invalidate your pull request 18 | > There are a lot of good websites that you can research before opening a request instead of just writing "cool" or "Hello World" 19 | 5. Pat yourself on the back and wait for your `pull request` to be reviewed and merged. 20 | 21 | ## Resources 22 | 23 | - [How to Start with Hacktoberfest](https://www.youtube.com/watch?v=4RvIFvmZA3o) 24 | - [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/) 25 | - [Using Pull Requests](https://help.github.com/articles/about-pull-requests/) 26 | - [GitHub Help](https://help.github.com) 27 | - [Your first time with git and github](https://kbroman.org/github_tutorial/pages/first_time.html) 28 | -------------------------------------------------------------------------------- /Leetcode 2. Add Two Numbers: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 14 | ListNode *temp = NULL; 15 | ListNode **cur = &temp; 16 | bool carry = false; 17 | while(l1 || l2 || carry){ 18 | int val = carry; 19 | if(l1){ 20 | val += l1->val; 21 | l1 = l1->next; 22 | } 23 | if(l2){ 24 | val += l2->val; 25 | l2 = l2->next; 26 | } 27 | carry = val >= 10; 28 | *cur = new ListNode(val%10); 29 | cur = &((*cur)->next); 30 | } 31 | return temp; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /QuickSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int n; 4 | 5 | int split(int Arr[],int lower,int upper); 6 | 7 | void Quick_Sort(int Arr[],int lower,int upper) 8 | { 9 | int i; 10 | if(upper>lower) 11 | { 12 | i=split(Arr,lower,upper); 13 | 14 | Quick_Sort(Arr,lower,i-1); 15 | Quick_Sort(Arr,i+1,upper); 16 | } 17 | } 18 | 19 | int split(int Arr[],int lower,int upper) 20 | { 21 | int i,x,y,t,k,flag=0; 22 | x=lower+1; 23 | y=upper; 24 | i=Arr[lower]; 25 | while(y>=x) 26 | { 27 | while(Arr[x]i) 30 | y--; 31 | if(y>x) 32 | { 33 | t=Arr[x]; 34 | Arr[x]=Arr[y]; 35 | Arr[y]=t; 36 | } 37 | } 38 | t=Arr[lower]; 39 | Arr[lower]=Arr[y]; 40 | Arr[y]=t; 41 | /*flag++; 42 | printf("\nThe array after iteration %d is: ",flag); 43 | for(k=0;k 2 | #include 3 | int main() 4 | { 5 | int num,ld,count,sum,i,n; 6 | scanf("%d",&n); 7 | for(i=1;i<=n;i++) 8 | { 9 | sum = 0; 10 | num = i; 11 | while (n != 0) 12 | { 13 | n = n / 10; 14 | count=count++; 15 | } 16 | 17 | while(num > 0) 18 | { 19 | ld = num % 10; 20 | sum = sum + pow(ld, count); 21 | num = num / 10; 22 | } 23 | if(i == sum) 24 | { 25 | printf("%d ", i); 26 | } 27 | 28 | } 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /c/ArraySort.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int a[100],b[100],c[100],d[100],i,n,j,k=0,temp,s=0; 5 | printf("Enter Size="); 6 | scanf("%d",&n); 7 | for(i=0;ic[j]) 21 | { 22 | temp=c[i]; 23 | c[i]=c[j]; 24 | c[j]=temp; 25 | } 26 | } 27 | } 28 | for(i=0;i<2*n;i++) 29 | { 30 | s=0; 31 | for(j=i;j<2*n;j++) 32 | { 33 | if(c[i]==c[j]) 34 | { 35 | s=s+1; 36 | } 37 | } 38 | if(s==1) 39 | { 40 | d[k]=c[i]; 41 | k++; 42 | } 43 | } 44 | printf("%d\n",k); 45 | for (i=0;i 2 | int main() 3 | { 4 | int c, first, last, middle, n, search, array[100]; 5 | 6 | printf("Enter number of elements\n"); 7 | scanf("%d", &n); 8 | 9 | printf("Enter %d integers\n", n); 10 | 11 | for (c = 0; c < n; c++) 12 | scanf("%d", &array[c]); 13 | 14 | printf("Enter value to find\n"); 15 | scanf("%d", &search); 16 | 17 | first = 0; 18 | last = n - 1; 19 | middle = (first+last)/2; 20 | 21 | while (first <= last) { 22 | if (array[middle] < search) 23 | first = middle + 1; 24 | else if (array[middle] == search) { 25 | printf("%d found at location %d.\n", search, middle+1); 26 | break; 27 | } 28 | else 29 | last = middle - 1; 30 | 31 | middle = (first + last)/2; 32 | } 33 | if (first > last) 34 | printf("Not found! %d isn't present in the list.\n", search); 35 | 36 | return 0; 37 | } 38 | 39 | /* 40 | Output 41 | 42 | Enter number of elements 43 | 5 44 | Enter 5 integers 45 | 65 46 | 85 47 | 1 48 | 6 49 | 9 50 | Enter value to find 51 | 6 52 | 6 found at location 4. 53 | */ 54 | -------------------------------------------------------------------------------- /c/Bogo Sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | bool check_sorted(int *a, int n) 6 | { 7 | while (--n >= 1) 8 | { 9 | if (a[n] < a[n - 1]) 10 | return false; 11 | } 12 | return true; 13 | } 14 | 15 | void shuffle(int *a, int n) 16 | { 17 | int i, t, r; 18 | for (i = 0; i < n; i++) 19 | { 20 | t = a[i]; 21 | r = rand() % n; 22 | a[i] = a[r]; 23 | a[r] = t; 24 | } 25 | } 26 | 27 | void sort(int *a, int n) 28 | { 29 | while (!check_sorted(a, n)) shuffle(a, n); 30 | } 31 | 32 | int main() 33 | { 34 | int numbers[6]; 35 | int i; 36 | printf("Enter 6 numbers unsorted \n\n"); 37 | for (i = 0; i < 6; i++) 38 | { 39 | scanf("%d", &numbers[i]); 40 | } 41 | sort(numbers, 6); 42 | for (i = 0; i < 6; i++) printf("%d ", numbers[i]); 43 | printf("\n"); 44 | } 45 | -------------------------------------------------------------------------------- /c/Celsius_to_Fahrenheit.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | float celsius, fahrenheit; 5 | printf("Enter temperature in Celsius: "); 6 | scanf("%f", &celsius); 7 | //celsius to fahrenheit conversion formula 8 | fahrenheit = (celsius * 9 / 5) + 32; 9 | printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit); 10 | return 0; 11 | } 12 | 13 | /* 14 | Output 15 | Enter temperature in Celsius: 10 16 | 10.00 Celsius = 50.00 Fahrenheit 17 | */ 18 | -------------------------------------------------------------------------------- /c/CircularQueue.c: -------------------------------------------------------------------------------- 1 | // Circular Queue Implementation in C 2 | #include 3 | #define Max_Size 5 4 | 5 | int queue[Max_Size]; 6 | int front=-1,rear=-1; 7 | 8 | // Function to check if queue is full 9 | int isFull(){ 10 | if((front==rear+1)||(front==0&&rear==Max_Size)) 11 | return 1; 12 | else 13 | return 0; 14 | } 15 | 16 | // Function to check if queue is empty 17 | int isEmpty(){ 18 | if(front==-1) 19 | return 1; 20 | else 21 | return 0; 22 | } 23 | 24 | // Function to enqueue a element in a queue 25 | void Enqueue(int item){ 26 | if(isFull()) 27 | printf("\nQueue is Full...\n"); 28 | else{ 29 | if(front==-1) 30 | front=0; 31 | rear=(rear+1)%Max_Size; 32 | queue[rear]=item; 33 | printf("\n%d has been enqueued..\n",item); 34 | } 35 | } 36 | 37 | // Function to Dequeue a element from a queue 38 | int Dequeue(){ 39 | int item; 40 | if(isEmpty()){ 41 | printf("\n Queue is Empty..\n"); 42 | return -1; 43 | } 44 | else{ 45 | item=queue[front]; 46 | 47 | // If queue has only one elment 48 | if(front==rear){ 49 | front=-1; 50 | rear=-1; 51 | } 52 | 53 | else 54 | front=(front+1)%Max_Size; 55 | printf("\n%d has been Dequeued from the queue...\n",item); 56 | return item; 57 | } 58 | } 59 | 60 | // Function to Display the Queue 61 | void display(){ 62 | int i; 63 | if(isEmpty()) 64 | printf("\nQueue is Empty...\n"); 65 | else{ 66 | printf("\nThe Queue is:\n"); 67 | for(i=front;i!=rear;i=(i+1)%Max_Size){ 68 | printf("%d\t",queue[i]); 69 | } 70 | printf("%d ",queue[i]); 71 | } 72 | } 73 | 74 | int main(){ 75 | Dequeue();//Queue is Empty, so it fails 76 | 77 | //5 elements are enqueued 78 | Enqueue(1); 79 | Enqueue(2); 80 | Enqueue(3); 81 | Enqueue(4); 82 | Enqueue(5); 83 | 84 | Enqueue(6);//This is not possible because Queue is full 85 | 86 | display(); 87 | 88 | Dequeue(); 89 | display(); 90 | 91 | Enqueue(7); 92 | display(); 93 | 94 | Enqueue(8);//This is not possible because Queue is full 95 | 96 | return 0; 97 | } -------------------------------------------------------------------------------- /c/EiffelTowerProg.c: -------------------------------------------------------------------------------- 1 | main() 2 | { 3 | int n,m,i,j,k; 4 | scanf("%d",&n); 5 | for(i=1;i<=n;i++) 6 | { 7 | for(m=n;m>=i;m--) 8 | { 9 | for(j=n;j>i;j--) 10 | printf(" "); 11 | for(k=1;k<=2*i-1;k++) 12 | printf("%d",i); 13 | printf("\n"); 14 | } 15 | 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /c/Factorial.c: -------------------------------------------------------------------------------- 1 | void fact(int n) 2 | { 3 | int i,f=1; 4 | for(i=1;i<=n;i++) 5 | f=f*i; 6 | printf("Factorial=%d",f); 7 | } 8 | main() 9 | { 10 | int n; 11 | scanf("%d",&n); 12 | fact(n); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /c/FibonacciSeries.c: -------------------------------------------------------------------------------- 1 | #include 2 | void printFibonacci(int n){ 3 | static int n1=0,n2=1,n3; 4 | if(n>0){ 5 | n3 = n1 + n2; 6 | n1 = n2; 7 | n2 = n3; 8 | printf("%d ",n3); 9 | printFibonacci(n-1); 10 | } 11 | } 12 | int main(){ 13 | int n; 14 | printf("Enter the number of elements: "); 15 | scanf("%d",&n); 16 | printf("Fibonacci Series: "); 17 | printf("%d %d ",0,1); 18 | printFibonacci(n-2);//n-2 because 2 numbers are already printed 19 | return 0; 20 | } 21 | /* 22 | Output: 23 | Enter the number of elements:15 24 | 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 25 | The time complexity of the above code is T(2^N), i.e., exponential. 26 | The Space complexity of the above code is O(N) for a recursive series. 27 | */ 28 | -------------------------------------------------------------------------------- /c/Sorting/bubbleSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void show(int arr[]) 4 | { 5 | for (int i = 0; i < 10; i++) 6 | { 7 | printf(" %d ",arr[i]); 8 | } 9 | 10 | printf("\n"); 11 | } 12 | 13 | int main() 14 | { 15 | 16 | int arr[] = {1, 12, 23, 111134, 45, 56, 67, 78, 89, 910}; 17 | 18 | printf("array before sorting : "); 19 | show(arr); 20 | int temp; 21 | for (int i = 0; i < 10; i++) 22 | { 23 | for (int j = 0; j < 9; j++) 24 | { 25 | if (arr[j]>arr[j+1]) 26 | { 27 | temp = arr[j+1]; 28 | arr[j+1]=arr[j]; 29 | arr[j]=temp; 30 | } 31 | 32 | } 33 | 34 | } 35 | printf("array after sorting : "); 36 | show(arr); 37 | } -------------------------------------------------------------------------------- /c/Sorting/countingSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(int argc, char const *argv[]) 3 | { 4 | 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /c/Sorting/insertionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | void show(int arr[]){for (int i = 0; i < 10; i++) 3 | { 4 | printf(" %d ",arr[i]); 5 | } 6 | } 7 | void main(){ 8 | 9 | int arr[10]={23,43,37,5,55,5,3,54,3,543}; 10 | printf("before sorting elements in array are "); 11 | show(arr); 12 | int key,j; 13 | 14 | for (int i = 1; i < 10; i++) 15 | { key=arr[i]; 16 | j=i-1; 17 | while (j>=0 && key 2 | int sum(int n); 3 | 4 | int main() { 5 | int number, result; 6 | 7 | printf("Enter a positive integer: "); 8 | scanf("%d", &number); 9 | 10 | result = sum(number); 11 | 12 | printf("sum = %d", result); 13 | return 0; 14 | } 15 | 16 | int sum(int n) { 17 | if (n != 0) 18 | // sum() function calls itself 19 | return n + sum(n-1); 20 | else 21 | return n; 22 | } 23 | 24 | 25 | 26 | // Enter a positive integer:3 27 | // output : sum = 6 28 | -------------------------------------------------------------------------------- /c/bankers_algorithm.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10]; 5 | int p, r, i, j, process, count; 6 | count = 0; 7 | 8 | // read no of processes(p0,p1,p2,p3) 9 | printf("Enter the no of processes : "); 10 | scanf("%d", &p); 11 | 12 | //read no of resources (A,B,C,D) 13 | printf("\n\nEnter the no of resources : "); 14 | scanf("%d", &r); 15 | 16 | //read alloc (for p processes and r resources) 17 | printf("\n\nEnter the allocation for each process : "); 18 | for(i = 0; i < p; i++) 19 | { 20 | printf("\nFor process %d : ",i + 1); 21 | for(j = 0; j < r; j++) 22 | scanf("%d", &alloc[i][j]); 23 | } 24 | 25 | //read max (for p processes and r resources) 26 | printf("\n\nEnter the Max Matrix for each process : "); 27 | for(i = 0; i < p; i++) 28 | { 29 | printf("\nFor process %d : ", i + 1); 30 | for(j = 0; j < r; j++) 31 | scanf("%d", &Max[i][j]); 32 | } 33 | 34 | //read avail (for p processes and r resources) 35 | printf("\n\nEnter the Available Resources : "); 36 | for(i = 0; i < r; i++) 37 | scanf("%d", &avail[i]); 38 | 39 | //calc need (for p processes and r resources) 40 | for(i = 0; i < p; i++) 41 | for(j = 0; j < r; j++) 42 | need[i][j] = Max[i][j] - alloc[i][j]; 43 | 44 | // set all completed proccesses as 0 45 | for(i = 0; i< p; i++) 46 | completed[i] = 0; 47 | 48 | // do while count !=process & process!=-1 49 | do 50 | { 51 | // process no i will be assigned to 'process' variable .So initialized as -1 52 | process = -1; 53 | 54 | // for each process p , if completion of p is 0 ,set process to i 55 | for(i = 0; i < p; i++) 56 | { 57 | if(completed[i] == 0) 58 | { 59 | process = i ; 60 | 61 | //for each resource r , if need > avail (process not selected),set process -1 62 | for(j = 0; j < r; j++) 63 | { 64 | 65 | if(need[i][j] > avail[j]) 66 | { 67 | process = -1; 68 | break; 69 | } 70 | } 71 | } 72 | // break loop if selected (process!=-1) 73 | if(process != -1) 74 | break; 75 | } 76 | 77 | // if selected 78 | if(process != -1) 79 | { 80 | //print 'process +1 selected' (if p=3 ; i=0,1,2,3 ; if i = 2 selected ,3rd process is selected) 81 | printf("\nProcess %d runs to completion!", process + 1); 82 | 83 | // process stored to safe sequence matrix 84 | safeSequence[count] = process + 1; 85 | count++; 86 | 87 | //for each resource r , 88 | for(j = 0; j < r; j++) 89 | { 90 | //new alloc = alloc +avail (not i but selected i which is 'process') 91 | avail[j] += alloc[process][j]; 92 | // alloc & max = 0 ,completion of 'process' =1 93 | alloc[process][j] = 0; 94 | Max[process][j] = 0; 95 | completed[process] = 1; 96 | } 97 | } 98 | }while(count != p && process != -1); 99 | 100 | // if all process selected ,ie count ==p 101 | if(count == p) 102 | { 103 | // print safe state 104 | printf("\nThe system is in a safe state!!\n"); 105 | // print safe sequence [i] 106 | printf("Safe Sequence : < "); 107 | for( i = 0; i < p; i++) 108 | printf("%d ", safeSequence[i]); 109 | printf(">\n"); 110 | } 111 | 112 | // else print unsafe 113 | else 114 | printf("\nThe system is in an unsafe state!!"); 115 | } -------------------------------------------------------------------------------- /c/binarysearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int binarySearch(int arr[], int x, int low, int high) { 4 | if (high >= low) { 5 | int mid = low + (high - low) / 2; 6 | 7 | // If found at mid, then return it 8 | if (arr[mid] == x) 9 | return mid; 10 | 11 | // Search the left half 12 | if (arr[mid] > x) 13 | return binarySearch(arr, x, low, mid - 1); 14 | 15 | // Search the right half 16 | return binarySearch(arr, x, mid + 1, high); 17 | } 18 | 19 | return -1; 20 | } 21 | 22 | int main(void) { 23 | int array[] = {3, 4, 6, 9, 12, 15, 21}; 24 | int n = sizeof(array) / sizeof(array[0]); 25 | int x = 4; 26 | int final = binarySearch(array, x, 0, n - 1); 27 | if (final == -1) 28 | printf("Not found"); 29 | else 30 | printf("Element founded at index= %d", final); 31 | } 32 | -------------------------------------------------------------------------------- /c/circularlinkedlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct n 4 | { 5 | int data; 6 | struct n *next; 7 | } node; 8 | 9 | void printlist(node *head){ 10 | if(head==NULL) return; 11 | node *p; 12 | p=head; 13 | do{ 14 | printf("%d ",p->data); 15 | p=p->next; 16 | } while(p!=head); 17 | 18 | } 19 | 20 | void insertbegin(node *head,int num){ 21 | 22 | node *new; 23 | new=(node*)malloc(sizeof(node)); 24 | new->data=num; 25 | new->next=head->next; 26 | head->next=new; 27 | //swap head and new 28 | new->data=head->data; 29 | head->data=num; 30 | 31 | } 32 | 33 | void insertend(node *head,int num){ 34 | node *temp3; 35 | temp3=(node*) malloc(sizeof(node)); 36 | if(head==NULL){ 37 | temp3->data=num; 38 | temp3->next=temp3; 39 | temp3=head; 40 | } 41 | else{ 42 | node *walker; 43 | walker =head; 44 | while(walker->next!=head){ 45 | walker=walker->next; 46 | } 47 | temp3->data=num; 48 | temp3->next=NULL; 49 | walker->next=temp3; 50 | temp3->next=head; 51 | 52 | } 53 | printlist(head); 54 | } 55 | 56 | void deletebegin(node** head){ 57 | node* te=*head; 58 | node* temp3=*head; 59 | 60 | while(te->next!=*head){ 61 | te=te->next; 62 | } 63 | te->next=(*head)->next; 64 | *head=(*head)->next; 65 | // free(temp3); 66 | printlist(*head); 67 | } 68 | 69 | int main() 70 | { 71 | printf("Enter the length of the linked list:\n"); 72 | int n; 73 | scanf("%d", &n); 74 | printf("Enter the element of circular linked list:\n"); 75 | int x; 76 | scanf("%d", &x); 77 | node *newnode, *head,*temp; 78 | newnode = (node *)malloc(sizeof(node)); 79 | if (newnode != NULL) 80 | { 81 | newnode->data = x; 82 | newnode->next = NULL; 83 | } 84 | head = newnode; 85 | temp=newnode; 86 | for (int i = 1; i < n; i++) 87 | { 88 | printf("Enter the element of circular linked list:"); 89 | int x; 90 | scanf("%d", &x); 91 | newnode = (node *)malloc(sizeof(node)); 92 | if (newnode != NULL) 93 | { 94 | newnode->data = x; 95 | newnode->next = NULL; 96 | temp->next=newnode; 97 | temp=temp->next; 98 | } 99 | } 100 | temp->next=head; 101 | printlist(head); 102 | printf("\nEnter the element to be inserted at the beginning: \n"); 103 | scanf("%d",&x); 104 | insertbegin(head,x); 105 | printlist(head); 106 | printf("\n"); 107 | printf("Enter the element to be inserted at the end: \n"); 108 | scanf("%d",&x); 109 | 110 | insertend(head,x); 111 | 112 | printf("\n"); 113 | 114 | printf("Deletion at the beginning\n"); 115 | deletebegin(&head); 116 | 117 | printf("\n"); 118 | 119 | return 0; 120 | } -------------------------------------------------------------------------------- /c/factorial.c: -------------------------------------------------------------------------------- 1 | #include 2 | int fat (n) { 3 | if ((n==1) || (n==0)){ 4 | return 1; 5 | } else{ 6 | return fat(n-1)*n; 7 | } 8 | } 9 | int main () { 10 | int n; 11 | printf("Enter an integer: "); 12 | scanf("%d",&n); 13 | printf("%d! = %d \n",n,fat (n)); 14 | } 15 | -------------------------------------------------------------------------------- /c/priority.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define max 10 3 | int main() 4 | { 5 | int i,j,n,bt[max],p[max],wt[max],tat[max],pr[max],total=0,pos,temp; 6 | float avg_wt,avg_tat; 7 | printf("Enter Total Number of Process:"); 8 | scanf("%d",&n); 9 | printf("\nEnter Burst Time and Priority For "); 10 | for(i=0;i 2 | int get_max (int a[], int n){ 3 | int max = a[0]; 4 | for (int i = 1; i < n; i++) 5 | if (a[i] > max) 6 | max = a[i]; 7 | return max; 8 | } 9 | void radix_sort (int a[], int n){ 10 | int bucket[10][10], bucket_cnt[10]; 11 | int i, j, k, r, NOP = 0, divisor = 1, lar, pass; 12 | lar = get_max (a, n); 13 | while (lar > 0){ 14 | NOP++; 15 | lar /= 10; 16 | } 17 | for (pass = 0; pass < NOP; pass++){ 18 | for (i = 0; i < 10; i++){ 19 | bucket_cnt[i] = 0; 20 | } 21 | for (i = 0; i < n; i++){ 22 | r = (a[i] / divisor) % 10; 23 | bucket[r][bucket_cnt[r]] = a[i]; 24 | bucket_cnt[r] += 1; 25 | } 26 | i = 0; 27 | for (k = 0; k < 10; k++){ 28 | for (j = 0; j < bucket_cnt[k]; j++){ 29 | a[i] = bucket[k][j]; 30 | i++; 31 | } 32 | } 33 | divisor *= 10; 34 | printf ("After pass %d : ", pass + 1); 35 | for (i = 0; i < n; i++) 36 | printf ("%d ", a[i]); 37 | printf ("\n"); 38 | } 39 | } 40 | int main (){ 41 | int i, n, a[10]; 42 | printf ("Enter the number of items to be sorted: "); 43 | scanf ("%d", &n); 44 | printf ("Enter items: "); 45 | for (i = 0; i < n; i++){ 46 | scanf ("%d", &a[i]); 47 | } 48 | radix_sort (a, n); 49 | printf ("Sorted items : "); 50 | for (i = 0; i < n; i++) 51 | printf ("%d ", a[i]); 52 | printf ("\n"); 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /c/sorting/Heapsort.c: -------------------------------------------------------------------------------- 1 | //samir-27 2 | 3 | #include 4 | 5 | void swap(int *a, int *b) 6 | { 7 | int temp = *a; 8 | *a = *b; 9 | *b = temp; 10 | } 11 | 12 | void heapify(int arr[], int n, int i) 13 | { 14 | 15 | int largest = i; 16 | int left = 2 * i + 1; 17 | int right = 2 * i + 2; 18 | 19 | if (left < n && arr[left] > arr[largest]) 20 | largest = left; 21 | 22 | if (right < n && arr[right] > arr[largest]) 23 | largest = right; 24 | 25 | if (largest != i) { 26 | swap(&arr[i], &arr[largest]); 27 | heapify(arr, n, largest); 28 | } 29 | } 30 | 31 | void heapSort(int arr[], int n) { 32 | 33 | for (int i = n / 2 - 1; i >= 0; i--) 34 | heapify(arr, n, i); 35 | 36 | for (int i = n - 1; i >= 0; i--) { 37 | swap(&arr[0], &arr[i]); 38 | 39 | 40 | heapify(arr, i, 0); 41 | } 42 | } 43 | 44 | void printArray(int arr[], int n) { 45 | for (int i = 0; i < n; ++i) 46 | printf("%d ", arr[i]); 47 | printf("\n"); 48 | } 49 | 50 | int main() { 51 | int arr[] = {1, 12, 9, 5, 6, 10}; 52 | int n = sizeof(arr) / sizeof(arr[0]); 53 | 54 | heapSort(arr, n); 55 | 56 | printf("Sorted array is \n"); 57 | printArray(arr, n); 58 | } 59 | -------------------------------------------------------------------------------- /c/sorting/SortStringLexicographically.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | /* 9 | bool my_comparator(string a, string b) 10 | { 11 | 12 | if(a.length() == b.length()) 13 | { 14 | // lexicographically a return krro --- chota 15 | return a < b; 16 | } 17 | 18 | 19 | // badi length return krro 20 | return a.length() > b.length(); 21 | 22 | } 23 | */ 24 | 25 | bool my_comparator(string a, string b) 26 | { 27 | if(a.compare(0,b.length(),b) == 0 || b.compare(0,a.length(),a) == 0) 28 | { 29 | return a.length() > b.length(); 30 | } 31 | return a < b; 32 | } 33 | 34 | 35 | 36 | int main() 37 | { 38 | int n; 39 | cin >> n; 40 | 41 | vector v; 42 | 43 | cin.get(); 44 | 45 | string s1; 46 | getline(cin,s1); 47 | 48 | v.push_back(s1); 49 | 50 | 51 | 52 | for(int i = 1; i < n; i++) 53 | { 54 | string s2; 55 | getline(cin,s2); 56 | 57 | v.push_back(s2); 58 | } 59 | 60 | 61 | sort(v.begin(), v.end(), my_comparator); 62 | 63 | for(int i = 0; i < n; i++) 64 | { 65 | cout << v[i] << endl; 66 | } 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /c/sorting/add_number.c: -------------------------------------------------------------------------------- 1 | /* 2 | Rahul Kumar GUpta 3 | Github = https://github.com/rahulshivan05 4 | */ 5 | 6 | #include 7 | 8 | int main(int argc, char const *argv[]) 9 | { 10 | /* code */ 11 | 12 | float a, b; 13 | printf("Enter number a \n"); 14 | scanf("%d", &a); 15 | 16 | printf("Enter number b\n"); 17 | scanf("%d", &b); 18 | 19 | printf("The sum of number is %d\n", a + b); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /c/sorting/bubble_sort.c: -------------------------------------------------------------------------------- 1 | // C program for implementation of Bubble sort 2 | #include 3 | 4 | void swap(int* xp, int* yp) 5 | { 6 | int temp = *xp; 7 | *xp = *yp; 8 | *yp = temp; 9 | } 10 | 11 | // A function to implement bubble sort 12 | void bubbleSort(int arr[], int n) 13 | { 14 | int i, j; 15 | for (i = 0; i < n - 1; i++) 16 | 17 | // Last i elements are already in place 18 | for (j = 0; j < n - i - 1; j++) 19 | if (arr[j] > arr[j + 1]) 20 | swap(&arr[j], &arr[j + 1]); 21 | } 22 | 23 | /* Function to print an array */ 24 | void printArray(int arr[], int size) 25 | { 26 | int i; 27 | for (i = 0; i < size; i++) 28 | printf("%d ", arr[i]); 29 | printf("\n"); 30 | } 31 | 32 | // Driver program to test above functions 33 | int main() 34 | { 35 | int arr[] = { 64, 34, 25, 12, 22, 11, 90 }; 36 | int n = sizeof(arr) / sizeof(arr[0]); 37 | bubbleSort(arr, n); 38 | printf("Sorted array: \n"); 39 | printArray(arr, n); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /c/sorting/countSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int FindMax(int *arr, int n) 4 | { 5 | int max = 0; 6 | for (int i = 0; i < n; i++) 7 | { 8 | if (arr[i] > max) 9 | { 10 | max = arr[i]; 11 | } 12 | } 13 | return max + 1; 14 | } 15 | 16 | void printArray(int *A, int n) 17 | { 18 | for (int i = 0; i < n; i++) 19 | { 20 | printf("%d ", A[i]); 21 | } 22 | printf("\n"); 23 | } 24 | 25 | void CountSort(int *A, int n) 26 | { 27 | int Max = FindMax(A, n); 28 | // printf("%d\n", Max); 29 | 30 | // ---------Created new extra array and initialize with all blocks with value 0---------- 31 | int *AuxiArr = (int *)malloc(Max * sizeof(int)); 32 | for (int i = 0; i < Max; i++) 33 | { 34 | AuxiArr[i] = 0; 35 | } 36 | // printArray(AuxiArr, Max); // To do for his happen or not 37 | 38 | // -----travasing and increamenting value by 1----------- 39 | for (int j = 0; j < n; j++) 40 | { 41 | if (A[j]) 42 | { 43 | AuxiArr[A[j]]++; 44 | } 45 | } 46 | // printArray(AuxiArr, Max); 47 | 48 | // --------------time for sorting data in main array----------- 49 | int p = 0; // p is for stroing index at A (in given array) 50 | for (int k = 0; k < Max;) 51 | { 52 | if (AuxiArr[k] == 0) 53 | { 54 | k++; // if value in auxi array is 0 then go for next index 55 | } 56 | else 57 | { 58 | // if not then push the index value in Main value and increament main array pointer 59 | A[p] = k; 60 | p++; 61 | AuxiArr[k]--; // and decreament value by 1 62 | } 63 | } 64 | // printArray(A, n); 65 | free(AuxiArr); 66 | } 67 | 68 | int main(int argc, char const *argv[]) 69 | { 70 | int Array[] = {3, 1, 9, 7, 1, 2, 4}; 71 | int sizeOfArr = sizeof(Array) / sizeof(Array[0]); 72 | 73 | printf("Before Sorting\n"); 74 | printArray(Array, sizeOfArr); 75 | printf("After Sorting\n"); 76 | CountSort(Array, sizeOfArr); 77 | printArray(Array, sizeOfArr); 78 | return 0; 79 | } -------------------------------------------------------------------------------- /c/sorting/insertion_sort.c: -------------------------------------------------------------------------------- 1 | // C program for insertion sort 2 | #include 3 | #include 4 | 5 | /* Function to sort an array using insertion sort*/ 6 | void insertionSort(int arr[], int n) 7 | { 8 | int i, key, j; 9 | for (i = 1; i < n; i++) { 10 | key = arr[i]; 11 | j = i - 1; 12 | 13 | /* Move elements of arr[0..i-1], that are 14 | greater than key, to one position ahead 15 | of their current position */ 16 | while (j >= 0 && arr[j] > key) { 17 | arr[j + 1] = arr[j]; 18 | j = j - 1; 19 | } 20 | arr[j + 1] = key; 21 | } 22 | } 23 | 24 | // A utility function to print an array of size n 25 | void printArray(int arr[], int n) 26 | { 27 | int i; 28 | for (i = 0; i < n; i++) 29 | printf("%d ", arr[i]); 30 | printf("\n"); 31 | } 32 | 33 | /* Driver program to test insertion sort */ 34 | int main() 35 | { 36 | int arr[] = { 12, 11, 13, 5, 6 }; 37 | int n = sizeof(arr) / sizeof(arr[0]); 38 | 39 | insertionSort(arr, n); 40 | printArray(arr, n); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /c/sorting/merge_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void merge(int arr[], int l, int m, int r) 5 | { 6 | int i, j, k; 7 | int n1 = m - l + 1; 8 | int n2 = r - m; 9 | 10 | /* create temp arrays */ 11 | int L[n1], R[n2]; 12 | 13 | /* Copy data to temp arrays L[] and R[] */ 14 | for (i = 0; i < n1; i++) 15 | L[i] = arr[l + i]; 16 | for (j = 0; j < n2; j++) 17 | R[j] = arr[m + 1 + j]; 18 | 19 | /* Merge the temp arrays back into arr[l..r]*/ 20 | i = 0; // Initial index of first subarray 21 | j = 0; // Initial index of second subarray 22 | k = l; // Initial index of merged subarray 23 | while (i < n1 && j < n2) { 24 | if (L[i] <= R[j]) { 25 | arr[k] = L[i]; 26 | i++; 27 | } 28 | else { 29 | arr[k] = R[j]; 30 | j++; 31 | } 32 | k++; 33 | } 34 | 35 | /* Copy the remaining elements of L[], if there 36 | are any */ 37 | while (i < n1) { 38 | arr[k] = L[i]; 39 | i++; 40 | k++; 41 | } 42 | 43 | /* Copy the remaining elements of R[], if there 44 | are any */ 45 | while (j < n2) { 46 | arr[k] = R[j]; 47 | j++; 48 | k++; 49 | } 50 | } 51 | 52 | /* l is for left index and r is right index of the 53 | sub-array of arr to be sorted */ 54 | void mergeSort(int arr[], int l, int r) 55 | { 56 | if (l < r) { 57 | // Same as (l+r)/2, but avoids overflow for 58 | // large l and h 59 | int m = l + (r - l) / 2; 60 | 61 | // Sort first and second halves 62 | mergeSort(arr, l, m); 63 | mergeSort(arr, m + 1, r); 64 | 65 | merge(arr, l, m, r); 66 | } 67 | } 68 | 69 | /* UTILITY FUNCTIONS */ 70 | /* Function to print an array */ 71 | void printArray(int A[], int size) 72 | { 73 | int i; 74 | for (i = 0; i < size; i++) 75 | printf("%d ", A[i]); 76 | printf("\n"); 77 | } 78 | 79 | /* Driver program to test above functions */ 80 | int main() 81 | { 82 | int arr[] = { 12, 11, 13, 5, 6, 7 }; 83 | int arr_size = sizeof(arr) / sizeof(arr[0]); 84 | 85 | printf("Given array is \n"); 86 | printArray(arr, arr_size); 87 | 88 | mergeSort(arr, 0, arr_size - 1); 89 | 90 | printf("\nSorted array is \n"); 91 | printArray(arr, arr_size); 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /c/sorting/quicksort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void printArray(int *A, int n) 4 | { 5 | for (int i = 0; i < n; i++) 6 | { 7 | printf("%d ", A[i]); 8 | } 9 | printf("\n"); 10 | } 11 | 12 | int partition(int A[], int low, int high) 13 | { 14 | int pivot = A[low]; 15 | int i = low + 1; 16 | int j = high; 17 | int temp; 18 | 19 | do 20 | { 21 | while (A[i] <= pivot) 22 | { 23 | i++; 24 | } 25 | 26 | while (A[j] > pivot) 27 | { 28 | j--; 29 | } 30 | 31 | if (i < j) 32 | { 33 | temp = A[i]; 34 | A[i] = A[j]; 35 | A[j] = temp; 36 | } 37 | } while (i < j); 38 | 39 | // Swap A[low] and A[j] 40 | temp = A[low]; 41 | A[low] = A[j]; 42 | A[j] = temp; 43 | return j; 44 | } 45 | 46 | void quickSort(int A[], int low, int high) 47 | { 48 | int partitionIndex; // Index of pivot after partition 49 | 50 | if (low < high) 51 | { 52 | partitionIndex = partition(A, low, high); 53 | printArray(A, 7); 54 | quickSort(A, low, partitionIndex - 1); // sort left subarray 55 | quickSort(A, partitionIndex + 1, high); // sort right subarray 56 | } 57 | } 58 | 59 | int main() 60 | { 61 | //int A[] = {3, 5, 2, 13, 12, 3, 2, 13, 45}; 62 | int A[] = {9, 4, 4, 8, 7, 5, 6}; 63 | // 3, 5, 2, 13, 12, 3, 2, 13, 45 64 | // 3, 2, 2, 13i, 12, 3j, 5, 13, 45 65 | // 3, 2, 2, 3j, 12i, 13, 5, 13, 45 --> first call to partition returns 3 66 | int n = 9; 67 | n =7; 68 | printf("Before Sorting..\n"); 69 | printArray(A, n); 70 | printf("In Sorting..\n"); 71 | quickSort(A, 0, n - 1); 72 | printf("After Sorting..\n"); 73 | printArray(A, n); 74 | 75 | // printArray(A, n); 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /c/sorting/selection_sort.c: -------------------------------------------------------------------------------- 1 | // C program for implementation of selection sort 2 | #include 3 | 4 | void swap(int *xp, int *yp) 5 | { 6 | int temp = *xp; 7 | *xp = *yp; 8 | *yp = temp; 9 | } 10 | 11 | void selectionSort(int arr[], int n) 12 | { 13 | int i, j, min_idx; 14 | 15 | // One by one move boundary of unsorted subarray 16 | for (i = 0; i < n-1; i++) 17 | { 18 | // Find the minimum element in unsorted array 19 | min_idx = i; 20 | for (j = i+1; j < n; j++) 21 | if (arr[j] < arr[min_idx]) 22 | min_idx = j; 23 | 24 | // Swap the found minimum element with the first element 25 | if(min_idx != i) 26 | swap(&arr[min_idx], &arr[i]); 27 | } 28 | } 29 | 30 | /* Function to print an array */ 31 | void printArray(int arr[], int size) 32 | { 33 | int i; 34 | for (i=0; i < size; i++) 35 | printf("%d ", arr[i]); 36 | printf("\n"); 37 | } 38 | 39 | // Driver program to test above functions 40 | int main() 41 | { 42 | int arr[] = {64, 25, 12, 22, 11}; 43 | int n = sizeof(arr)/sizeof(arr[0]); 44 | selectionSort(arr, n); 45 | printf("Sorted array: \n"); 46 | printArray(arr, n); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /floyd warshall algorithm dp: -------------------------------------------------------------------------------- 1 | // C++ Program for Floyd Warshall Algorithm 2 | #include 3 | using namespace std; 4 | // Number of vertices in the graph 5 | #define V 4 6 | /* Define Infinite as a large enough value.This value will be used for vertices not connected to each other */ 7 | #define INF 99999 8 | // A function to print the solution matrix 9 | void printSolution(int dist[][V]); 10 | 11 | 12 | 13 | 14 | // Solves the all-pairs shortest path - problem using Floyd Warshall algorithm 15 | void floydWarshall(int graph[][V]) 16 | { 17 | /* dist[][] will be the output matrix that will finally have the shortest distances between every pair of vertices */ 18 | int dist[V][V], i, j, k; 19 | /* Initialize the solution matrix same as input graph matrix. Or we can say the initial values of shortest distances are based on shortest paths considering 20 | no intermediate vertex. */ 21 | for (i = 0; i < V; i++) 22 | for (j = 0; j < V; j++) 23 | dist[i][j] = graph[i][j]; 24 | 25 | /* Add all vertices one by one to the set of intermediate vertices. ---> Before start of an iteration, 26 | we have shortest distances between all pairs of vertices such that the shortest distances consider only the vertices in 27 | set {0, 1, 2, .. k-1} as intermediate vertices. ----> After the end of an iteration, 28 | vertex no. k is added to the set of intermediate vertices and the set becomes {0, 1, 2, .. k} */ 29 | 30 | 31 | 32 | 33 | 34 | for (k = 0; k < V; k++) { 35 | // Pick all vertices as source one by one 36 | for (i = 0; i < V; i++) { 37 | // Pick all vertices as destination for the above picked source 38 | for (j = 0; j < V; j++) { 39 | // If vertex k is on the shortest path from i to j, then update the value of dist[i][j] 40 | if (dist[i][j] > (dist[i][k] + dist[k][j]) 41 | && (dist[k][j] != INF 42 | && dist[i][k] != INF)) 43 | dist[i][j] = dist[i][k] + dist[k][j]; 44 | } } } // Print the shortest distance matrix 45 | printSolution(dist); } 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | /* A utility function to print solution */ 58 | void printSolution(int dist[][V]) 59 | { 60 | cout << "The following matrix shows the shortest " 61 | "distances" 62 | " between every pair of vertices \n"; 63 | for (int i = 0; i < V; i++) { 64 | for (int j = 0; j < V; j++) { 65 | if (dist[i][j] == INF) 66 | cout << "INF" 67 | << " "; 68 | else 69 | cout << dist[i][j] << " "; 70 | } 71 | cout << endl; 72 | } 73 | } 74 | int main() 75 | { 76 | /* Let us create the following weighted graph 77 | 10 78 | (0)------->(3) 79 | | /|\ 80 | 5 | | 81 | | | 1 82 | \|/ | 83 | (1)------->(2) 84 | 3 */ 85 | int graph[V][V] = { { 0, 5, INF, 10 }, 86 | { INF, 0, 3, INF }, 87 | { INF, INF, 0, 1 }, 88 | { INF, INF, INF, 0 } }; 89 | 90 | // Function call 91 | floydWarshall(graph); 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /java/sorting/LinkedList.java: -------------------------------------------------------------------------------- 1 | public class LinkedList{ 2 | 3 | private Node head; 4 | 5 | private static class Node { 6 | private int val; 7 | private Node next; 8 | 9 | Node(int val) { 10 | this.val = val; 11 | 12 | } 13 | } 14 | 15 | public void insetInList(Node node) { 16 | 17 | if (head == null) { 18 | head = node; 19 | } else { 20 | Node temp = head; 21 | while (temp.next != null) 22 | temp = temp.next; 23 | 24 | temp.next = node; 25 | } 26 | } 27 | 28 | public void printLinkedList(Node head) { 29 | Node temp = head; 30 | while (temp != null) { 31 | System.out.format("%d ", temp.val); 32 | temp = temp.next; 33 | } 34 | System.out.println(); 35 | } 36 | 37 | public static Node reverseLinkedList(Node currNode) 38 | { 39 | Node prevNode=null; 40 | Node nextNode; 41 | while(currNode!=null) 42 | { 43 | nextNode=currNode.next; 44 | currNode.next=prevNode; 45 | prevNode=currNode; 46 | currNode=nextNode; 47 | } 48 | return prevNode; 49 | } 50 | 51 | public static void main(String[] args) { 52 | LinkedList list = new LinkedList(); 53 | Node head=new Node(1); 54 | list.insetInList(head); 55 | list.insetInList(new Node(2)); 56 | list.insetInList(new Node(3)); 57 | list.insetInList(new Node(4)); 58 | list.insetInList(new Node(5)); 59 | list.printLinkedList(head); 60 | Node reverseHead=reverseLinkedList(head); 61 | System.out.println("After reversing the linked list"); 62 | list.printLinkedList(reverseHead); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /java/sorting/insertion_sort.java: -------------------------------------------------------------------------------- 1 | // Java program for implementation of Insertion Sort 2 | class InsertionSort { 3 | /*Function to sort array using insertion sort*/ 4 | void sort(int arr[]) 5 | { 6 | int n = arr.length; 7 | for (int i = 1; i < n; ++i) { 8 | int key = arr[i]; 9 | int j = i - 1; 10 | 11 | /* Move elements of arr[0..i-1], that are 12 | greater than key, to one position ahead 13 | of their current position */ 14 | while (j >= 0 && arr[j] > key) { 15 | arr[j + 1] = arr[j]; 16 | j = j - 1; 17 | } 18 | arr[j + 1] = key; 19 | } 20 | } 21 | 22 | /* A utility function to print array of size n*/ 23 | static void printArray(int arr[]) 24 | { 25 | int n = arr.length; 26 | for (int i = 0; i < n; ++i) 27 | System.out.print(arr[i] + " "); 28 | 29 | System.out.println(); 30 | } 31 | 32 | // Driver method 33 | public static void main(String args[]) 34 | { 35 | int arr[] = { 12, 11, 13, 5, 6 }; 36 | 37 | InsertionSort ob = new InsertionSort(); 38 | ob.sort(arr); 39 | 40 | printArray(arr); 41 | } 42 | } /* This code is contributed by Rajat Mishra. */ 43 | -------------------------------------------------------------------------------- /java/sorting/using_loop.java: -------------------------------------------------------------------------------- 1 | // Java Program to sort an elements 2 | // by bringing Arrays into play 3 | 4 | // Main class 5 | class GFG { 6 | 7 | // Main driver method 8 | public static void main(String[] args) 9 | { 10 | 11 | // Custom input array 12 | int arr[] = { 4, 3, 2, 1 }; 13 | 14 | // Outer loop 15 | for (int i = 0; i < arr.length; i++) { 16 | 17 | // Inner nested loop pointing 1 index ahead 18 | for (int j = i + 1; j < arr.length; j++) { 19 | 20 | // Checking elements 21 | int temp = 0; 22 | if (arr[j] < arr[i]) { 23 | 24 | // Swapping 25 | temp = arr[i]; 26 | arr[i] = arr[j]; 27 | arr[j] = temp; 28 | } 29 | } 30 | 31 | // Printing sorted array elements 32 | System.out.print(arr[i] + " "); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /knapsack 01: -------------------------------------------------------------------------------- 1 | // A dynamic programming based 2 | // solution for 0-1 Knapsack problem 3 | #include 4 | using namespace std; 5 | // A utility function that returns 6 | // maximum of two integers 7 | int max(int a, int b){ return (a > b) ? a : b; } 8 | // Returns the maximum value that 9 | // can be put in a knapsack of capacity W 10 | 11 | 12 | 13 | 14 | int knapSack(int W, int wt[], int val[], int n) 15 | { int i, w; 16 | vector> K(n + 1, vector(W + 1)); 17 | // Build table K[][] in bottom up manner 18 | for(i = 0; i <= n; i++) 19 | { for(w = 0; w <= W; w++) 20 | { if (i == 0 || w == 0) 21 | K[i][w] = 0; 22 | else if (wt[i - 1] <= w) 23 | K[i][w] = max(val[i - 1] + 24 | K[i - 1][w - wt[i - 1]], 25 | K[i - 1][w]); 26 | else K[i][w] = K[i - 1][w]; 27 | } 28 | } return K[n][W]; 29 | } 30 | 31 | 32 | 33 | 34 | int main() 35 | { 36 | int val[] = { 60, 100, 120 }; 37 | int wt[] = { 10, 20, 30 }; 38 | int W = 50; 39 | int n = sizeof(val) / sizeof(val[0]); 40 | cout << knapSack(W, wt, val, n); 41 | return 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /python/InsertionSort.py: -------------------------------------------------------------------------------- 1 | # Insertion sort-->Python 2 | 3 | def insertionSort(arr): 4 | 5 | for i in range(1, len(arr)): 6 | key = arr[i] 7 | j = i - 1 8 | 9 | # For descending order --> keyarray[j]. 10 | while j >= 0 and i < arr[j]: 11 | arr[j + 1] = arr[j] 12 | j = j - 1 13 | arr[j + 1] = key 14 | 15 | 16 | d = [9, 5, 1, 4, 3] 17 | insertionSort(d) 18 | print('Sorted Array= ') 19 | print(d) 20 | -------------------------------------------------------------------------------- /python/Merge_sort.py: -------------------------------------------------------------------------------- 1 | # Python program for implementation of MergeSort 2 | 3 | # Merges two subarrays of arr[]. 4 | # First subarray is arr[l..m] 5 | # Second subarray is arr[m+1..r] 6 | 7 | 8 | def merge(arr, l, m, r): 9 | n1 = m - l + 1 10 | n2 = r - m 11 | 12 | # create temp arrays 13 | L = [0] * (n1) 14 | R = [0] * (n2) 15 | 16 | # Copy data to temp arrays L[] and R[] 17 | for i in range(0, n1): 18 | L[i] = arr[l + i] 19 | 20 | for j in range(0, n2): 21 | R[j] = arr[m + 1 + j] 22 | 23 | # Merge the temp arrays back into arr[l..r] 24 | i = 0 # Initial index of first subarray 25 | j = 0 # Initial index of second subarray 26 | k = l # Initial index of merged subarray 27 | 28 | while i < n1 and j < n2: 29 | if L[i] <= R[j]: 30 | arr[k] = L[i] 31 | i += 1 32 | else: 33 | arr[k] = R[j] 34 | j += 1 35 | k += 1 36 | 37 | # Copy the remaining elements of L[], if there 38 | # are any 39 | while i < n1: 40 | arr[k] = L[i] 41 | i += 1 42 | k += 1 43 | 44 | # Copy the remaining elements of R[], if there 45 | # are any 46 | while j < n2: 47 | arr[k] = R[j] 48 | j += 1 49 | k += 1 50 | 51 | # l is for left index and r is right index of the 52 | # sub-array of arr to be sorted 53 | 54 | 55 | def mergeSort(arr, l, r): 56 | if l < r: 57 | 58 | # Same as (l+r)//2, but avoids overflow for 59 | # large l and h 60 | m = l+(r-l)//2 61 | 62 | # Sort first and second halves 63 | mergeSort(arr, l, m) 64 | mergeSort(arr, m+1, r) 65 | merge(arr, l, m, r) 66 | 67 | 68 | # Driver code to test above 69 | arr = [12, 11, 13, 5, 6, 7] 70 | n = len(arr) 71 | print("Given array is") 72 | for i in range(n): 73 | print("%d" % arr[i],end=" ") 74 | 75 | mergeSort(arr, 0, n-1) 76 | print("\n\nSorted array is") 77 | for i in range(n): 78 | print("%d" % arr[i],end=" ") 79 | 80 | # This code is contributed by Mohit Kumra 81 | -------------------------------------------------------------------------------- /python/Number_Guessing.py: -------------------------------------------------------------------------------- 1 | # A program for fun where you need to guess a number using hints given by the program. 2 | 3 | import random 4 | 5 | randNumber = random.randint(1,100) 6 | 7 | userGuess = None 8 | guesses = 0 9 | 10 | while(userGuess!=randNumber): 11 | userGuess = int(input("Enter your guess between(1 to 100):")) 12 | if userGuess>100 or userGuess<1: 13 | print("Enter Valid Number") 14 | guesses+=1 15 | 16 | if(userGuess == randNumber): 17 | print("You Guessed it right!!") 18 | else: 19 | if(userGuess>randNumber): 20 | print("You Guessed it worng!! Enter a smaller number") 21 | else: 22 | print("You Guessed it wrong!! Enter a larger number") 23 | 24 | 25 | print(f"You Guessed the number in {guesses} Guesses") 26 | 27 | #****************************************************** THE END ********************************************** 28 | -------------------------------------------------------------------------------- /python/Quick_sort.py: -------------------------------------------------------------------------------- 1 | # Python3 implementation of QuickSort 2 | 3 | 4 | # Function to find the partition position 5 | def partition(array, low, high): 6 | 7 | # Choose the rightmost element as pivot 8 | pivot = array[high] 9 | 10 | # Pointer for greater element 11 | i = low - 1 12 | 13 | # Traverse through all elements 14 | # compare each element with pivot 15 | for j in range(low, high): 16 | if array[j] <= pivot: 17 | # If element smaller than pivot is found 18 | # swap it with the greater element pointed by i 19 | i = i + 1 20 | 21 | # Swapping element at i with element at j 22 | (array[i], array[j]) = (array[j], array[i]) 23 | 24 | # Swap the pivot element with 25 | # e greater element specified by i 26 | (array[i + 1], array[high]) = (array[high], array[i + 1]) 27 | 28 | # Return the position from where partition is done 29 | return i + 1 30 | 31 | # Function to perform quicksort 32 | 33 | 34 | def quick_sort(array, low, high): 35 | if low < high: 36 | 37 | # Find pivot element such that 38 | # element smaller than pivot are on the left 39 | # element greater than pivot are on the right 40 | pi = partition(array, low, high) 41 | 42 | # Recursive call on the left of pivot 43 | quick_sort(array, low, pi - 1) 44 | 45 | # Recursive call on the right of pivot 46 | quick_sort(array, pi + 1, high) 47 | 48 | 49 | # Driver code 50 | array = [10, 7, 8, 9, 1, 5] 51 | quick_sort(array, 0, len(array) - 1) 52 | 53 | print(f'Sorted array: {array}') 54 | 55 | # This code is contributed by Adnan Aliakbar 56 | -------------------------------------------------------------------------------- /python/Zip_code.py: -------------------------------------------------------------------------------- 1 | import geopy 2 | from geopy.geocoders import Nominatim 3 | 4 | geolocator = Nominatim(user_agent="geoapiExercises") 5 | 6 | # Zipocde input 7 | # zipcode = "201304" 8 | # zipcode = input("Enter your Zipcode here: ") 9 | 10 | # Using geocode() 11 | location = geolocator.geocode("201307") 12 | print(location.address) 13 | print((location.latitude, location.longitude)) 14 | print(location.raw) 15 | 16 | # Displaying address details 17 | # print("Zipcode:",zipcode) 18 | # print("Checking details.....") 19 | # print("Details of the Zipcode:") 20 | # print(location) 21 | -------------------------------------------------------------------------------- /python/bubble_sort.py: -------------------------------------------------------------------------------- 1 | # Python program for implementation of Bubble Sort 2 | 3 | 4 | def bubbleSort(arr): 5 | n = len(arr) 6 | 7 | # Traverse through all array elements 8 | for i in range(n): 9 | 10 | # Last i elements are already in place 11 | for j in range(0, n-i-1): 12 | 13 | # traverse the array from 0 to n-i-1 14 | # Swap if the element found is greater 15 | # than the next element 16 | if arr[j] > arr[j+1]: 17 | arr[j], arr[j+1] = arr[j+1], arr[j] 18 | 19 | 20 | # Driver code to test above 21 | if __name__ == "__main__": 22 | arr = [64, 34, 25, 12, 22, 11, 90] 23 | 24 | bubbleSort(arr) 25 | 26 | print("Sorted array is:") 27 | for i in range(len(arr)): 28 | print("%d" % arr[i], end=" ") 29 | -------------------------------------------------------------------------------- /python/string.py: -------------------------------------------------------------------------------- 1 | str=input() 2 | words=[i for i in str.split()] 3 | words.sort() 4 | for i in words: 5 | n=i.count('a')+i.count('e')+i.count('i')\ 6 | +i.count('o')+i.count('u')+i.count('A')\ 7 | +i.count('E')+i.count('I')+i.count('O')+i.count('U') 8 | print(i,n,sep=',') 9 | -------------------------------------------------------------------------------- /strassen matrix: -------------------------------------------------------------------------------- 1 | /*Given two square matrices A and B of size n x n each, find their multiplication matrix. 2 | 3 | Old Method: T.C. = O(N*N*N) 4 | void multiply(int A[][N], int B[][N], int C[][N]) 5 | { 6 | for (int i = 0; i < N; i++) 7 | { 8 | for (int j = 0; j < N; j++) 9 | { 10 | C[i][j] = 0; 11 | for (int k = 0; k < N; k++) 12 | { 13 | C[i][j] += A[i][k]*B[k][j]; 14 | } 15 | } 16 | More better method, 17 | Following is simple Divide and Conquer method to multiply two square matrices. 18 | Divide matrices A and B in 4 sub-matrices of size N/2 x N/2 as shown in the below diagram. 19 | Calculate following values recursively. ae + bg, af + bh, ce + dg and cf + dh. 20 | a b \ / e f -- ae+bg af+bh 21 | c d / \ g h -- ce+dg cf+dh 22 | 23 | Doing this will also be O(N*N*N) time complexity 24 | code: 25 | 26 | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 27 | 28 | 29 | 30 | 31 | 32 | Better method which is strassen method, 33 | 34 | In the above divide and conquer method, the main component for high time complexity is 8 recursive calls. 35 | the idea of Strassen’s method is to reduce the number of recursive calls to 7. 36 | Strassen’s method is similar to above simple divide and conquer method in the sense that this method also divide matrices to sub-matrices 37 | of size N/2 x N/2 as shown in the above diagram, but in Strassen’s method, the four sub-matrices of result are calculated using following formulae. 38 | p1 = a(f-h) ; p2 = (a+b)h ; p3 = (c+d)e ; p4 = d(g-e) ; p5 = (a+d)(e+h) ; p6 = (b-d)(g+h) ; p7 = (a-c)(e+f); 39 | answer of abovwe matrix is : | p4+p5-p2+p6 | p1+p2 | 40 | | p3+p4 | p1+p5-p3-p7/ | 41 | Time complexity of strassen’s method: 42 | Addition and Subtraction of two matrices takes O(N2) time. So time complexity can be written as T(N) = 7T(N/2) + O(N2) 43 | From Master's Theorem, time complexity of above method is O(NLog7) which is approximately O(N2.8074). 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | Generally Strassen’s Method is not preferred for practical applications for following reasons : 58 | 59 | 1.The constants used in Strassen’s method are high and for a typical application Naive method works better. 60 | 2.For Sparse matrices, there are better methods especially designed for them. 61 | 3.The submatrices in recursion take extra space. 62 | 4.Because of the limited precision of computer arithmetic on noninteger values, larger errors accumulate in Strassen’s algorithm than in Naive Method 63 | 64 | Easy way to remember formulae: 65 | remember these 4 rules: 66 | 1.AHED (Learn it as ‘Ahead’) 67 | 2.Diagonal 68 | 3.Last CR 69 | 4.First CR 70 | 71 | Also, consider X as (Row +) and Y as (Column -) matrix Follow the Steps : 72 | 73 | 1.Write P1 = A; P2 = H; P3 = E; P4 = D 74 | 75 | 2.For P5 we will use Diagonal Rule i.e. (Sum the Diagonal Elements Of Matrix X ) * (Sum the Diagonal Elements Of Matrix Y ), we get P5 = (A + D)* (E + H) 76 | 77 | 3.For P6 we will use Last CR Rule i.e. Last Column of X and Last Row of Y and remember that Row+ and Column- so i.e. (B – D) * (G + H), we get P6 = (B – D) * (G + H) 78 | 79 | 4.For P7 we will use First CR Rule i.e. First Column of X and First Row of Y and remember that Row+ and Column- so i.e. (A – C) * (E + F), we get P7 = (A – C) * (E + F) 80 | 81 | 5.Come Back to P1 : we have A there and it’s adjacent element in Y Matrix is E, since Y is Column Matrix so we select a column in Y such that E won’t come, 82 | we find F H Column, so multiply A with (F – H) So, finally P1 = A * (F – H) 83 | 84 | 6.Come Back to P2 : we have H there and it’s adjacent element in X Matrix is D, since X is Row Matrix so we select a Row in X such that D won’t come, 85 | we find A B Column, so multiply H with (A + B) So, finally P2 = (A + B) * H 86 | 87 | 7.Come Back to P3 : we have E there and it’s adjacent element in X Matrix is A, since X is Row Matrix so we select a Row in X such that A won’t come, 88 | we find C D Column, so multiply E with (C + D) So, finally P3 = (C + D) * E 89 | 90 | 8.Come Back to P4 : we have D there and it’s adjacent element in Y Matrix is H, since Y is Column Matrix so we select a column in Y such that H won’t come, 91 | we find G E Column, so multiply D with (G – E) So, finally P4 = D * (G – E) 92 | 93 | 9.Remember Counting : Write P1 + P2 at C2 94 | 95 | 10.Write P3 + P4 at its diagonal Position i.e. at C3 96 | 97 | 11.Write P4 + P5 + P6 at 1st position and subtract P2 i.e. C1 = P4 + P5 + P6 – P2 98 | 99 | 12.Write odd values at last Position with alternating – and + sign i.e. P1 P3 P5 P7 becomes C4 = P1 – P3 + P5 – P7 100 | */ 101 | Code: 102 | #include 103 | #include 104 | #define vi vector 105 | #define vii vector 106 | using namespace std; 107 | /* finding next square of 2*/ 108 | int nextPowerOf2(int k) 109 | { 110 | return pow(2, int(ceil(log2(k)))); 111 | } 112 | // printing matrix 113 | void display(vii C, int m, int n) 114 | { 115 | for (int i = 0; i < m; i++) 116 | { 117 | cout << "|" 118 | << " "; 119 | for (int j = 0; j < n; j++) 120 | { 121 | cout << C[i][j] << " "; 122 | } 123 | cout << "|" << endl; 124 | } 125 | } 126 | //! addition and subtraction 127 | void add(vii &A, vii &B, vii &C, int size) 128 | { 129 | for (int i = 0; i < size; i++) 130 | { 131 | for (int j = 0; j < size; j++) 132 | { 133 | C[i][j] = A[i][j] + B[i][j]; 134 | } 135 | } 136 | } 137 | void sub(vii &A, vii &B, vii &C, int size) 138 | { 139 | for (int i = 0; i < size; i++) 140 | { 141 | for (int j = 0; j < size; j++) 142 | { 143 | C[i][j] = A[i][j] - B[i][j]; 144 | } 145 | } 146 | } 147 | //!----------------------------- 148 | void Strassen_algorithm(vii &A, vii &B, vii &C, int size) 149 | { 150 | if (size == 1) 151 | { 152 | C[0][0] = A[0][0] * B[0][0]; 153 | return; 154 | } 155 | else 156 | { 157 | int newSize = size / 2; 158 | vi z(newSize); 159 | vii a(newSize, z), b(newSize, z), c(newSize, z), d(newSize, z), 160 | e(newSize, z), f(newSize, z), g(newSize, z), h(newSize, z), 161 | c11(newSize, z), c12(newSize, z), c21(newSize, z), c22(newSize, z), 162 | p1(newSize, z), p2(newSize, z), p3(newSize, z), p4(newSize, z), 163 | p5(newSize, z), p6(newSize, z), p7(newSize, z), fResult(newSize, z), 164 | sResult(newSize, z); 165 | int i, j; 166 | 167 | //! divide the matrix in equal parts 168 | for (i = 0; i < newSize; i++) 169 | { 170 | for (j = 0; j < newSize; j++) 171 | { 172 | a[i][j] = A[i][j]; 173 | b[i][j] = A[i][j + newSize]; 174 | c[i][j] = A[i + newSize][j]; 175 | d[i][j] = A[i + newSize][j + newSize]; 176 | 177 | e[i][j] = B[i][j]; 178 | f[i][j] = B[i][j + newSize]; 179 | g[i][j] = B[i + newSize][j]; 180 | h[i][j] = B[i + newSize][j + newSize]; 181 | } 182 | } 183 | /* 184 | A B C 185 | [a b] * [e f] = [c11 c12] 186 | [g h] [c21 c22] 187 | p1,p2,p3,p4=AHED for this: A:Row(+) and B:Column(-) 188 | p5=Diagonal :both +ve 189 | p6=Last CR :A:Row(-) B:Column(+) 190 | p7=First CR :A:Row(-) B:Column(+) 191 | */ 192 | //! calculating all strassen formulas 193 | //*p1=a*(f-h) 194 | sub(f, h, sResult, newSize); 195 | Strassen_algorithm(a, sResult, p1, newSize); 196 | 197 | //*p2=h*(a+b) 198 | add(a, b, fResult, newSize); 199 | Strassen_algorithm(fResult, h, p2, newSize); 200 | 201 | //*p3=e*(c+d) 202 | add(c, d, fResult, newSize); 203 | Strassen_algorithm(fResult, e, p3, newSize); 204 | 205 | //*p4=d*(g-e) 206 | sub(g, e, sResult, newSize); 207 | Strassen_algorithm(d, sResult, p4, newSize); 208 | 209 | //*p5=(a+d)*(e+h) 210 | add(a, d, fResult, newSize); 211 | add(e, h, sResult, newSize); 212 | Strassen_algorithm(fResult, sResult, p5, newSize); 213 | 214 | //*p6=(b-d)*(g+h) 215 | sub(b, d, fResult, newSize); 216 | add(g, h, sResult, newSize); 217 | Strassen_algorithm(fResult, sResult, p6, newSize); 218 | 219 | //*p7=(a-c)*(e+f) 220 | sub(a, c, fResult, newSize); 221 | add(e, f, sResult, newSize); 222 | Strassen_algorithm(fResult, sResult, p7, newSize); 223 | 224 | /* calculating all elements of C by p1,p2,p3 225 | c11=p4+p5+p6-p2 226 | c12=p1+p2 227 | c21=p3+p4 228 | c22=p1-p3+p5-p7 229 | */ 230 | add(p1, p2, c12, newSize); //! 231 | add(p3, p4, c21, newSize); //! 232 | 233 | add(p4, p5, fResult, newSize); 234 | add(fResult, p6, sResult, newSize); 235 | sub(sResult, p2, c11, newSize); //! 236 | 237 | sub(p1, p3, fResult, newSize); 238 | add(fResult, p5, sResult, newSize); 239 | sub(sResult, p7, c22, newSize); //! 240 | 241 | // Grouping the results obtained in a single matrix: 242 | for (i = 0; i < newSize; i++) 243 | { 244 | for (j = 0; j < newSize; j++) 245 | { 246 | C[i][j] = c11[i][j]; 247 | C[i][j + newSize] = c12[i][j]; 248 | C[i + newSize][j] = c21[i][j]; 249 | C[i + newSize][j + newSize] = c22[i][j]; 250 | } 251 | } 252 | } 253 | } 254 | /*for converting matrix to square matrix*/ 255 | void ConvertToSquareMat(vii &A, vii &B, int r1, int c1, int r2, int c2) 256 | { 257 | int maxSize = max({r1, c1, r2, c2}); 258 | int size = nextPowerOf2(maxSize); 259 | 260 | vi z(size); 261 | vii Aa(size, z), Bb(size, z), Cc(size, z); 262 | 263 | for (unsigned int i = 0; i < r1; i++) 264 | { 265 | for (unsigned int j = 0; j < c1; j++) 266 | { 267 | Aa[i][j] = A[i][j]; 268 | } 269 | } 270 | for (unsigned int i = 0; i < r2; i++) 271 | { 272 | for (unsigned int j = 0; j < c2; j++) 273 | { 274 | Bb[i][j] = B[i][j]; 275 | } 276 | } 277 | Strassen_algorithm(Aa, Bb, Cc, size); 278 | vi temp1(c2); 279 | vii C(r1, temp1); 280 | for (unsigned int i = 0; i < r1; i++) 281 | { 282 | for (unsigned int j = 0; j < c2; j++) 283 | { 284 | C[i][j] = Cc[i][j]; 285 | } 286 | } 287 | display(C, r1, c1); 288 | } 289 | int main() 290 | { 291 | vii a = { 292 | {1, 2, 3}, 293 | {1, 2, 3}, 294 | {0, 0, 2}}; 295 | vii b = { 296 | {1, 0, 0}, 297 | {0, 1, 0}, 298 | {0, 0, 1}}; 299 | ConvertToSquareMat(a, b, 3, 3, 3, 3); // A[][],B[][],R1,C1,R2,C2 300 | return 0; 301 | 302 | } 303 | --------------------------------------------------------------------------------