Skip to content

Add files via upload #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/quicksort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
#include <vector>
#include <string>

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);

}
112 changes: 112 additions & 0 deletions src/searchPatterns.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//
// Created by Sithum on 2021-01-01.
//

#include "clientClass.h"

#include <iostream>



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++;

}
}
}


}
};