From 20c3b1b987deacacfa6e6fe57e0043805b0dda6d Mon Sep 17 00:00:00 2001 From: aj-34719 Date: Sat, 2 Oct 2021 23:57:01 +0530 Subject: [PATCH 1/3] added 5+ progrma --- .../CPP/Dynamic Programming/quickSort.cpp | 184 ++++++++++++++++++ .../CPP/Dynamic Programming/selectionSort.cpp | 99 ++++++++++ .../CPP/Sorting/quickSort.cpp | 184 ++++++++++++++++++ .../CPP/Sorting/selectionSort.cpp | 99 ++++++++++ 4 files changed, 566 insertions(+) create mode 100644 DataStructure_and_Algorithms/CPP/Dynamic Programming/quickSort.cpp create mode 100644 DataStructure_and_Algorithms/CPP/Dynamic Programming/selectionSort.cpp create mode 100644 DataStructure_and_Algorithms/CPP/Sorting/quickSort.cpp create mode 100644 DataStructure_and_Algorithms/CPP/Sorting/selectionSort.cpp diff --git a/DataStructure_and_Algorithms/CPP/Dynamic Programming/quickSort.cpp b/DataStructure_and_Algorithms/CPP/Dynamic Programming/quickSort.cpp new file mode 100644 index 0000000..d9007a2 --- /dev/null +++ b/DataStructure_and_Algorithms/CPP/Dynamic Programming/quickSort.cpp @@ -0,0 +1,184 @@ + +// ............................. Quick Sort : O(NlogN) ...................... +// principle : it choose a pivote element from array and part +// the array around that element it, continue untill it cannot be part further then sort every part + +#include +using namespace std; + +// partition function +int partition(int arr[], int l, int r){ + + // 3 approach + + // if pivote is first element + //............................................................................................... + + + /* + int pivote = arr[l]; + int i = l; + + for(int j=l+1; j<=r; j++){ + if(arr[j] < pivote){ + i++; + swap(arr[j],arr[i]); + } + } + + swap(arr[i],arr[l]); + return i++; + + */ + + +//.....................OR..................................................................... + + /* + int pivote = arr[l]; + int index=0; + for(int i=l; i<=r; i++) + if(arr[i](l+index)){ + if(arr[i]=pivote) + j--; + else{ + swap(arr[i],arr[j]); + i++; j--; + } + } + + return l+index; + + */ + + + + + + + + + // if pivote is last element +//................................................................................................. + + /* + int pivote = arr[r]; + int i = l-1; + + for(int j=l; j(l+index)){ + if(arr[i]=pivote) + j--; + else{ + swap(arr[i],arr[j]); + i++; j--; + } + } + + return l+index; + + */ + + + + + + + + // if middle element is pivote +//............................................................................................. + + + int mid = l + (r-l)/2; + int pivote = arr[mid]; + + int index=0; + for(int i=l; i<=r; i++) + if(arr[i](l+index)){ + if(arr[i]=pivote) + j--; + else{ + swap(arr[i],arr[j]); + i++; j--; + } + } + + return l+index; + + +} + + +// Quick Sort Function +void quickSort(int arr[], int l, int r){ + if(l>=r) + return; + + int pi = partition(arr, l, r); + + quickSort(arr,l,pi-1); + quickSort(arr,pi+1,r); +} + + +int main(){ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + #endif + freopen("output.txt","w",stdout); + + int test; cin >> test; + while(test--){ + int n; cin >> n; + int arr[n]; + + for(int i=0; i> arr[i]; + + quickSort(arr,0,n-1); + + for(int x : arr) + cout << x << " "; + + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/DataStructure_and_Algorithms/CPP/Dynamic Programming/selectionSort.cpp b/DataStructure_and_Algorithms/CPP/Dynamic Programming/selectionSort.cpp new file mode 100644 index 0000000..dd465b4 --- /dev/null +++ b/DataStructure_and_Algorithms/CPP/Dynamic Programming/selectionSort.cpp @@ -0,0 +1,99 @@ +//.............. Selection sort O(N^2) ................ +// princple : it is something like insertion sort , it has two array sorted and unsorted +// each time it pick the minimum element from unsorted array and put it in sorted array at right position(at last) + + +#include +using namespace std; + + +// function for sort by recursion (select max element and put at last i.e first of sorted array) +void selectionSort1(int arr[], int n){ + if(n<=1) + return ; + + int max = INT_MIN; + int maxIndex = n-1; + for(int i=0; i arr[i]){ + min = arr[i]; + minIndex = i; + } + if(minIndex != index){ + arr[minIndex] = arr[index]; + arr[index] = min; + + } + secondRecursion(arr,n,index+1); +} + +// by iterator ..... +void selectionSort2(int arr[], int n){ + if(n<=1) + return ; + + int lastIndexOfSorted = 0; + for(int i=0; i arr[j]){ + min = arr[j]; + minIndex = j; + } + if(minIndex != lastIndexOfSorted){ + arr[minIndex] = arr[lastIndexOfSorted]; + arr[lastIndexOfSorted] = min; + } + lastIndexOfSorted++; + } + + +} + + +int main(){ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + #endif + freopen("output.txt","w",stdout); + + int test; cin >> test; + while(test--){ + int n; cin >> n; + int arr[n]; + + for(int i=0; i> arr[i]; + + selectionSort1(arr,n); // recursion + //secondRecursion(arr,n); // recursion + //selectionSort2(arr,n); // iterartor + + for(int x : arr) + cout << x << " "; + + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/DataStructure_and_Algorithms/CPP/Sorting/quickSort.cpp b/DataStructure_and_Algorithms/CPP/Sorting/quickSort.cpp new file mode 100644 index 0000000..d9007a2 --- /dev/null +++ b/DataStructure_and_Algorithms/CPP/Sorting/quickSort.cpp @@ -0,0 +1,184 @@ + +// ............................. Quick Sort : O(NlogN) ...................... +// principle : it choose a pivote element from array and part +// the array around that element it, continue untill it cannot be part further then sort every part + +#include +using namespace std; + +// partition function +int partition(int arr[], int l, int r){ + + // 3 approach + + // if pivote is first element + //............................................................................................... + + + /* + int pivote = arr[l]; + int i = l; + + for(int j=l+1; j<=r; j++){ + if(arr[j] < pivote){ + i++; + swap(arr[j],arr[i]); + } + } + + swap(arr[i],arr[l]); + return i++; + + */ + + +//.....................OR..................................................................... + + /* + int pivote = arr[l]; + int index=0; + for(int i=l; i<=r; i++) + if(arr[i](l+index)){ + if(arr[i]=pivote) + j--; + else{ + swap(arr[i],arr[j]); + i++; j--; + } + } + + return l+index; + + */ + + + + + + + + + // if pivote is last element +//................................................................................................. + + /* + int pivote = arr[r]; + int i = l-1; + + for(int j=l; j(l+index)){ + if(arr[i]=pivote) + j--; + else{ + swap(arr[i],arr[j]); + i++; j--; + } + } + + return l+index; + + */ + + + + + + + + // if middle element is pivote +//............................................................................................. + + + int mid = l + (r-l)/2; + int pivote = arr[mid]; + + int index=0; + for(int i=l; i<=r; i++) + if(arr[i](l+index)){ + if(arr[i]=pivote) + j--; + else{ + swap(arr[i],arr[j]); + i++; j--; + } + } + + return l+index; + + +} + + +// Quick Sort Function +void quickSort(int arr[], int l, int r){ + if(l>=r) + return; + + int pi = partition(arr, l, r); + + quickSort(arr,l,pi-1); + quickSort(arr,pi+1,r); +} + + +int main(){ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + #endif + freopen("output.txt","w",stdout); + + int test; cin >> test; + while(test--){ + int n; cin >> n; + int arr[n]; + + for(int i=0; i> arr[i]; + + quickSort(arr,0,n-1); + + for(int x : arr) + cout << x << " "; + + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/DataStructure_and_Algorithms/CPP/Sorting/selectionSort.cpp b/DataStructure_and_Algorithms/CPP/Sorting/selectionSort.cpp new file mode 100644 index 0000000..dd465b4 --- /dev/null +++ b/DataStructure_and_Algorithms/CPP/Sorting/selectionSort.cpp @@ -0,0 +1,99 @@ +//.............. Selection sort O(N^2) ................ +// princple : it is something like insertion sort , it has two array sorted and unsorted +// each time it pick the minimum element from unsorted array and put it in sorted array at right position(at last) + + +#include +using namespace std; + + +// function for sort by recursion (select max element and put at last i.e first of sorted array) +void selectionSort1(int arr[], int n){ + if(n<=1) + return ; + + int max = INT_MIN; + int maxIndex = n-1; + for(int i=0; i arr[i]){ + min = arr[i]; + minIndex = i; + } + if(minIndex != index){ + arr[minIndex] = arr[index]; + arr[index] = min; + + } + secondRecursion(arr,n,index+1); +} + +// by iterator ..... +void selectionSort2(int arr[], int n){ + if(n<=1) + return ; + + int lastIndexOfSorted = 0; + for(int i=0; i arr[j]){ + min = arr[j]; + minIndex = j; + } + if(minIndex != lastIndexOfSorted){ + arr[minIndex] = arr[lastIndexOfSorted]; + arr[lastIndexOfSorted] = min; + } + lastIndexOfSorted++; + } + + +} + + +int main(){ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + #endif + freopen("output.txt","w",stdout); + + int test; cin >> test; + while(test--){ + int n; cin >> n; + int arr[n]; + + for(int i=0; i> arr[i]; + + selectionSort1(arr,n); // recursion + //secondRecursion(arr,n); // recursion + //selectionSort2(arr,n); // iterartor + + for(int x : arr) + cout << x << " "; + + cout << endl; + } + + return 0; +} \ No newline at end of file From 8249ae60848cacf05acee2e7dbcc366b930aa822 Mon Sep 17 00:00:00 2001 From: aj-34719 Date: Sat, 2 Oct 2021 23:59:17 +0530 Subject: [PATCH 2/3] added programs and files --- .../CPP/Segment Tree/segmentree.cpp | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 DataStructure_and_Algorithms/CPP/Segment Tree/segmentree.cpp diff --git a/DataStructure_and_Algorithms/CPP/Segment Tree/segmentree.cpp b/DataStructure_and_Algorithms/CPP/Segment Tree/segmentree.cpp new file mode 100644 index 0000000..6957fd3 --- /dev/null +++ b/DataStructure_and_Algorithms/CPP/Segment Tree/segmentree.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; + + +void updateSegmentTree(int arr[], int tree[], int start, int end, int node, int idx, int value){ + if(start==end){ + return; + } + if(start==idx || end==idx){ + arr[idx] = value; + tree[node] = arr[idx]; + return ; + } + + int mid = (start+end)/2; + int node1 = node*2; + int node2 = node*2+1; + if(mid<=idx){ + updateSegmentTree(arr, tree, start, mid, node1, idx, value); + }else { + updateSegmentTree(arr, tree, mid+1, end, node2, idx, value); + } + + tree[node] = tree[node1]+tree[node2]; +} + +void makeSegmentTree(int arr[], int tree[], int start, int end, int node){ + if(start==end){ + tree[node] = arr[start]; + return ; + } + int mid = (start+end)/2; + int node1 = node*2; + int node2 = node*2+1; + makeSegmentTree(arr, tree, start, mid, node1); + makeSegmentTree(arr, tree, mid+1, end, node2); + tree[node] = tree[node1]+tree[node2]; +} + +int main(){ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + + int n; + cin >> n; + + int arr[n] ; + for(int i=0; i> arr[i]; + } + + for(int i : arr) cout << i << " "; + cout << endl; + int* tree = new int[2*n]; + makeSegmentTree(arr, tree, 0, n-1, 1); + for(int i=1; i<2*n; i++) + cout << tree[i] << " "; + + cout << endl; + + updateSegmentTree(arr, tree, 0, n-1, 1, 4, 200); + for(int i=1; i<2*n; i++) + cout << tree[i] << " "; + + cout << endl; + for(int i : arr) cout << i << " "; + + // cout << findSegmentSum(tree, (2*n), 1, 3,6); + return 0; +} \ No newline at end of file From 291b5156d4600f276eb8cb16147c0d531707e921 Mon Sep 17 00:00:00 2001 From: aj-34719 Date: Sun, 3 Oct 2021 00:09:22 +0530 Subject: [PATCH 3/3] added more string programs --- .../CPP/String/anagram.cpp | 41 +++++++++++++++++++ .../CPP/String/checkRotation.cpp | 38 +++++++++++++++++ .../CPP/String/checkpalin.cpp | 20 +++++++++ .../CPP/String/duplicateChar.cpp | 29 +++++++++++++ .../CPP/String/reverseStr.cpp | 15 +++++++ .../CPP/String/shuffleStr.cpp | 35 ++++++++++++++++ 6 files changed, 178 insertions(+) create mode 100644 DataStructure_and_Algorithms/CPP/String/anagram.cpp create mode 100644 DataStructure_and_Algorithms/CPP/String/checkRotation.cpp create mode 100644 DataStructure_and_Algorithms/CPP/String/checkpalin.cpp create mode 100644 DataStructure_and_Algorithms/CPP/String/duplicateChar.cpp create mode 100644 DataStructure_and_Algorithms/CPP/String/reverseStr.cpp create mode 100644 DataStructure_and_Algorithms/CPP/String/shuffleStr.cpp diff --git a/DataStructure_and_Algorithms/CPP/String/anagram.cpp b/DataStructure_and_Algorithms/CPP/String/anagram.cpp new file mode 100644 index 0000000..3a62586 --- /dev/null +++ b/DataStructure_and_Algorithms/CPP/String/anagram.cpp @@ -0,0 +1,41 @@ + +#include +using namespace std; + + +class Solution +{ + public: + //Function is to check whether two strings are anagram of each other or not. + bool isAnagram(string a, string b){ + + // Your code here + long s1 = 0; + long s2 = 0; + + for(int i=0; i> s1 >> s2; + + Solution s; + s.isAnagram(s1,s2) ? cout << "YES" : cout << "NO"; + + + return 0; +} + \ No newline at end of file diff --git a/DataStructure_and_Algorithms/CPP/String/checkRotation.cpp b/DataStructure_and_Algorithms/CPP/String/checkRotation.cpp new file mode 100644 index 0000000..fa17f0b --- /dev/null +++ b/DataStructure_and_Algorithms/CPP/String/checkRotation.cpp @@ -0,0 +1,38 @@ +// #5 - [https://www.geeksforgeeks.org/a-program-to-check-if-strings-are-rotations-of-each-other/] +#include +using namespace std; + +bool check(string str1, string str2){ + if(str1.size() != str2.size()) + return false; + string temp = str1+str1; + int n = temp.size(); + int m = str2.size(); + + for(int i=0; i<=(n-m); i++){ + bool isFound = true; + for(int j=0; j> str1 >> str2; + + cout << check(str1, str2); + + return 0; +} \ No newline at end of file diff --git a/DataStructure_and_Algorithms/CPP/String/checkpalin.cpp b/DataStructure_and_Algorithms/CPP/String/checkpalin.cpp new file mode 100644 index 0000000..7e66a8a --- /dev/null +++ b/DataStructure_and_Algorithms/CPP/String/checkpalin.cpp @@ -0,0 +1,20 @@ +// #2 - [https://practice.geeksforgeeks.org/problems/palindrome-string0817/1#] +#include +using namespace std; + +class Solution{ +public: + + + int isPlaindrome(string S) + { + // Your code goes here + int i=0, j=S.size()-1; + while(i +using namespace std; + +unordered_map findDup(string str){ + unordered_map ans; + for(int i=0; i> str; + + unordered_map ans = findDup(str); + unordered_map :: iterator it = ans.begin(); + while(it != ans.end()){ + cout << it->first << " " << it->second << endl;; + it++; + } + + return 0; +} \ No newline at end of file diff --git a/DataStructure_and_Algorithms/CPP/String/reverseStr.cpp b/DataStructure_and_Algorithms/CPP/String/reverseStr.cpp new file mode 100644 index 0000000..4d4c3d7 --- /dev/null +++ b/DataStructure_and_Algorithms/CPP/String/reverseStr.cpp @@ -0,0 +1,15 @@ +// #1 - [https://leetcode.com/problems/reverse-string/submissions/] +#include +using namespace std; + +class Solution { +public: + void reverseString(vector& s) { + + int i=0, j=s.size()-1; + while(i +using namespace std; + +bool check(string s1, string s2, string suff){ + unordered_map t; + string temp = s1+s2; + for(int i=0; i> str1 >> str2 >> suff; + + cout << check(str1, str2, suff); + + return 0; +} \ No newline at end of file