CS 1400 Lab 24: Bubble Sort


Objectives:

The objective of this lab is to build your understanding of sorting arrays.

After completing this lab, you should be able to

  1. Explain how a bubble sort works.
  2. Describe the performance issues with a bubble sort.
  3. Correctly write the code to do a bubble sort.

Study Material

  1. Designing a Bubble Sort
  2. Animation of a Bubble Sort
    (click on the green button to start the animation)

Programming Exercise

You should have reviewed the material on designing a bubble sort before starting this assignment. Create a new project in Visual Sudio. Download the program bsort.cs and add it to the project.

Program Specification

The program, as written, sorts an array of integers in ascending order (the smallest number is first). For this exercise, all you need to do is to change the program so that it sorts the array in descending order (the biggest number is first). If you understand how the sort algorithm works, this should be quite easy to do.

Questions

Study the following block of code carefully. When you submit your lab, add a comment to your submission that answers the following questions about this code:

for (int j = 0; j < 3; j++)
{
     for (int i = 0; i < 3; i++)
     {
          if ( block [i] > block [i+1])
               swap (ref block [i], ref block [i + 1]);
     }
}

(1) When the code above is executed, how many times is this statement executed?
      if ( block [i] > block [i+1])

(2) A bubble sort is not very efficient. Write down one reason why it is not.

File(s) to Submit:

Place your complete project folder in a zip file and name the zip file
lab_24_your-initials_V1.0.zip. For example, I would name my file lab_24_RKD_V1.0.zip. Submit this assignment as Lab #24 on Canvas.

Grading Guidelines

Description Points possible

Assignment meets grading guidelines:
o Source code files contain a declaration that you did not copy any code, except that provided.
o Assignment has been properly submitted to Canvas
o Code meets style guidelines
o Code contains a Console.ReadLine( ) statement at the end

2

Program correctly sorts the array from largest to smallest.

3

You have correctly asnwered the questions in this lab.

2
Total 7