CSC300: Homework (Order of Growth, Sorting Problems)

Contents [0/3]

Homework (Order of Growth, Sorting Problems) [1/3]
HW Answers [2/3]
Quiz Answers [3/3]

(Click here for one slide per page)


Homework (Order of Growth, Sorting Problems) [1/3]

HW Answers [2/3]

 2.1.1
  Show, in the style of the example trace with Algorithm 2.1,
  how selection sort sorts the array
  E A S Y Q U E S T I O N
  ^ * 
  A E S Y Q U E S T I O N
    ^
  A E S Y Q U E S T I O N
      ^       *   
  A E E Y Q U S S T I O N
        ^           *
  A E E I Q U S S T Y O N
          ^             *
  A E E I N U S S T Y O Q
            ^         *
  A E E I N O S S T Y U Q
              ^         *
  A E E I N O Q S T Y U S
                ^      
  A E E I N O Q S T Y U S
                  ^     *
  A E E I N O Q S S Y U T
                    ^   *
  A E E I N O Q S S T U Y
                      ^
  A E E I N O Q S S T U Y
                        ^
  A E E I N O Q S S T U Y
                          ^

2.1.2*
  What is the maximum number of exchanges involving any particular item
  during selection sort? What is the average number of exchanges
  involving an item?

  
  max # of exchanges for selection sort for a particular element: N
  An example:
  501234
  051234
  015234
  012534
  012354
  012345

  total # of exchanges <= N
  total # of elements = N
  average <= 1

2.1.4
  Show, in the style of the example trace with Algorithm 2.1,
  how insertion sort sorts the array
  E A S Y Q U E S T I O N
  ^
  E A S Y Q U E S T I O N
  * ^
  A E S Y Q U E S T I O N
      ^
  A E S Y Q U E S T I O N
        ^
  A E S Y Q U E S T I O N
        * ^
  A E S Q Y U E S T I O N
      *   ^
  A E Q S Y U E S T I O N
          * ^
  A E Q S U Y E S T I O N
            * ^
  A E Q S U E Y S T I O N
          *   ^
  A E Q S E U Y S T I O N
        *     ^
  A E Q E S U Y S T I O N
      *       ^
  A E E Q S U Y S T I O N
              * ^
  A E E Q S U S Y T I O N
            *   ^
  A E E Q S S U Y T I O N
                * ^
  A E E Q S S U T Y I O N
              *   ^
  A E E Q S S T U Y I O N
                  * ^
  A E E Q S S T U I Y O N
                *   ^
  A E E Q S S T I U Y O N
              *     ^
  A E E Q S S I T U Y O N
            *       ^
  A E E Q S I S T U Y O N
          *         ^
  A E E Q I S S T U Y O N
        *           ^
  A E E I Q S S T U Y O N
                    * ^
  A E E I Q S S T U O Y N
                  *   ^
  A E E I Q S S T O U Y N
                *     ^
  A E E I Q S S O T U Y N
              *       ^
  A E E I Q S O S T U Y N
            *         ^
  A E E I Q O S S T U Y N
          *           ^
  A E E I O Q S S T U Y N
                      * ^
  A E E I O Q S S T U N Y
                    *   ^
  A E E I O Q S S T N U Y
                  *     ^
  A E E I O Q S S N T U Y
                *       ^
  A E E I O Q S N S T U Y
              *         ^
  A E E I O Q N S S T U Y
            *           ^
  A E E I O N Q S S T U Y
          *             ^
  A E E I N O Q S S T U Y
                          ^          
2.1.6*
  Which method runs faster for an array with all keys identical,
  selection sort or insertion sort?

  faster with identical keys:
  insertion=N compares and 0 swaps
  selection=N^2/2 compares and 0 swaps

  Three important facts for this problem:
  1. if all keys are identical then it's sorted
  2. for a sorted array, insert sort is linear (it just checks every
  element to the adjacent one)
  3. for any array, selection sort does a quadratic number of comparisons.

  Since the comparison pattern of selection sort is fixed, it still has
  worst case performance.



2.1.7*
  Which method runs faster for an array in reverse order,
  selection sort or insertion sort?

  faster for reversed array:
  insertion=N^2/2 compares and N^2/2 swaps
  selection=N^2/2 compares and N swaps

  Experimentally
  insertion with reversed array:
    4000   15999998   4.0 [0.049 0.721]
    8000   63999998   4.0 [0.173 3.531]
   16000  255999998   4.0 [0.761 4.399]
   32000 1023999998   4.0 [2.911 3.825]
   64000 4095999998   4.0 [12.374 4.251]
    
  selection with reversed array:
    4000    8005999   4.0 [0.026 0.226]
    8000   32011999   4.0 [0.115 4.423]
   16000  128023999   4.0 [0.489 4.252]
   32000  512047999   4.0 [1.964 4.016]
   64000 2048095999   4.0 [8.282 4.217]  

2.1.8*
  Suppose that we use insertion sort on a randomly ordered array where items 
  have only one of three values. Is the running time linear, quadratic, or 
  something in between? 

  should be quadratic.  Number of values does not matter, it's their distribution.

  three values:
    4000    5203612   3.8 [0.016 0.364]
    8000   21434206   4.1 [0.053 3.313]
   16000   84502719   3.9 [0.210 3.962]
   32000  342564934   4.1 [0.886 4.219]
   64000 1360115026   4.0 [3.488 3.937]
  128000 5419985602   4.0 [12.636 3.623]
  
  two values:
    4000    4039762   4.1 [0.012 0.279]
    8000   16011924   4.0 [0.041 3.417]
   16000   64837772   4.0 [0.155 3.780]
   32000  254433286   3.9 [0.599 3.865]
   64000 1022514350   4.0 [2.537 4.235]
  128000 4097713293   4.0 [9.868 3.890]

  one value (therefore presorted):
    4000       7998   2.0 [0.002 Infinity]
    8000      15998   2.0 [0.001 0.500]
   16000      31998   2.0 [0.001 1.000]
   32000      63998   2.0 [0.000 0.000]
   64000     127998   2.0 [0.001 Infinity]
  128000     255998   2.0 [0.000 0.000]  
    
2.1.15*
  Expensive exchange. A clerk at a shipping company is charged with the task
  of rearranging a number of large crates in order of the time they are to be
  shipped out. Thus, the cost of compares is very low (just look at the labels)
  relative to the cost of ex- changes (move the crates). The warehouse is
  nearly full --- there is extra space sufficient to hold any one of the crates,
  but not two. What sorting method should the clerk use?

  selection sort # exchanges <= N.

Quiz Answers [3/3]

Selection sort
        
4 1 9 5 6 0 3 8 7 2
^         *
0 1 9 5 6 4 3 8 7 2
  ^                    
0 1 9 5 6 4 3 8 7 2
    ^             *
0 1 2 5 6 4 3 8 7 9  
      ^     *
0 1 2 3 6 4 5 8 7 9  
        ^ *
0 1 2 3 4 6 5 8 7 9  
          ^...

          
1 2 3 4 5 6 7 8 9 0
^                 *
0 2 3 4 5 6 7 8 9 1
  ^               *
0 1 3 4 5 6 7 8 9 2
    ^             *
0 1 2 4 5 6 7 8 9 3
      ^           *
0 1 2 3 5 6 7 8 9 4
        ^...

        
Insertion sort
        
4 1 9 5 6 0 3 8 7 2
^
4 1 9 5 6 0 3 8 7 2
* ^
1 4 9 5 6 0 3 8 7 2
    ^
1 4 9 5 6 0 3 8 7 2  
    * ^
1 4 5 9 6 0 3 8 7 2 
      * ^
1 4 5 6 9 0 3 8 7 2  
        * ^
1 4 5 6 0 9 3 8 7 2  
      *   ^
1 4 5 0 6 9 3 8 7 2  
    *     ^
1 4 0 5 6 9 3 8 7 2  
  *       ^
1 0 4 5 6 9 3 8 7 2  
*         ^
0 1 4 5 6 9 3 8 7 2  
          ^...

          
1 2 3 4 5 6 7 8 9 0
^
1 2 3 4 5 6 7 8 9 0
  ^
1 2 3 4 5 6 7 8 9 0
    ^
1 2 3 4 5 6 7 8 9 0
      ^
1 2 3 4 5 6 7 8 9 0
        ^
1 2 3 4 5 6 7 8 9 0
          ^
1 2 3 4 5 6 7 8 9 0
            ^
1 2 3 4 5 6 7 8 9 0
              ^
1 2 3 4 5 6 7 8 9 0
                ^
1 2 3 4 5 6 7 8 9 0
                * ^
1 2 3 4 5 6 7 8 0 9
              *   ^
1 2 3 4 5 6 7 0 8 9
            *     ^
1 2 3 4 5 6 0 7 8 9
          *       ^
1 2 3 4 5 0 6 7 8 9
        *         ^
...