Skip to main content
ICT
Lesson A17 - Quadratic Sorting Algorithms
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

E. Counting Steps - Quadratic Algorithms page 7 of 11

  1. These three sorting algorithms are categorized as quadratic sorts because the number of steps increases as a quadratic function of the size of the list.

  2. It will be very helpful to study algorithms based on the number of steps they require to solve a problem. We will add code to the sorting template program and count the number of steps for each algorithm.

  3. This will require the use of an instance variable - we'll call it steps. The steps variable will be maintained within the sorting class and be accessed through appropriate accessor and modifier methods. You will need to initialize steps to 0 at the appropriate spot in the main menu method.

  4. For our purposes, we will only count comparisons of items in the list, and gets or sets within the list. These operations are typically the most expensive (time-wise) operations in a sort.

  5. As you type in the sorting algorithms, add increment statements for the instance variable steps. For example, here is a revised version of the bubbleSort method:

    public void bubbleSort(ArrayList <Comparable> list){
      steps = 0;
      for (int outer = 0; outer < list.size() - 1; outer++){
        for (int inner = 0; inner < list.size()-outer-1; inner++){
            steps += 3;//count one compare and 2 gets
            if (list.get(inner).compareTo(list.get(inner + 1)) > 0){
               steps += 4;//count 2 gets and 2 sets
               Comparable temp = list.get(inner);
               list.set(inner,list.get(inner + 1));
               list.set(inner + 1,temp);
            }
        }
      }
    }

  6. It is helpful to remember that a for statement is simply a compressed while statement. Each for loop has three parts: initialization, boundary check, and incrementation.

  7. As you count the number of steps, an interesting result will show up in your data. As the size of the data set doubles, the number of steps executed increases by approximately four times, a “quadratic” rate.

  8. Bubble Sort is an example of a quadratic algorithm in which the number of steps required increases at a quadratic rate as the size of the data set increases.

  9. A quadratic equation in algebra is one with a squared term, like x2. In our sorting example, as the size of the array increases to N, the number of steps required for any of the quadratic sorts increases as a function of N2.

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.