-1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

main()
{
    int ctr, inner, outer, didSwap, temp;
    int nums[10];
    time_t t;

    srand(time(&t));

    for (ctr = 0; ctr < 10; ctr++) {
        nums[ctr] = (rand() % 99) + 1;
    }

    printf("\nHere is the list before the sort:\n");
    for (ctr = 0; ctr < 10; ctr++) {
        printf("%3d", nums[ctr]);
    }

    // Sorting the array
    for (outer = 0; outer < 9; outer++) {
        didSwap = 0;

        for (inner = outer + 1; inner < 10; inner++) {
            if (nums[inner] < nums[outer]) {
                temp = nums[inner];
                nums[inner] = nums[outer];
                nums[outer] = temp;
                didSwap = 1;
            }
        }

        if (didSwap == 0) {
            break;
        }
    }

    printf("\n\nHere is the list after sorting:\n");
    for (ctr = 0; ctr < 10; ctr++) {
        printf("%3d", nums[ctr]);
    }

    printf("\n");

    return 0;
}

It works most of the times but sometimes doesn't sort properly and sometimes doesn't sort at all.

P.S. If the code is incorrect then why does it work 85% of the times.

Snapshot of error enter image description here

bruno
  • 32,421
  • 7
  • 25
  • 37
jiVatx19
  • 21
  • 7
  • 1
    Find a specific case where it doesn't work, try to minimize the input set, and then use a *debugger* to step through your code statement by statement while monitoring variables and their values. – Some programmer dude Jan 02 '21 at 08:54
  • 1
    This is the same [code you posted 2 days ago](https://stackoverflow.com/questions/65518396/simple-bubble-sort-program-it-works-flawlessly-about-85-of-times-but-in-some-c). What is different in this question? – Blastfurnace Jan 02 '21 at 09:40
  • @Blastfurnace ah yes you are right – bruno Jan 02 '21 at 09:44
  • The variable `t` is not used. Use `srand(time(NULL));` instead. – Bo R Jan 02 '21 at 09:45
  • 1
    Hope my answer answers to your question about *sometimes does not sort*, but in any case to duplicate a question as you did is not the right way. Please choose the question having the answer you prefer and delete the other question, if you don't quickly we will do and may be not in the way you prefer, including deleting both your questions ... – bruno Jan 02 '21 at 10:00
  • 1
    Does this answer your question? [Simple Bubble sort program. It works flawlessly about 85% of times but in some cases it doesn't sort the list](https://stackoverflow.com/questions/65518396/simple-bubble-sort-program-it-works-flawlessly-about-85-of-times-but-in-some-c) – Dante R. Jan 02 '21 at 11:52
  • That was a bubble sort ques. this is a Selection Sort one. In that post a person suggested that it was a selection sort code and not a bubble sort one and so I asked it again under a correct label. – jiVatx19 Jan 02 '21 at 13:31
  • You should have edited that question to correct or improve it instead of asking this duplicate. – Blastfurnace Jan 02 '21 at 14:45

3 Answers3

1

For some reason, you break if no swap is done for any particular element, thus leaving the other elements unsorted:

if (didSwap == 0) {
    break;
}

You need to remove the above conditional to make it work and sort all the elements. There is no need of such conditional in the standard selection sort algorithm as well.

Jarvis
  • 8,494
  • 3
  • 27
  • 58
  • buddy, didSwap flag variable is outside the for loop and so that condition would turn out to be true only when an entire step is tested without sorting and not just the first element . Just try running my code and tell me why it works 85% of times then? – jiVatx19 Jan 02 '21 at 13:45
1

You are breaking the outer loop when there is no swap. This will not gonna work so remove the didSwap and remove the below code

if (didSwap == 0) {
    break;
}

and the program should work fine.

Suvajit Patra
  • 190
  • 1
  • 4
  • buddy, didSwap flag variable is outside the for loop and so that condition would turn out to be true only when an entire step is tested without sorting and not just the first element . Just try running my code and tell me why it works 85% of times then? – jiVatx19 Jan 02 '21 at 13:45
  • if your first element is the smallest then it will compare with all other n-1 elements in one inner iteration and there is no swap, so when it comes out of the inner loop it does not even consider that other elements are sorted or not , it just breaks the outer loop. – Suvajit Patra Jan 02 '21 at 13:55
  • Thank you. I got it. – jiVatx19 Jan 02 '21 at 14:12
1

Your code is a wrong implementation of the bubble sort.

If the first element of the array is the smaller one as the example in your question the code

if (didSwap == 0) {
    break;
}

breaks the loops at the first turn and nothing is sort.

A test similar to the one for didSwap exists in a bubble sort, but the implementation is different, it does not do if (nums[inner] < nums[outer]) but compare each element with the next.

If the code is incorrect then why does it work 85% of the times

I don't know from where these 85% comes (and is very probably false), of course all depends on the values in the array, but when nums[outer]is smaller than all the elements having a greater index your program stops, and the elements having a greater index are not sorted. In the example in your question the very first element is the smaller of the array so nothing at all is sort.


So two possibilities :

  • to remove all concerning didSwap

  • to implement a bubble sort

to have a bubble sort modify your internal loop to have for instance :

for (inner = outer + 1; inner < 8; inner++) {
    if (nums[inner] > nums[inner + 1]) {
        temp = nums[inner];
        nums[inner] = nums[inner + 1];
        nums[inner + 1] = temp;
        didSwap = 1;
    }
}

for instance to use the values you give in your question :

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int ctr, inner, outer, didSwap, temp;
#if 1
    int nums[10] = { 4, 6, 25, 9, 60, 59, 44, 65, 59, 90};
#else
    int nums[10];
    time_t t;

    srand(time(&t));

    for (ctr = 0; ctr < 10; ctr++) {
        nums[ctr] = (rand() % 99) + 1;
    }
#endif

    puts("\nHere is the list before the sort:");
    for (ctr = 0; ctr < 10; ctr++) {
        printf("%3d", nums[ctr]);
    }

    // Sorting the array
    for (outer = 0; outer < 9; outer++) {
        didSwap = 0;

        for (inner = outer + 1; inner < 8; inner++) {
            if (nums[inner] > nums[inner + 1]) {
                temp = nums[inner];
                nums[inner] = nums[inner + 1];
                nums[inner + 1] = temp;
                didSwap = 1;
            }
        }

        if (didSwap == 0) {
            break;
        }
    }

    puts("\n\nHere is the list after sorting:");
    for (ctr = 0; ctr < 10; ctr++) {
        printf("%3d", nums[ctr]);
    }

    putchar('\n');

    return 0;
}

Compilation and execution :

pi@raspberrypi:/tmp $ gcc -Wall c.c
pi@raspberrypi:/tmp $ ./a.out

Here is the list before the sort:
  4  6 25  9 60 59 44 65 59 90

Here is the list after sorting:
  4  6  9 25 44 59 59 60 65 90
pi@raspberrypi:/tmp $ 
bruno
  • 32,421
  • 7
  • 25
  • 37
  • First of all the didSwap flag variable is outside the for loop and so that condition would turn out to be true only when an entire step is tested without sorting and not just the first element . Secondly for your "is very probably false" comment just once try running my code and you would know !!!! – jiVatx19 Jan 02 '21 at 13:39
  • @jiVatx19 of course the var is defined and set to 0 out of the nested loop, visibly you do not understand what happens. I also encourage you do not be aggressive with people helping you – bruno Jan 02 '21 at 13:44
  • Also it is a selection sort algorithm not bubble sort one. This is a different question. – jiVatx19 Jan 02 '21 at 13:48
  • I had no intention of being aggressive and if I sounded so then sorry for that. Plz just run my code once. – jiVatx19 Jan 02 '21 at 13:50
  • @jiVatx19 I really start to regret to use my time for you ! your question is why your function do not sort in all cases and my answer explain why ! – bruno Jan 02 '21 at 13:50
  • you say it doesn't work at all . I just wanna prove it do works 85% of times – jiVatx19 Jan 02 '21 at 13:52
  • @jiVatx19 all the first part of my answer explain why your function does not work, you cannot understand that ? – bruno Jan 02 '21 at 13:53
  • I am new to programming and my book "Absolute beginner to C by Greg Perry" really confused me on sorting by giving a selection sort example under bubble sort lable ... – jiVatx19 Jan 02 '21 at 13:57
  • @jiVatx19 of course I did, even it was enough to read your code to understand the problem. Did you read my answer even once ? – bruno Jan 02 '21 at 13:57
  • @jiVatx19 the probability to have the right order from (pseudo) random numbers when your functions stops to sort each time `nums[outer]` is smaller than all the elements having a greater index cannot be so high like 85%. I prefer to not ask you to know from where these 85% comes. And whatever a function 'working' in 99.99999 of the cases is a non working function. A function must work in 100% of the cases => the function in your question does not work – bruno Jan 02 '21 at 14:06
  • those random situations in which first element is smallest the code doesn't work and in all other situations it works. Since, it is not compatible for every different types of unsorted array so it is incorrect. – jiVatx19 Jan 02 '21 at 14:10