Repository navigation
Expand file tree
/
Copy pathBubble_sort_time.cpp
More file actions
110 lines (96 loc) · 2.39 KB
/
Copy pathBubble_sort_time.cpp
File metadata and controls
110 lines (96 loc) · 2.39 KB
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
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
class Bubble_sort
{
private:
int *array;
int size;
public:
void setvalues(int n)
{
size = n;
array = new int[size];
for (int i = 0; i < size; i++)
{
array[i] = i + 2;
}
std::cout << std::endl;
}
void bubble_sort()
{
int temp;
bool iteration = false;
for (int i = 1; i < size; i++)
{
iteration = true;
for (int j = 0; j < size - i; j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
iteration = false;
}
}
if (iteration == true)
{
cout << "Array is sorted " << endl;
return;
}
}
}
void random_number(int n)
{
size = n;
array = new int[size];
int num;
srand(time(NULL));
for (int i = 0; i < size; i++)
{
num = (rand() % 10 + 1);
array[i] = num;
}
}
void print()
{
cout << "value are : ";
for (int i = 0; i < size; i++)
{
cout << array[i] << " ";
}
cout << "----------------------------------\n";
}
~Bubble_sort()
{
delete[] array;
}
};
int main()
{
Bubble_sort obj_1, obj_2;
int n = 5;
obj_1.setvalues(n);
std::cout << "Simple Array is:" << std::endl;
obj_1.print();
auto start = std::chrono::steady_clock::now();
obj_1.bubble_sort();
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
float ms = elapsed_seconds.count() * 1000;
std::cout << "Sorting took " << ms << " ms" << std::endl;
obj_2.random_number(n);
std::cout << "Array of random numbers before sort:" << std::endl;
obj_2.print();
start = std::chrono::steady_clock::now();
obj_2.bubble_sort();
end = std::chrono::steady_clock::now();
elapsed_seconds = end - start;
ms = elapsed_seconds.count() * 1000;
std::cout << "Array of random numbers after sort:" << std::endl;
obj_2.print();
std::cout << "Sorting took " << ms << " ms" << std::endl;
return 0;
}