Anonymous
Anonymous asked in Science & MathematicsMathematics · 6 years ago

Construct an algorithm that will preform a "bubble" sort in ascending order, CREATE ALL components pf an algorithm: DESCRIPTION, INPUT, BODY?

Construct an algorithm that will preform a "bubble" sort in ascending order. In a bubble sort, we compare the first two elements and swap positions if the first element is larger than the second element. Move the second element and perform the comparison with the third element.Continue the procedure by moving thru the list until you reach the end of the list. Start at the top and repeat the procedure until no more swaps are required, which means that the list is now in sorted order. CREATE ALL components pf an algorithm: DESCRIPTION, INPUT, BODY, OUTPUT

1 Answer

Relevance
  • 6 years ago
    Best Answer

    I'm not sure what language you are expected to use, whether you are expecting the user to input the values and then have them output, etc. But here's a general description of the algorithm along with the input that is expected and the output that will be generated:

    DESCRIPTION:

    The routine will take an array of numbers and perform a "bubble" sort to get them into ascending order.

    INPUT:

    The routine will take an array of numbers (of a certain size) in an arbitrary order.

    BODY:

    DO

    changed = 0

    FOR I = 1 TO size -1

    IF nums(I) > nums(I + 1) THEN

    # Swap

    tmp = nums(I)

    nums(I) = nums(I + 1)

    nums(I + 1) = tmp

    changed = 1

    END IF

    NEXT

    LOOP WHILE(NOT changed)

    OUTPUT:

    The routine will return that same array of numbers sorted in ascending order.

Still have questions? Get your answers by asking now.