forked from hiamsevval/Top-10-Frequent-Words
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting_heap.cpp
86 lines (77 loc) · 1.76 KB
/
sorting_heap.cpp
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
#include "sorting_heap.h"
#include<iostream>
#include <fstream>
#include<string>
void sorting_heap::swap(heapElem &a, heapElem &b) //swaps strings and numbers of a and b
{
std::string tempW = a.name_w;
int tempN = a.number_w;
a.name_w = b.name_w;
b.name_w = tempW;
a.number_w = b.number_w;
b.number_w = tempN;
}
void sorting_heap::heapify(heapElem sortArr[], int n, int i)
{
int big = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && sortArr[left].number_w < sortArr[big].number_w)
{
big = left;
}
if (right < n && sortArr[right].number_w < sortArr[big].number_w)
{
big = right;
}
if (big != i)
{
swap(sortArr[i], sortArr[big]);
heapify(sortArr, n, big);
}
}
void sorting_heap::sortHeap(heapElem sortArr[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
{
heapify(sortArr, n, i);
}
for (int i = n - 1; i >= 0; i--)
{
swap(sortArr[0], sortArr[i]);
heapify(sortArr, i, 0);
}
}
void sorting_heap::heapSortingAll(hashT heapArr, std::string stops)
{
int k = 0; //total unique word number
nodeType *current;
for (int i = 0; i < heapArr.HTSize; i++) //travelling in the hash
{
current = heapArr.hTable[i];
while (current != NULL)
{
arr[k].name_w = current->info1; //assigns word
arr[k].number_w = current->info2; //assigns number of the word
current = current->link;
k++;
}
}
sortHeap(arr, k);
int counter = 0; //top10 counter
int i = 0;
while (counter < 10)
{
if (stops.find(arr[i].name_w) != std::string::npos) //checks stopword
{
i++;
continue;
}
else //prints if it is not in stopwords
{
std::cout << "<" << arr[i].name_w << "> <" << arr[i].number_w << ">" << std::endl;
i++;
counter++;
}
}
}