-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path4.cpp
More file actions
39 lines (39 loc) · 829 Bytes
/
Copy path4.cpp
File metadata and controls
39 lines (39 loc) · 829 Bytes
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
// array
// Bubble sort
#include<iostream>
using namespace std;
int main(){
int a[10],i,n,j,temp,sum=0;
cout<<"Enter the no. of array elements: ";
cin>>n;
cout<<"Enter the array elements:\n";
for(i=0;i<n;i++){
cout<<"Element "<<i+1<<": ";
cin>>a[i];
sum+=a[i];
}
cout<<"Unsorted Array:\n";
cout<<"[";
for(i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<"]\n";
//bubble sort
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){
if(a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
cout<<"Sorted Array:\n";
cout<<"[";
for(i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<"]"<<"\n";
cout<<"Sum of array elements = "<<sum;
return 0;
}