Close
Close Window

DSA Coursenotes

Chapter 6 Week 7

Show Source |    | About   «  6.2. Sorting: Quadratic Sorts   ::   Contents   ::   7.1. Sorting: Limits to Sorting  »

6.3. Sorting: Faster Sorts

6.3.1. Faster Sorts

6.3.1.1. Shellsort

6.3.1.2. Shellsort (2)

static void shellsort(int[] A) {
  for (int i=A.length/2; i>2; i/=2) { // For each increment
    for (int j=0; j<i; j++) {         // Sort each sublist
      inssort2(A, j, i);
    }
  }
  inssort2(A, 0, 1);     // Could call regular inssort here
}

/** Modified Insertion Sort for varying increments */
static void inssort2(int[] A, int start, int incr) {
  for (int i=start+incr; i<A.length; i+=incr)
    for (int j=i; (j>=incr) && (A[j] < A[j-incr]); j-=incr)
      Swap.swap(A, j, j-incr);
}

6.3.1.3. Mergesort

6.3.1.4. .

.

6.3.1.5. Mergesort cost

  • Mergesort cost:

  • Mergesort is also good for sorting linked lists.

  • Mergesort requires twice the space.

6.3.1.6. Quicksort

static void quicksort(Comparable[] A, int i, int j) { // Quicksort
  int pivotindex = findpivot(A, i, j);  // Pick a pivot
  Swap.swap(A, pivotindex, j);               // Stick pivot at end
  // k will be the first position in the right subarray
  int k = partition(A, i, j-1, A[j]);
  Swap.swap(A, k, j);                        // Put pivot in place
  if ((k-i) > 1) { quicksort(A, i, k-1); }  // Sort left partition
  if ((j-k) > 1) { quicksort(A, k+1, j); }  // Sort right partition
}
static int findpivot(Comparable[] A, int i, int j)
  { return (i+j)/2; }

6.3.1.7. Quicksort Partition

1 / 20 Settings
<<<>>>

When we start the partition function, pivot value 60 has been moved to the right most position.

  1. 760
  2. 61
  3. 572
  4. 883
  5. 854
  6. 425
  7. 836
  8. 737
  9. 488
  10. 609
Proficient Saving... Error Saving
Server Error
Resubmit

6.3.1.8. Quicksort Partition Cost

1 / 10 Settings
<<<>>>

To analyze Quicksort, we first analyze the findpivot and partition functions when operating on a subarray of length $k$

Proficient Saving... Error Saving
Server Error
Resubmit

6.3.1.9. Quicksort Summary

6.3.1.10. Quicksort Worst Case

1 / 8 Settings
<<<>>>

Quicksort's worst case will occur when the pivot does a poor job of breaking the array, that is, when there are no records in one partition, and $n-1$ records in the other

Created with Raphaël 2.1.2
Proficient Saving... Error Saving
Server Error
Resubmit

6.3.1.11. .

.

6.3.1.12. Quicksort Best Case

1 / 7 Settings
<<<>>>

Quicksort's best case occurs when the selected pivot always breaks the array into two equal halves

Created with Raphaël 2.1.2
Proficient Saving... Error Saving
Server Error
Resubmit

6.3.1.13. .

.

6.3.1.14. Quicksort Average Case

1 / 12 Settings
<<<>>>

QuickSort is a recursive function, accordingly we should end up with a recursive relation to describe its average case running time

Created with Raphaël 2.1.2
Proficient Saving... Error Saving
Server Error
Resubmit

6.3.1.15. Optimizations for Quicksort

  • Better Pivot

  • Inline instead of function calls

  • Eliminate recursion

  • Better algorithm for small sublists: Insertion sort
    • Best: Don’t sort small lists at all, do a final Insertion Sort to clean up.

6.3.1.16. Heapsort

1 / 51 Settings
<<<>>>

Initially, we start with our unsorted array.

  1. 730
  2. 61
  3. 572
  4. 883
  5. 604
  6. 425
  7. 836
  8. 727
  9. 488
  10. 859
Created with Raphaël 2.1.2
73
6
57
88
60
72
48
85
42
83
Proficient Saving... Error Saving
Server Error
Resubmit

6.3.1.17. Heapsort Analysis

1 / 12 Settings
<<<>>>

The first step in heapsort is to heapify the array. This will cost $\theta(n)$ running time for an array of size $n$.
Consider the following structure of a Max Heap

Created with Raphaël 2.1.2
Proficient Saving... Error Saving
Server Error
Resubmit

   «  6.2. Sorting: Quadratic Sorts   ::   Contents   ::   7.1. Sorting: Limits to Sorting  »

nsf
Close Window