-
Notifications
You must be signed in to change notification settings - Fork 2
/
bubblesort.cpp
93 lines (73 loc) · 1.69 KB
/
bubblesort.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
/*This program uses random variables to insert the elements of the array.
If you want to see the result, uncomment the commented lines. */
#include <iostream>
#include <ctime>
#include <stdlib.h>
using namespace std;
class Bubble
{
int size;
int a[200000];
public:
Bubble() //constructor to get the size at the time of creating the object
{
cout<<"Enter the size: ";
cin >> size;
}
void swap(int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
}
void getElements()
{
int i;
//cout<<"Put the elements in: " <<endl;
for(i=0; i<size; i++)
a[i] = rand() % 100; // Generating random numbers between 0 to 99 and assigning it to array.
}
//Algorithm for bubble sort.
void bubbleSort()
{
int i,j;
for(i=0; i<size; i++)
for(j=0; j<size-1-i; j++)
{
if(a[j] > a[j+1])
{
swap(a[j], a[j+1]);
}
}
}
void display()
{
int i;
for(i=0; i<size; i++)
cout << a[i] << " ";
}
};
int main()
{
Bubble obj;
obj.getElements();
//cout<<"Unsorted elements are: " << endl;
//obj.display();
clock_t start, end;
double total_time;
start = clock();
obj.bubbleSort();
end = clock();
total_time =(double) (end - start)/(CLOCKS_PER_SEC);
//cout<<endl << "Sorted elements are: " << endl;
//obj.display();
cout<< endl << "The time taken is " << total_time <<" seconds."<<endl;
return 0;
}
// You may also want to check out the graph for the program the link for which is given below.
// Copy the url and paste it in your browser's address bar to see the graph.
/************* GRAPH ***************/
/*
https://drive.google.com/file/d/0B4UekpEoeMP8RjNxYThtcERBUTg/view?usp=sharing
*/