Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 41 additions & 47 deletions bubble_sort.cpp
Original file line number Diff line number Diff line change
@@ -1,47 +1,41 @@
// demonstrating bubble sort

#include<iostream>
using namespace std;

void bubble_sort(int arr[], int n)
{
int i,j;
int temp;

//bubble sort
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
int main()
{
int n,i,k;
cout<<"enter size:";
cin>>n;
int arr[n];

cout<<"enter :";
for(i=0;i<n;i++)
{
cin>>arr[i];
}

bubble_sort(arr,n);

cout<<"\n the sorted array: \n";
for(i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}

return 0;
}

#include <iostream>
#include <vector>
using namespace std;
void bubble_sort(vector<int>&input){
int i,j;
for(i=0;i<input.size()-1;i++){
for(j=0;j<input.size()-i-1;j++){
if(input[j]>input[j+1]){
/*swapping the elements without using an extra variable*/
input[j]=input[j]+input[j+1];
input[j+1]=input[j]-input[j+1];
input[j]=input[j]-input[j+1];
}
}
}

}
int main()
{
vector<int>v;
int n;
cout<<"Enter the number of elements to be entered:\n";
cin>>n;

/*taking inputs of the elements from the user*/
for(int i=0;i<n;i++){
int ele;
cin>>ele;
v.push_back(ele);/*entering the elements in the vector*/
}
bubble_sort(v);/*this will sort the elements of the vector*/

/*printing the elements after sorting*/
for(int i=0;i<v.size();i++){
cout<<v[i]<<" ";
}


return 0;
}