forked from Sissuire/Grokking-Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UtilsGrokkingAlgo.cpp
127 lines (106 loc) · 2.22 KB
/
UtilsGrokkingAlgo.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
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
#include <climits>
#include <cstring>
#include "GrokkingAlgorithms.h"
using namespace std;
//template<typename T> void PrintData(const T &data)
//{
// auto iter = data.begin();
// while (iter != data.end())
// cout << *iter++ << ", ";
// cout << endl;
//
// return;
//}
//
//template<typename T> void PrintData(T *data, int num)
//{
// int cnt = 0;
// for (; cnt != num; ++cnt)
// cout << data[cnt] << ", ";
//
// return;
//}
/****************************************************************************
int InBinarySearch(const int *array, int pos1, int pos2, int val)
{
// Binary Search for recursive computation
int pos = -1;
int j = (pos1 + pos2) / 2;
if (pos1 > pos2);
else
{
if (val == array[j])
pos = j;
else
if (val < array[j])
pos = InBinarySearch(array, pos1, j - 1, val);
else
if (val > array[j])
pos = InBinarySearch(array, j + 1, pos2, val);
}
return pos;
}
****************************************************************************/
void SelectionSort(int *array, int num)
{
int val = INT_MAX;
int pos = 0;
int j, counter = 0;
int temp;
while (counter != num)
{
val = INT_MAX;
for (j = counter; j != num; ++j)
{
if (val > array[j])
{
val = array[j];
pos = j;
}
}
temp = array[counter];
array[counter] = val;
array[pos] = temp;
++counter;
}
return;
}
void Merge(int *array, int num, int pos)
{
/*
* function for Merge Sort
*
* merge two arrays | [0, pos) & [pos, num)
*/
int *newArray = (int *)malloc(num * sizeof(int));
int i = 0, j = pos, k = 0;
while ((i != pos) && (j != num))
{
if (array[i] < array[j])
newArray[k++] = array[i++];
else
newArray[k++] = array[j++];
}
while (i != pos)
newArray[k++] = array[i++];
while (j != num)
newArray[k++] = array[j++];
memcpy(array, newArray, num * sizeof(int));
free(newArray);
return;
}
std::map<std::string, int>::iterator find_lowest_cose_node(std::map < std::string, int> &cost, std::map<std::string, int> &counter)
{
std::map<std::string, int>::iterator iter = cost.begin(), ret = cost.end();
int minCost = INT_MAX;
while (iter != cost.end())
{
if (iter->second <= minCost && counter[iter->first] == 0)
{
ret = iter;
minCost = iter->second;
}
++iter;
}
return ret;
}