mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Merge pull request #117 from VARoDeK/master
Optimize bubble sort algorithm, solve run time error in insertion sort
This commit is contained in:
commit
9f027ac6ff
@ -7,12 +7,14 @@ using namespace std;
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
cout << "Enter the amount of numbers to sort: ";
|
short swap_check=0;
|
||||||
|
cout << "Enter the amount of numbers to sort: ";
|
||||||
cin >> n;
|
cin >> n;
|
||||||
vector<int> numbers;
|
vector<int> numbers;
|
||||||
cout << "Enter " << n << " numbers: ";
|
cout << "Enter " << n << " numbers: ";
|
||||||
int num;
|
int num;
|
||||||
//Input
|
|
||||||
|
//Input
|
||||||
for(int i=0; i<n; i++)
|
for(int i=0; i<n; i++)
|
||||||
{
|
{
|
||||||
cin >> num;
|
cin >> num;
|
||||||
@ -22,13 +24,19 @@ int main()
|
|||||||
//Bubble Sorting
|
//Bubble Sorting
|
||||||
for(int i=0; i<n; i++)
|
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])
|
if(numbers[j]>numbers[j+1])
|
||||||
{
|
{
|
||||||
|
swap_check=1;
|
||||||
swap(numbers[j], numbers[j+1]);
|
swap(numbers[j], numbers[j+1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(swap_check == 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Output
|
//Output
|
||||||
@ -43,4 +51,5 @@ int main()
|
|||||||
cout << numbers[i] << endl;
|
cout << numbers[i] << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ int main()
|
|||||||
cout<<"\nEnter the length of your array : ";
|
cout<<"\nEnter the length of your array : ";
|
||||||
cin>>n;
|
cin>>n;
|
||||||
int Array[n];
|
int Array[n];
|
||||||
cout<<"\nEnter the Numbers for Unsorted Array : ";
|
cout<<"\nEnter any "<<n<<" Numbers for Unsorted Array : ";
|
||||||
|
|
||||||
//Input
|
//Input
|
||||||
for(int i=0; i<n; i++)
|
for(int i=0; i<n; i++)
|
||||||
@ -36,5 +36,6 @@ int main()
|
|||||||
{
|
{
|
||||||
cout<<Array[i]<<"\t";
|
cout<<Array[i]<<"\t";
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user