From a159c2fe5605c4b9dd2038ac3e30321139f4a780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=B7=83=E0=B7=92=E0=B6=AD=E0=B7=94=E0=B6=B8=E0=B7=8A?= <56906402+SHandapangoda@users.noreply.github.com> Date: Mon, 17 May 2021 17:01:12 +0530 Subject: [PATCH 1/2] Add files via upload --- src/searchPatterns.cpp | 112 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/searchPatterns.cpp diff --git a/src/searchPatterns.cpp b/src/searchPatterns.cpp new file mode 100644 index 00000000..75a728e0 --- /dev/null +++ b/src/searchPatterns.cpp @@ -0,0 +1,112 @@ +// +// Created by Sithum on 2021-01-01. +// + +#include "clientClass.h" + +#include + + + +class patternSearchNaive { + + char* pattern; + char* text; + // naive bias pattern search +public: void search(char* pattern, char* text) { + int txt = strlen(text); + int pat = strlen(pattern); + + for (int i = 0; i <= txt - pat; i++) { + // for pattern matching + for (int j = 0; j < pat; j++) { + if (text[i + j] != pattern[j]) { + break; + } + // if pattern matches + if (j = pat) { + std::cout << "Pattern found" << i << std::endl; + } + } + } + +} + +}; + +//for Knuth morris pratt algorithm +class patternSearchKMP { + + //occurance of text[],pattern[] +public: + void search(char* pattern, char* text) { + + int txt = strlen(text); + int pat = strlen(pattern); + + // to hold longest prefix suffix + // value for pattern + int lps[1]; + + //preprocess the pattern(calculate lps[] array) + LPS(pattern, pat, lps); + + int index = 0; + int j_index = 0; + + while (index < txt) { + if (pattern[j_index] == text[index]) { + j_index++; // index for pattern + index++; // index for text + } + + if (j_index == pat) { + std::cout << "Index found " << index - j_index << std::endl; + j_index = lps[j_index - 1]; + } + + //mismatch after pattern match + else if (index < pat && pattern[j_index] != text[index]) { + if (j_index != 0) { + j_index = lps[j_index - 1]; + } + else { + + index = index++; + } + } + } + + } + +public: void LPS(char* pattern, int pat, int* lps) { + + int lenght = 0; + + lps[0] = 0; + + // the loop calculates lps[index] for index = 1 to pat-1 + int index = 1; + + while (index < pat) { + if (pattern[index] == pattern[lenght]) { + + lenght++; + lps[index] = lenght; + index++; + } + else { + if (lenght != 0) { + lenght = lps[lenght - 1]; + } + else { + lps[index] = 0; + index++; + + } + } + } + + +} +}; \ No newline at end of file From db678f9ba75bd54ffa66ba2598044c6d1bcd42f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=B7=83=E0=B7=92=E0=B6=AD=E0=B7=94=E0=B6=B8=E0=B7=8A=20?= =?UTF-8?q?=E0=B7=84=E0=B6=AF=E0=B6=B4=E0=B7=8F=E0=B6=B1=E0=B7=8A=E0=B6=9C?= =?UTF-8?q?=E0=B7=9C=E0=B6=A9?= Date: Sun, 18 Jul 2021 08:57:23 +0530 Subject: [PATCH 2/2] Add files via upload --- src/quicksort.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/quicksort.cpp diff --git a/src/quicksort.cpp b/src/quicksort.cpp new file mode 100644 index 00000000..a41cb257 --- /dev/null +++ b/src/quicksort.cpp @@ -0,0 +1,49 @@ +#include +#include +#include + +using namespace std; +void swap(int* a, int* b){ + int store = *a; + *a = *b; + *b = store; +} + +int part(int array[], int high, int low){ + int pin = array[high]; + int index = (low - 1); + + for(int i = low; i <= high; i++){ + if(array[i] <= pin){ + index++; + swap(&array[index], &array[high]); + } + } + swap(&array[index + 1], &array[high]); + return (index + 1); +} + +void quicksort(int array[], int high, int low){ + if(low < high){ + int pi = part(array,high,low); + + quicksort(array, low, pi - 1 ); + quicksort(array, pi + 1, high); + } +} + +void printArray(int array[], int size){ + int index; + for (index =0; index < size; index++){ + cout << "" << array[index] << endl; + cout << endl; + } +} + +int main(){ + int array[] = {25,17,3,1,22}; + int n = sizeof(array) / sizeof(array[0]); + quicksort(array, n-1, 0); + printArray(array, n); + +} \ No newline at end of file