-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.cpp
More file actions
62 lines (57 loc) · 1.16 KB
/
Copy pathbubbleSort.cpp
File metadata and controls
62 lines (57 loc) · 1.16 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
#include <iostream>
using namespace std;
void input(int arr[], int &size)
{
cin>> size;
for(int i=0;i<size;i++)
cin>> arr[i];
}
void swap (int &a, int &b)
{
int t=a;
a=b;
b=t;
}
/*
void bubbleSort(int arr[], int size)
{
int i,j;
bool haveSwap=false;// cờ hiệu báo true khi đã swap
for(int i=0; i<size-1; i++)
{
haveSwap=false;
for(int j=0; j<size-i-1;j++)
{
if(arr[j]>arr[j+1])
{
swap(arr[j], arr[j+1]);
haveSwap=true;
}
if (haveSwap=false)// khi mảng đã được xếp thì không hoán vị tiếp
break;
}
}
}
*/
void bubbleSort2(int arr[], int size)
{
for(int i=0; i<size-1; i++)
{
for(int j=size-10; j>i;j--)
if(arr[j]<arr[j-1])
swap(arr[j],arr[j-1]);
}
}
void output(int arr[], int size)
{
bubbleSort2(arr, size);
for(int i=0; i<size;i++)
cout<<arr[i]<<" ";
}
int main()
{
int arr[100], size;
input(arr, size);
output(arr, size);
return 0;
}