-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble_sort.cpp
74 lines (55 loc) · 1.25 KB
/
bubble_sort.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
#include<iostream>
int b_sort(int arr[],int size);
int main()
{
int values[] = {10,1,9,2,8,3,7,4,6,5};
b_sort(values,sizeof(values)/sizeof(values[0]));
return 0;
}
int b_sort(int arr[],int size)
{
//it should be performed until whole array is sorted
bool sorted = true;
for (int i = 0 ; i<size-1; i++)
{
if (arr[i]>arr[i+1])
{
sorted = false;
}
while (!sorted)
{
for(int j =0; j<size-1;j++)
{
if(arr[j]>arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
sorted = true;
}
}
int new_arr[size];
std::copy(arr,arr+size,new_arr);
for(int num:new_arr)
{
std::cout<<num<<" ";
}
return 0;
}
void correct_b_sort(int arr[],int size)
{
bool sorted = true;
do
{
for(int i = 0;i<=size-1;i++)
{
if(arr[i]>arr[i+1])
{
std::swap(arr[i],arr[i+1]);
sorted = false;
}
}
} while (!sorted);
}