From 31363f4a9eaa4a79eaf88565df917c121f68da61 Mon Sep 17 00:00:00 2001 From: kittu-20 Date: Fri, 9 Oct 2020 01:26:59 +0530 Subject: [PATCH 1/5] Add code for Selection Sort --- cpp/sanyamSelectionSort.cpp | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 cpp/sanyamSelectionSort.cpp diff --git a/cpp/sanyamSelectionSort.cpp b/cpp/sanyamSelectionSort.cpp new file mode 100644 index 00000000..c50222d5 --- /dev/null +++ b/cpp/sanyamSelectionSort.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; + +void swap(int *xp, int *yp) +{ + int temp = *xp; + *xp = *yp; + *yp = temp; +} + +void selectionSort(int arr[], int n) +{ + int i, j, min_idx; + for (i = 0; i < n-1; i++) + { + min_idx = i; + for (j = i+1; j < n; j++) + if (arr[j] < arr[min_idx]) + min_idx = j; + swap(&arr[min_idx], &arr[i]); + } +} + + +void printArray(int arr[], int size) +{ + int i; + for (i=0; i < size; i++) + cout << arr[i] << " "; + cout << endl; +} +int main() +{ + int arr[] = {64, 25, 12, 22, 11}; + int n = sizeof(arr)/sizeof(arr[0]); + selectionSort(arr, n); + cout << "Sorted array: \n"; + printArray(arr, n); + return 0; +} \ No newline at end of file From b3280b466c4503cc79cb0f3c1b7f1ccf7476e223 Mon Sep 17 00:00:00 2001 From: kittu-20 Date: Fri, 9 Oct 2020 11:49:13 +0530 Subject: [PATCH 2/5] Add the code for MergeSort --- cpp/sanyamMergeSort.cpp | 74 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 cpp/sanyamMergeSort.cpp diff --git a/cpp/sanyamMergeSort.cpp b/cpp/sanyamMergeSort.cpp new file mode 100644 index 00000000..a9ce7008 --- /dev/null +++ b/cpp/sanyamMergeSort.cpp @@ -0,0 +1,74 @@ +#include +#include + +void merge(int arr[], int l, int m, int r) +{ + int i, j, k; + int n1 = m - l + 1; + int n2 = r - m; + + int L[n1], R[n2]; + for (i = 0; i < n1; i++) + L[i] = arr[l + i]; + for (j = 0; j < n2; j++) + R[j] = arr[m + 1 + j]; + + + i = 0; + j = 0; + k = l; + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { + arr[k] = L[i]; + i++; + } + else { + arr[k] = R[j]; + j++; + } + k++; + } + + while (i < n1) { + arr[k] = L[i]; + i++; + k++; + } + while (j < n2) { + arr[k] = R[j]; + j++; + k++; + } +} +void mergeSort(int arr[], int l, int r) +{ + if (l < r) { + + int m = l + (r - l) / 2; + mergeSort(arr, l, m); + mergeSort(arr, m + 1, r); + + merge(arr, l, m, r); + } +} +void printArray(int A[], int size) +{ + int i; + for (i = 0; i < size; i++) + printf("%d ", A[i]); + printf("\n"); +} +int main() +{ + int arr[] = { 12, 11, 13, 5, 6, 7 }; + int arr_size = sizeof(arr) / sizeof(arr[0]); + + printf(" The given array is \n"); + printArray(arr, arr_size); + + mergeSort(arr, 0, arr_size - 1); + + printf("\nSorted array is \n"); + printArray(arr, arr_size); + return 0; +} \ No newline at end of file From df734f9e9c3f3a10a7840784cd4d31afdfd89b33 Mon Sep 17 00:00:00 2001 From: kittu-20 Date: Fri, 9 Oct 2020 19:40:14 +0530 Subject: [PATCH 3/5] Added the KMP search Algo --- cpp/sanyamSearchingPattern.cpp | 68 ++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 cpp/sanyamSearchingPattern.cpp diff --git a/cpp/sanyamSearchingPattern.cpp b/cpp/sanyamSearchingPattern.cpp new file mode 100644 index 00000000..c3b5f1b5 --- /dev/null +++ b/cpp/sanyamSearchingPattern.cpp @@ -0,0 +1,68 @@ +#include + //KMP Agorithm +void computeLPSArray(char* pat, int M, int* lps); +void KMPSearch(char* pat, char* txt) +{ + int M = strlen(pat); + int N = strlen(txt); + + int lps[M]; + computeLPSArray(pat, M, lps); + + int i = 0; + int j = 0; + while (i < N) { + if (pat[j] == txt[i]) { + j++; + i++; + } + if (j == M) { + printf("Found pattern at index %d ", i - j); + j = lps[j - 1]; + } + + else if (i < N && pat[j] != txt[i]) { + + if (j != 0) + j = lps[j - 1]; + else + i = i + 1; + } + } +} +void computeLPSArray(char* pat, int M, int* lps) +{ + int len = 0; + + lps[0] = 0; + + int i = 1; + while (i < M) { + if (pat[i] == pat[len]) { + len++; + lps[i] = len; + i++; + } + else + { + + if (len != 0) { + len = lps[len - 1]; + + } + else + { + lps[i] = 0; + i++; + } + } + } +} + +int main() +{ + char txt[] = "ABABDABACDABABCABAB"; + char pat[] = "ABABCABAB"; + KMPSearch(pat, txt); + return 0; +} \ No newline at end of file From 88986b5e112a53cbfd6a9ffe2dbb724716ffe1f5 Mon Sep 17 00:00:00 2001 From: kittu-20 Date: Fri, 9 Oct 2020 19:53:17 +0530 Subject: [PATCH 4/5] Added the code for String frequency in given string --- cpp/sanyamStringFrequency.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 cpp/sanyamStringFrequency.cpp diff --git a/cpp/sanyamStringFrequency.cpp b/cpp/sanyamStringFrequency.cpp new file mode 100644 index 00000000..be3f0a55 --- /dev/null +++ b/cpp/sanyamStringFrequency.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; +int count(string a, string b, int m, int n) +{ + if ((m == 0 && n == 0) || n == 0) + return 1; + if (m == 0) + return 0; + if (a[m - 1] == b[n - 1]) + return count(a, b, m - 1, n - 1) + + count(a, b, m - 1, n); + else + return count(a, b, m - 1, n); +} +int main() +{ + string a = "abcabcadrewabc"; + string b = "abc"; + + cout << count(a, b, a.size(), b.size()) << endl; + + return 0; +} \ No newline at end of file From 76afa689835f1f86a2afe8edb49633d49f7d7fb9 Mon Sep 17 00:00:00 2001 From: kittu-20 Date: Tue, 13 Oct 2020 12:21:48 +0530 Subject: [PATCH 5/5] Added the code for heap sort --- .vscode/launch.json | 19 +++++++++++ cpp/heapsort.cpp | 61 +++++++++++++++++++++++++++++++++++ cpp/sanyamStringFrequency.cpp | 17 ++-------- 3 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 cpp/heapsort.cpp diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..3b7283b7 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,19 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(Windows) Launch", + "type": "cppvsdbg", + "request": "launch", + "program": "enter program name, for example ${workspaceFolder}/a.exe", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false + } + ] +} \ No newline at end of file diff --git a/cpp/heapsort.cpp b/cpp/heapsort.cpp new file mode 100644 index 00000000..b7a7901e --- /dev/null +++ b/cpp/heapsort.cpp @@ -0,0 +1,61 @@ +#include +using namespace std; + +void heapify(int arr[], int n, int i) +{ + int largest = i; + int l = 2*i + 1; + int r = 2*i + 2; + + + if (l < n && arr[l] > arr[largest]) + largest = l; + + + if (r < n && arr[r] > arr[largest]) + largest = r; + + + if (largest != i) + { + swap(arr[i], arr[largest]); + + + heapify(arr, n, largest); + } +} + + +void heapSort(int arr[], int n) +{ + + for (int i = n / 2 - 1; i >= 0; i--) + heapify(arr, n, i); + + + for (int i=n-1; i>0; i--) + { + + swap(arr[0], arr[i]); + heapify(arr, i, 0); + } +} + +/ +void printArray(int arr[], int n) +{ + for (int i=0; i using namespace std; -int count(string a, string b, int m, int n) -{ - if ((m == 0 && n == 0) || n == 0) - return 1; - if (m == 0) - return 0; - if (a[m - 1] == b[n - 1]) - return count(a, b, m - 1, n - 1) + - count(a, b, m - 1, n); - else - return count(a, b, m - 1, n); -} + int main() { - string a = "abcabcadrewabc"; - string b = "abc"; - cout << count(a, b, a.size(), b.size()) << endl; + cout <<"ssanyam"; return 0; } \ No newline at end of file