121: Computer Science 1 --- Lecture February 21

Topics:

Computing the Mode

Sorting Arrays

Selection Sort

Insertion Sort

 

Computing the Mode

The mode of list of numbers is the number that appears most frequently in the list. To compute the mode we would first create a counter for each possible list element. This is naturally accomplished with an array of counters if the range of elements is relatively small. For example if we are computing the mode of a list of grades in the range 0 to 100, we could declare the following set of counters

 

int Counter[101];

 

where we would use Counter[I] to tabulate the number of list items equal to I.

 

Now using a for-loop to scan the original list, we determine the counter values as follows…

 

For ( int I=0; I< ListSize ; ++I)

     ++Counter[ List[I] ];

 

Now the mode is simply the index of the largest element in the Counter array.

 

Mode =  IndexofMax(Counter, 101);

 

Here is the full function definition.

 

Int ComputeMode( int List[], int Size)

{

int Counter[101];

 

for ( int I=0; I< ListSize ; ++I)

     ++Counter[ List[I] ];

 

return  IndexofMax(Counter, 101);

}

 

Sorting Methods

 

Selection Sort

In your lab this week (Lab 5) you worked on a sorting method called Selection Sort which works as follows.

1)      Find the max  element in the list and swap it into the last array position.

2)      Recursively sort the smaller list, ignoring the last position, and applying the same procedure given in step 1

 

See the lab assignment for more details.

Insertion Sort

In this class we will consider a different sorting method, called Insertion Sort, which outperforms Selection Sort on lists that are nearly sorted.

Let L be a list of size N. For convenience, let L[0:i] denote the sublist of L indexed from 0 to i. Insertion Sort sorts L by successively inserting the list element L[i] into its proper position in the sorted list L[0,i-1], i = 1, 2, ..., N-1. It works similarly to how a card player might insert a newly dealt card into the previously dealt hand that was already put in order. One starts at one end of the hand and stops at a place where the new card can be inserted and still maintain an ordered hand. This scan can start at either the low end of the hand (forward scan) or the high end (backward scan). The card player has no reason (other than a personal quirk) to prefer one hand over the other. However, when implementing Insertion Sort using an array it is more efficient to use the backward scan.

The sublist consisting only of the element L[0] is a priori sorted. Suppose we have a list L where the sublist L[0:i-1] is already sorted. We can obtain a sorted sublist L[0,i] by inserted the element L[i] in its proper position as follows. Using a backward scan we successively swap adjacent elements that are out of order, i.e., starting with j = i and performing a backward scan, we swap L[j] and L[j-1] as long as L[j] < L[j-1].

The following sample program utilizes the function InsertionSort().

void InsertionSort(int List[],int Size)

{

   int Position;

   int PassValue;

   for (int Pass = 1; Pass < Size; ++Pass)

   {

       Position = Pass - 1;

       PassValue = List[Pass];

       while(Position >= 0 && PassValue  < List[Position])

       //Find position to insert current value, bumping values up.//

       {

         Swap ( List[Position+1], List[Position] );  //Bump value in Position to Position + 1

         --Position;

       }//END WHILE

    

   }// END FOR

}// END InsertionSort

Note that a slightly more efficient way to perform this insertion is as follows. We still perform a backward scan of the list, but instead of a sequence of swaps to insert an element, we store the value of L[i] in a temporary variable, bump elements to the right until the correct position to insert is found, and then insert the temporary variable in this position.

 

Here is a ful program that illustrates this sorting method.

Insertion Sort