Code Listings Chapter 11

Listing 11-1
Listing 11-2
Listing 11-3
Listing 11-4
Listing 11-5

Listing 11-1 An implementation of the selection sort

/** Finds the largest item in an array.
@pre The size of the array is >= 1.
@post The arguments are unchanged.
@param theArray The given array.
@param size The number of elements in theArray.
@return The index of the largest entry in the array. */
int findIndexofLargest (const ItemType theArray[], int size);

/** Sorts the items in an array into ascending order.
@pre None.
@post The array is sorted into ascending order; the size of the array
is unchanged.
@param theArray The array to sort.
@param n The size of theArray. */
void selectionSort (ItemType theArray[], int n) { // last = index of the last item in the subarray of items yet // to be sorted; // largest = index of the largest item found for (int last = n - 1; last >= 1; last) { // At this point, theArray[last+1..n-1] is sorted, and its // entries are greater than those in theArray[0..last]. // Select the largest entry in theArray[0..last] int largest = findIndexofLargest (theArray, last + 1); // Swap the largest entry, theArray[largest], with // theArray[last] std::swap (theArray[largest], theArray[last]); } // end for } // end selectionSort int findIndexofLargest (const ItemType theArray[], int size) { int indexSoFar = 0; // Index of largest entry found so far for (int currentIndex = 1; currentIndex < size; currentIndex++) { // At this point, theArray[indexSoFar] >= all entries in // theArray[0..currentIndex - 1] if (theArray[currentIndex] > theArray[indexSoFar]) indexSoFar = currentIndex; } // end for return indexSoFar; // Index of largest entry } // end findIndexofLargest


 

 

Listing 11-2 An implementation of the bubble sort

/** Sorts the items in an array into ascending order.
@pre None.
@post theArray is sorted into ascending order; n is unchanged.
@param theArray The given array.
@param n The size of theArray. */

void
bubbleSort (ItemType theArray[], int n) { bool sorted = false; // False when swaps occur int pass = 1; while (!sorted && (pass < n)) { // At this point, theArray[n+1-pass..n-1] is sorted // and all of its entries are > the entries in theArray[0..n-pass] sorted = true; // Assume sorted for (int index = 0; index < n - pass; index++) { // At this point, all entries in theArray[0..index-1] // are <= theArray[index] int nextIndex = index + 1; if (theArray[index] > theArray[nextIndex]) { // Exchange entries std::swap (theArray[index], theArray[nextIndex]); sorted = false; // Signal exchange } // end if } // end for // Assertion: theArray[0..n-pass-1] < theArray[n-pass] pass++; } // end while } // end bubbleSort


 

Listing 11-3  An implementation of the insertion sort

/** Sorts the items in an array into ascending order.
@pre None.
@post theArray is sorted into ascending order; n is unchanged.
@param theArray The given array.
@param n The size of theArray. */

void i
nsertionSort (ItemType theArray[], int n) { // unsorted = first index of the unsorted region, // loc = index of insertion in the sorted region, // nextItem = next item in the unsorted region. // Initially, sorted region is theArray[0], // unsorted region is theArray[1..n-1]. // In general, sorted region is theArray[0..unsorted-1], // unsorted region theArray[unsorted..n-1] for (int unsorted = 1; unsorted < n; unsorted++) { // At this point, theArray[0..unsorted-1] is sorted. // Find the right position (loc) in theArray[0..unsorted] // for theArray[unsorted], which is the first entry in the // unsorted region; shift, if necessary, to make room ItemType nextItem = theArray[unsorted]; int loc = unsorted; while ((loc > 0) && (theArray[loc - 1] > nextItem)) { // Shift theArray[loc - 1] to the right theArray[loc] = theArray[loc - 1]; } // end for // At this point, theArray[loc] is where nextItem belongs theArray[loc] = nextItem; // Insert nextItem into sorted region loc--; } // end while } // end insertionSort


Listing 11-4 An implementation of the merge sort

/** Merges two sorted array segments theArray[first..mid] and
theArray[mid+1..last] into one sorted array.
@pre first <= mid <= last. The subarrays theArray[first..mid] and
theArray[mid+1..last] are each sorted in increasing order.
@post theArray[first..last] is sorted.
@param theArray The given array.
@param first The index of the beginning of the first segment in
theArray.
@param mid The index of the end of the first segment in theArray;
mid + 1 marks the beginning of the second segment.
@param last The index of the last element in the second segment in
theArray.
@note This function merges the two subarrays into a temporary
array and copies the result into the original array theArray. */

void
merge (ItemType theArray[], int first, int mid, int last) { ItemType tempArray[MAX_SIZE]; // Temporary array // Initialize the local indices to indicate the subarrays int first1 = first; // Beginning of first subarray int last1 = mid; // End of first subarray int first2 = mid + 1; // Beginning of second subarray int last2 = last; // End of second subarray // While both subarrays are not empty, copy the // smaller item into the temporary array int index = first1; // Next available location in tempArray while ((first1 <= last1) && (first2 <= last2)) { // At this point, tempArray[first..index-1] is in order if (theArray[first1] <= theArray[first2]) { tempArray[index] = theArray[first1]; first1++; } else { tempArray[index] = theArray[first2]; first2++; } // end if index++; } // end while // Finish off the first subarray, if necessary while (first1 <= last1) { // At this point, tempArray[first..index-1] is in order tempArray[index] = theArray[first1]; first1++; index++; } // end while // Finish off the second subarray, if necessary while (first2 <= last2) { // At this point, tempArray[first..index-1] is in order tempArray[index] = theArray[first2]; first2++; index++; } // end for // Copy the result back into the original array for (index = first; index <= last; index++) theArray[index] = tempArray[index]; } // end merge

 

Listing 11-5 A function that performs a quick sort

/** Sorts an array into ascending order. Uses the quick sort with
median-of-three pivot selection for arrays of at least MIN_SIZE
entries, and uses the insertion sort for other arrays.
@pre theArray[first..last] is an array.
@post theArray[first..last] is sorted.
@param theArray The given array.
@param first The first element to consider in theArray.
@param last The last element to consider in theArray. */

void quickSort (ItemType theArray[], int first, int last)
{
  if (last - first + 1 < MIN_SIZE)
    {
      insertionSort (theArray, first, last);
    }
  else
    {
        // Create the partition: S1 | Pivot | S2
      int pivotIndex = partition (theArray, first, last);
        // Sort subarrays S1 and S2
      quickSort (theArray, first, pivotIndex - 1);
      quickSort (theArray, pivotIndex + 1, last);
    }	// end if
}   // end quickSort