-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunsorted-array.cpp
More file actions
48 lines (41 loc) · 1013 Bytes
/
Copy pathunsorted-array.cpp
File metadata and controls
48 lines (41 loc) · 1013 Bytes
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
#include "unsorted-array.h"
#include <iostream>
#include <string>
using namespace std;
UnsortedArray::UnsortedArray(int size) {
this->size = size;
data = new string[size + 1];
for (int i = 0; i < size; i++)
data[i] = "";
}
UnsortedArray::~UnsortedArray(){
delete [] data;
}
void UnsortedArray::insert(string *A) {
for (int i = 0; i < size; i++) {
data[i] = A[i];
}
}
// Linear Search for Unsorted Array
int UnsortedArray::LinearSearch(string x) {
data[size] = x;
int i;
for (i = 0; x != data[i]; i++); //Using only one comparison to save time
if (i != size) {
return i;
}
return -1;
}
int *UnsortedArray::getIterations(int *keys, const int Size, const int Pairs) {
int *iterations = new int[Pairs];
for (int i = 0; i < Pairs; i++) {
iterations[i] = 0;
for (int j = keys[i] + 1; j < size; j++) { //start searching after the 1st value
if (data[keys[i]] == data[j]) {
iterations[i]++;
}
}
iterations[i]++;
}
return iterations;
}