-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBubble_sort.cpp
More file actions
50 lines (49 loc) · 1 KB
/
Bubble_sort.cpp
File metadata and controls
50 lines (49 loc) · 1 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
#include<iostream>
#include<unistd.h>
#include<cstdlib>
using namespace std;
int main()
{
int n, i, j, temp;
int* arr;
cout << "Enter the Size: ";
cin >> n;
arr = new int[n];
for (i = 0; i < n; i++)
{ cout << "Enter the value [" << i + 1 << "] =";
cin >> arr[i];
}
sleep(2);
cout << "Old Array [";
for (i = 0; i < n; i++)
{ cout << arr[i];
if (i < n - 1)
cout << ",";
}
cout <<"]" << endl;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
sleep(2);
cout << "\nArray Sorted Successfully!\n";
cout << "\nThe New Array is: [";
for (i = 0; i < n; i++)
{
cout << arr[i];
if (i < n - 1)
cout << ",";
}
cout << "]" << endl;
std::cin.get();
std::cin.get();
return 0;
}