-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubble Sort || Ascending order & Descending order.
52 lines (49 loc) · 1.29 KB
/
Bubble Sort || Ascending order & Descending order.
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
#include<iostream>
#include<cmath>
using namespace std;
//This Code for Ascendiong order Sort.
void printsortedarray(int arr[],int n){ //This function for printing sorted array.
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
}
void bubblesort(int arr[],int n){ //This function for sorting the array.
for(int i=0; i<n-1; i++){
for(int j=0; j<n-i-1; j++){
if(arr[j]>arr[j+1]){
swap(arr[j],arr[j+1]);
}
}
}
printsortedarray(arr,n);
}
int main(){
int arr[]= {5,4,1,2,3};
int n=sizeof(arr)/sizeof(int);
bubblesort(arr,n);
return 0;
}
_______________________________________________________________________________________________________________
_______________________________________________________________________________________________________________
//This Code for Descending order Sort.
void printsortedarray(int arr[],int n){ //This function for printing sorted array.
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
}
void bubblesort(int arr[],int n){ //This function for sorting the array.
for(int i=0; i<n-1; i++){
for(int j=0; j<n-i-1; j++){
if(arr[j]<arr[j+1]){
swap(arr[j],arr[j+1]);
}
}
}
printsortedarray(arr,n);
}
int main(){
int arr[]= {5,4,1,2,3};
int n=sizeof(arr)/sizeof(int);
bubblesort(arr,n);
return 0;
}