Merge pull request #117 from VARoDeK/master

Optimize bubble sort algorithm, solve run time error in insertion sort
This commit is contained in:
Ashwek Swamy 2019-02-09 13:26:28 +05:30 committed by GitHub
commit 9f027ac6ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View File

@ -7,12 +7,14 @@ using namespace std;
int main()
{
int n;
cout << "Enter the amount of numbers to sort: ";
short swap_check=0;
cout << "Enter the amount of numbers to sort: ";
cin >> n;
vector<int> numbers;
cout << "Enter " << n << " numbers: ";
int num;
//Input
//Input
for(int i=0; i<n; i++)
{
cin >> num;
@ -22,13 +24,19 @@ int main()
//Bubble Sorting
for(int i=0; i<n; i++)
{
for(int j=0; j<n-1; j++)
swap_check=0;
for(int j=0; j<n-1-i; j++)
{
if(numbers[j]>numbers[j+1])
{
swap_check=1;
swap(numbers[j], numbers[j+1]);
}
}
if(swap_check == 0)
{
break;
}
}
//Output
@ -43,4 +51,5 @@ int main()
cout << numbers[i] << endl;
}
}
return 0;
}

View File

@ -9,7 +9,7 @@ int main()
cout<<"\nEnter the length of your array : ";
cin>>n;
int Array[n];
cout<<"\nEnter the Numbers for Unsorted Array : ";
cout<<"\nEnter any "<<n<<" Numbers for Unsorted Array : ";
//Input
for(int i=0; i<n; i++)
@ -36,5 +36,6 @@ int main()
{
cout<<Array[i]<<"\t";
}
return 0;
}