-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathbubbleSort.cpp
More file actions
68 lines (65 loc) · 1.32 KB
/
bubbleSort.cpp
File metadata and controls
68 lines (65 loc) · 1.32 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
63
64
65
66
67
68
#include<iostream>
#include<stdio.h>
#include<conio.h>
#define max 10
using namespace std;
int arr[max];
int last = -1;
void insert(){
if(last == max-1){
cout<<"Array is full"<<endl;
}
else{
last++;
cout<<"Enter value : ";
cin>>arr[last];
}
}
void swap(int f,int s){
int c = arr[f];
arr[f] = arr[s];
arr[s] = c;
}
void bubble_sort(){
if(last == -1){
cout<<"Array is empty :"<<endl;
}
else{
int n = last-1;
for(int i=0;i<=n;i++){
for(int j=0;j<=n-i;j++){
if(arr[j]>arr[j+1]){
swap(j,j+1);
}
}
}
}
}
void display(){
if(last == -1){
cout<<"Array is empty"<<endl;
}
else{
for(int i=0;i<=last;i++){
cout<<arr[i]<<" ";
}
}
cout<<endl;
}
int main(){
int ch;
bool c = true;
while(c){
cout<<"1 for insert value \n2 for sorting \n3 for display \n4 for exit"<<endl;
cout<<"Enter your choice : ";
cin>>ch;
switch(ch){
case 1:{insert();break;}
case 2:{bubble_sort();break;}
case 3:{display();break;}
case 4:{c = false;break;}
default:{cout<<"You enter wrong option"<<endl;}
}
}
return 0;
}