Skip to content

BubbleSort Added :I made the changes you told me to do. Tell me any other changes i need to make #37

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include<iostream>
/**
*Bubble sort
*Sometimes referred to as sinking sort, is a simple sorting algorithm that
*repeatedly steps through the list to be sorted, compares each pair of
*adjacent items and swaps them if they are in the wrong order.
*Complexity for this algorithm will be O(n^2)
*/
using namespace std;


/* Function for bubble sort*/

template<typename T>

void bubble_sort (T arr[], int length) {
int i, j;
T temp;
for (i = 0; i < length; ++i)
for (j = 0; j < length - i - 1; ++j)
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr [j + 1];
arr[j + 1] = temp;
}
}

/*End of function*/

int main () {
int length;
cout << "Enter the number of elements you want to sort \n";
cin >> length;
int arr[length]; //Defining an array of specific length
for (int i = 0; i < length; ++i) {
cout << "Enter element number " << i+1 << endl;
cin >> arr[i];
}
bubble_sort(arr, length); //Sorting the entered array
cout << " { ";
for (int i = 0; i < length; ++i) {

cout << arr[i] ;
if (i != length-1)
{
cout << ", " ;
}
}
cout << " }";
return 0;
}