Bubble Sort now uses vectors

This commit is contained in:
William Grigor 2018-12-21 23:01:52 -06:00
parent 16e1bcdb52
commit 672f9dc57f

View File

@ -1,19 +1,22 @@
//Bubble Sort //Bubble Sort
#include<iostream> #include<iostream>
#include<vector>
using namespace std; using namespace std;
int main() int main()
{ {
int n; int n;
cout << "Enter the amount of numbers to sort: ";
cin >> n; cin >> n;
int Array[n]; vector<int> numbers;
cout<<"\nEnter any 6 Numbers for Unsorted Array : "; cout << "Enter " << n << " numbers: ";
int num;
//Input //Input
for(int i=0; i<n; i++) for(int i=0; i<n; i++)
{ {
cin>>Array[i]; cin >> num;
numbers.push_back(num);
} }
//Bubble Sorting //Bubble Sorting
@ -21,19 +24,23 @@ int main()
{ {
for(int j=0; j<n-1; j++) for(int j=0; j<n-1; j++)
{ {
if(Array[j]>Array[j+1]) if(numbers[j]>numbers[j+1])
{ {
int temp=Array[j]; swap(numbers[j], numbers[j+1]);
Array[j]=Array[j+1];
Array[j+1]=temp;
} }
} }
} }
//Output //Output
cout<<"\nSorted Array : "; cout<<"\nSorted Array : ";
for(int i=0; i<n; i++) for(int i=0; i<numbers.size(); i++)
{ {
cout<<Array[i]<<"\t"; if(i != numbers.size() -1)
{
cout << numbers[i] << ", ";
}else
{
cout << numbers[i] << endl;
}
} }
} }