algorithms

algorithms Commit Details


Date:2013-02-17 18:50:00 (11 years 10 months ago)
Author:Natalie Adams
Branch:default
Commit:8b7c98b7acaa
Parents: 28e26cad1a6d
Message:Adding mergesort and binarysearch

Updating main
Updating project file
Changes:
Acpp/algorithms/src/BinarySearch.h (full)
Acpp/algorithms/src/MergeSort.h (full)
Mcpp/algorithms/algorithms.vcxproj (1 diff)
Mcpp/algorithms/algorithms.vcxproj.filters (1 diff)
Mcpp/algorithms/src/Algorithm.h (1 diff)
Mcpp/algorithms/src/SearchAlgorithm.h (1 diff)
Mcpp/algorithms/src/main.cpp (1 diff)

File differences

cpp/algorithms/algorithms.vcxproj
1515
1616
1717
18
1819
1920
2021
22
2123
2224
2325
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\Algorithm.h" />
<ClInclude Include="src\BinarySearch.h" />
<ClInclude Include="src\HeapSort.h" />
<ClInclude Include="src\InsertSort.h" />
<ClInclude Include="src\MedianSort.h" />
<ClInclude Include="src\MergeSort.h" />
<ClInclude Include="src\QuickSort.h" />
<ClInclude Include="src\SearchAlgorithm.h" />
<ClInclude Include="src\SortAlgorithm.h" />
cpp/algorithms/algorithms.vcxproj.filters
4444
4545
4646
47
48
49
50
51
52
4753
4854
<ClInclude Include="src\HeapSort.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\MergeSort.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\BinarySearch.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
cpp/algorithms/src/Algorithm.h
2828
2929
3030
31
32
33
34
35
3136
3237
3338
ss << "] ";
return ss.str();
}
std::vector<T> getContainer()
{
return this->_container;
}
};
#endif
cpp/algorithms/src/BinarySearch.h
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
#ifndef BINARYSEARCH_H
#define BINARYSEARCH_H
#include "SearchAlgorithm.h"
template <class T>
class BinarySearch : public SearchAlgorithm<T>
{
public:
virtual int Search(T item)
{
int low = 0;
int high = this->_container.size() - 1;
int idx;
while (low <= high)
{
idx = ( low + high ) / 2;
if ( item == this->_container.at(idx) )
return idx;
else if ( item < this->_container.at(idx) )
high = idx - 1;
else
low = idx + 1;
}
return -1;
}
};
#endif
cpp/algorithms/src/MergeSort.h
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
#ifndef MERGESORT_H
#define MERGESORT_H
#include "SortAlgorithm.h"
#include <vector>
template <class T>
class MergeSort : public SortAlgorithm<T>
{
public:
virtual void Sort()
{
this->_container = mergesort(this->_container);
}
std::vector<T> mergesort(std::vector<T> arr)
{
if (arr.size() <= 1)
return arr;
size_t middle = arr.size() / 2;
std::vector<T> left(arr.begin(), arr.begin() + middle);
std::vector<T> right(arr.begin() + middle, arr.end());
left = mergesort(left);
right = mergesort(right);
return merge(left, right);
}
std::vector<T> merge(std::vector<T> left, std::vector<T> right)
{
std::vector<T> res;
while (left.size() > 0 || right.size() > 0)
{
if (left.size() > 0 && right.size() > 0)
{
if (left.at(0) <= right.at(0))
{
res.push_back(left.at(0));
left.erase(left.begin());
} else {
res.push_back(right.at(0));
right.erase(right.begin());
}
} else if (left.size() > 0) {
res.push_back(left.at(0));
left.erase(left.begin());
} else if (right.size() > 0) {
res.push_back(right.at(0));
right.erase(right.begin());
}
}
return res;
}
};
#endif
cpp/algorithms/src/SearchAlgorithm.h
33
44
55
6
6
7
78
89
910
10
11
1112
1213
1314
#include "Algorithm.h"
class SearchAlgorithm
template <class T>
class SearchAlgorithm : public Algorithm<T>
{
public:
//void f() { this->
virtual void Search() = 0;
virtual int Search(T item) = 0;
};
#endif
cpp/algorithms/src/main.cpp
77
88
99
10
11
1012
1113
1214
1315
1416
15
17
1618
1719
1820
19
21
2022
2123
24
25
26
2227
#include "InsertSort.h"
#include "MedianSort.h"
#include "QuickSort.h"
#include "MergeSort.h"
#include "BinarySearch.h"
using namespace std;
int main()
{
QuickSort<int> sort1;
MergeSort<int> sort1;
//sort1.initContainer(create_vector<string>("test")("test2"));
//cout << sort1.ToString();
//vector<int> arr1 = create_vector<int>(
sort1.initContainer(create_vector<int>(3)(2)(3)(1));
sort1.initContainer(create_vector<int>(3)(2)(4)(3)(1));
sort1.Sort();
cout << sort1.ToString() << endl;
BinarySearch<int> search1;
search1.initContainer(sort1.getContainer());
cout << search1.Search(7) << endl;
}

Archive Download the corresponding diff file

Branches

Tags

Page rendered in 0.42642s using 14 queries.