-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassified.cpp
More file actions
137 lines (118 loc) · 3.79 KB
/
Classified.cpp
File metadata and controls
137 lines (118 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "Classified.h"
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cctype>
#include <bits/stdc++.h>
#include <ctime>
#include "Unclassified.h"
using namespace std;
Classified :: Classified(int num) : Unclassified(num) //constructor
{
n=num;
orderArray=new string[n];
}
Classified :: ~Classified() //destructor
{
delete[] orderArray;
}
string Classified :: getword(int num){ //getter
return orderArray[num];
}
void Classified :: setword(int num,string word){ //setter
float start=clock();
orderArray[num]=word;
timeOfOrderArray+=timecount(start);
}
void Classified :: removeClassified(int num) //deletes element from the ordered array
{
orderArray[num]='\0'; //sets the given element to \0;
}
void Classified :: swap (int s1, int s2)//swaps the content of 2 elements of the array
{
string t=orderArray[s1];
orderArray[s1]=orderArray[s2];
orderArray[s2]=t;
}
int Classified :: Partition (string orderArray[],int low,int high) //devides the array in to smallers to classify them
{
string pivot = orderArray[high]; //sets the pivot to the value of the last position
int i = (low - 1); //sets i to first position -1
for (int j = low; j < high; j++)
{
if (orderArray[j].compare(pivot)<0) //if the content of j is smaller than pivot, then swap i,j
{
i++;
swap(i,j);
}
}
swap(i+1 , high); //swaps the content of i+1 with the content of the last position of array
return (i + 1);
}
int Classified :: partition_r(string orderArray[], int low, int high) //calls the partition function with a random pivot
{
srand(time(NULL));
int random = low + rand() % (high - low);
swap(random,high);
return Partition(orderArray, low, high);
}
void Classified :: OrderClassified(int low,int high) //calssifies the elements of the array //low, high: borders of array
{
if (low < high)
{
int pi = partition_r(orderArray, low, high); //calls partition r function and sets the result in to pi
OrderClassified(low, pi - 1); //calls itself with pi as upper limit of array
OrderClassified(pi + 1, high); //calls itself with pi as lower limit of array
}
}
void Classified :: searchClassified (string *Q) //searches all the words of Q array in the main array using binary search and counts the number of impressions of each one
{
OrderClassified(0, this->n-1); //calls the order function to clssify the elements before the search
int L=0,R=n,M,count[1000]; //L, R: the borders of the array of the binary search
bool D=false; //returns true if it finds the word, false
for (int j=0;j<1000;j++){
count[j]=0; //sets the elements of the count array to 0
}
float begin =clock(); //starts the clock
for(int i=0;i<1000;i++)
{
while(D==false && L<=R)
{
M=(L+R)/2; //sets M to the middle element of the array
if (orderArray[M]==Q[i]) //compares the Q[i] to all the elements of the array until it finds the word
{
D=true;
int p=M+1;
while(orderArray[M]==Q[i]) //starts serial search to find all the impression after M
{
count[i]++;
M--;
}
M=p-1;
while(orderArray[p]==Q[i]) //starts serial search to find all the impression before M
{
count[i]++;
p++;
}
}
else if (orderArray[M]<Q[i]){
L=M+1; // picks the right array
}
else{
R=M-1; //picks the left array
}
}
L=0;
R=n;
D=false;
}
float time=timecount(begin); // saves the time of function
cout<<endl<<"\t\t\t\t\tCLASSIFIED"<<endl;
cout<<"time of creation: "<<timeOfOrderArray<<endl;
for (int i=0;i<1000;i++)
{
cout<<Q[i]<<" "<<count[i]<<endl; //prints the words and their number of impression
}
cout<<endl<<"time: "<<time<<endl; //prints the time
}