Search code examples
carrayssortingfor-loopbubble-sort

C Programming: reate 10 Element Array, Bubblesort Algorithm, Return Min and Max Values of Array


So the actual problem just asks to accept a 10 element array and return the min and max values. I'm very used to dealing with arrays in Matlab/GNUoctave but today is my first time messing with them in C.

Anyway, I guess what I'd like to know is if there is a better way to input an array than with the for loop like I did.

Also, I could not figure out how to get my bubblesort if block to keep cycling until the array is sorted. I tried "while(;;)" without success, and started looking into boolean variables but didn't find what I was looking for.

Also also, if there is a better way to do this altogether, I'm here to learn. Like maybe bubblesort is stupid for this, I don't know. I suspect it is. It would probably take a long time for a longer array?

#include <stdio.h>


int main()
{
    int a[10];
    int i;
    int k;
    int temp;   


    for (i=0; i < 10; i++)
    {
        printf("Enter an integer: ");
        scanf("%d",&a[i]);
    }

    for (i=0; i < 10; i++)
    {
        if (a[i] > a[i+1])
        {
            temp = a[i];
            a[i] = a[i+1];
            a[i+1] = temp;
        }
    }
    printf("Smallest = %i\nLargest = %i\n",a[0],a[9]);
    return 0;
}

Solution

  • I see two immediate problems with your code(a).

    First, a bubble sort generally requires more than one pass to sort an entire collection. Each pass "bubbles" a single item to its correct location.

    The second problem is that, when you're comparing items n and n + 1, n better not be more than eight in a ten-element array.

    Taking those two things into account, the easiest (not necessarily the most efficient) bubble sort would be:

    for (int pass = 1; pass <= 10; ++pass) {
        for (int idx = 0; idx < 9; ++idx) {
            if (a[idx] > a[idx + 1]) {
                int temp = a[idx];
                a[idx] = a[idx + 1];
                a[idx + 1] = temp;
            }
        }
    }
    

    One that exits after the pass that finishes sorting (rather than doing ten passes no matter what) would use a flag to indicate this:

    int madeSwap = 1; // or bool madeSwap (after #include <stdbool.h>).
    while (madeSwap) {
        madeSwap = 0; // false for stdbool
        for (int idx = 0; idx < 9; ++idx) {
            if (a[idx] > a[idx + 1]) {
                int temp = a[idx];
                a[idx] = a[idx + 1];
                a[idx + 1] = temp;
                madeSwap = 1; // true for stdbool
            }
        }
    }
    

    Of course, all that only matters if you are required to sort the array. Your question title seems to indicate this but the body does not.

    So, if the only requirement is to return the minimum and maximum values, no sorting is required. You can just do something like:

    int minVal = a[0], maxVal = a[0];
    for (int idx = 1; idx < 10; ++idx) {
        if (a[idx] < minVal) minVal = a[idx];
        if (a[idx] > maxVal) maxVal = a[idx];
    }
    // minVal and maxVal now hold the minimum and maximum value respectively.
    

    (a) There is actually a third issue that will rear its ugly head if you enter something that isn't an int. If that happens, the value will not be set and the input stream will remain in the state it was in before the attempted read. Use of scanf should generally always check the return code, with something like:

    for (int i = 0; i < 10; i++) {
        printf("Enter an integer: ");
        if (scanf("%d", &a[i]) != 1) {
            puts("Invalid data in input stream, will exit.");
            return 1;
        }
    }
    

    I've kept that separate since, while it's better to have robust code, that's often not considered necessary for educational code. You would, however, be better off getting into the habit early.