-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubble_Sort.h
59 lines (53 loc) · 1.28 KB
/
Bubble_Sort.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
#pragma once
#include "SortBase.h"
template <typename T>
class BubbleSort : public SortBase<T>
{
public:
BubbleSort() : SortBase<T>(returnNameOfSort()) {}
const char* returnNameOfSort() const
{
return "Bubble sort";
}
void sort() // optimized version of the bubble sort; there's room for implementing "raw" bubble sort
{
this->startTrackingTime();
for (int i = 0; i < this->_length; i++)
{
bool sorted = true;
for (int j = 0; j < this->_length - i - 1; j++)
if (this->_arr[j] > this->_arr[j + 1])
{
swap(this->_arr[j], this->_arr[j + 1]);
sorted = false;
}
if (sorted) break;
}
this->stopAndPrintTrackingTime();
}
void informativeSort()
{
for (int i = 0; i < this->_length; i++)
{
bool sorted = true;
cout << "Going from " << i << " for " << this->_length - i - 1 << " times. " << i + 1 << ". loop: \n";
for (int j = 0; j < this->_length - i - 1; j++)
{
cout << "Comparing " << this->_arr[j] << " and " << this->_arr[j + 1] << ".";
if (this->_arr[j] > this->_arr[j + 1])
{
cout << " Switched.";
swap(this->_arr[j], this->_arr[j + 1]);
sorted = false;
}
cout << "\n";
}
cout << "Finished " << i + 1 << ". loop.\n\n";
if (sorted)
{
cout << "Already sorted.";
return;
}
}
}
};