-2
#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;
}

Sometimes it doesn't sort the list properly and sometimes doesn't sort at all. Screenshot of error

chqrlie
  • 131,814
  • 10
  • 121
  • 189
jiVatx19
  • 21
  • 7
  • 2
    Please , edited your code and write it correctly , https://stackoverflow.com/questions/366588/what-does-a-good-programmers-code-look-like – MED LDN Dec 31 '20 at 08:28
  • 1
    That's also not bubblesort. There are no intentional adjacent walk-ups/downs in this algorithm. – WhozCraig Dec 31 '20 at 08:36

1 Answers1

1

You are not using bubble sort. this is a selection sort algorithm and your code is not correct. I have updated the code for the bubble sort algorithm.

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

int 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]);
    }

    // bubble Sorting the array
    for (outer = 0; outer < 9; outer++) {
        didSwap = 0;
        for (inner = 0; inner < 9-outer; inner++) {
            //repeatedly swapping the adjacent elements if they are in wrong order
            if (nums[inner] > nums[inner+1]) {
                temp = nums[inner];
                nums[inner] = nums[inner+1];
                nums[inner+1] = 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;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
roottraveller
  • 7,942
  • 7
  • 60
  • 65
  • Actually the book from from which I am studying (Absolute Beginner's Guide to C by Greg Perry) gave this selection sort example under the label of bubble sort and so I got confused. By the way, why the code is sorting 85% of the times if it is incorrect ? – jiVatx19 Jan 01 '21 at 17:57
  • 1
    your code works for a specific set of input and its a worg code for selection sort we sell. read selection sort here: https://www.geeksforgeeks.org/java-program-for-selection-sort/ – roottraveller Jan 02 '21 at 15:06
  • 1
    If you are a C beginner, I would recommend this blog: https://www.geeksforgeeks.org/c-programming-language/ – roottraveller Jan 02 '21 at 15:08