mirror of
https://github.com/RolandWH/ceesort.git
synced 2025-03-14 20:31:36 +00:00
bubblesort: stop once list is sorted
This commit is contained in:
parent
a3ea40e9fe
commit
e6266f6137
@ -5,12 +5,16 @@
|
||||
|
||||
void bubblesort(int* arr, size_t n)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
bool swapped = true;
|
||||
|
||||
for (int i = 0; i < n && swapped; i++)
|
||||
{
|
||||
for (int elem = 0; elem < n - 1 - i; elem++)
|
||||
{
|
||||
swapped = false;
|
||||
if (arr[elem] > arr[elem + 1])
|
||||
{
|
||||
swapped = true;
|
||||
int temp = arr[elem];
|
||||
arr[elem] = arr[elem + 1];
|
||||
arr[elem + 1] = temp;
|
||||
|
@ -40,7 +40,8 @@ int main(int argc, char** argv)
|
||||
"Please pick a mode of operation: "
|
||||
);
|
||||
scanf("%s", choice_str);
|
||||
// Add checking that number is in range
|
||||
|
||||
// TODO: Add checking that number is in range
|
||||
while (!isint(choice_str))
|
||||
{
|
||||
printf("Sorry but you must enter a number between 1 and 2\n");
|
||||
@ -70,6 +71,7 @@ int main(int argc, char** argv)
|
||||
else if (choice == 2)
|
||||
{
|
||||
bubblesort(arr, n);
|
||||
|
||||
printf("Here is your sorted list:\n");
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user