Skip to content

Commit

Permalink
Merge pull request #3447 from goyalsaksh/patch-1
Browse files Browse the repository at this point in the history
Create SakshiGoyal_BubbleSort.cpp
  • Loading branch information
ZoranPandovski authored Oct 29, 2022
2 parents 57b249f + 812506d commit 6c5a341
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions sort/SakshiGoyal_BubbleSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

#include <bits/stdc++.h>
using namespace std;

// Bubble sort
void BubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++){
for (j = 0; j < n - i - 1; j++){
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}
}
}

// print an array
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}


int main()
{
int n;
cout<<"Enter number of elements :";
cin>>n;
int arr[n];
cout<<"Enter elements:";
for(int i=0;i<n;i++){
cin>>arr[i];
}
BubbleSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}

0 comments on commit 6c5a341

Please sign in to comment.