[Current] [News] [Glance] [Search] [Instructions] [Links] [Handouts] [Project] [Outlines] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [EIJ] [API]
Back to More Efficient Sorting Algorithms. On to Introduction to Data Structures.
Held Monday, October 23, 2000
Summary
Today we visit our final sorting algorithm (for the time being), Quicksort. Quicksort is a divide-and-conquer algorithm based on the notion of dividing the set into small values and large values.
Notes
Overview
/** * Sort an array using Quicksort. * Pre: All elements in the array can be compared to each other. * Post: The vector is sorted (using the standard meaning). */ public void quickSort(Object[] stuff) { quickSort(stuff, 0, stuff.length-1); } // quickSort(Object[]) /** * Sort part of an array using Quicksort. * Pre: All elements in the subarray can be compared to each other. * Pre: 0 <= lb <= ub < stuff.length * Post: The vector is sorted (using the standard meaning). */ public void quickSort(Object[] stuff, int lb, int ub, Comparator compare) { // Variables Object pivot; // The pivot used to split the vector int mid; // The position of the pivot // Base case: size one arrays are sorted. if (lb == ub) return; // Pick a pivot and put it at the front of the array. putPivotAtFront(stuff,lb,ub); // Determine the position of the pivot, while rearranging the array. mid = partition(stuff, lb, ub); // Recurse on nonempty subarrays. if (lb<=mid-1) quickSort(stuff, lb,mid-1); if (mid+1<=ub) quickSort(stuff, mid+1,ub); } // quickSrt /** * Split the array given by [lb .. ub] into ``smaller'' and * ``larger'' elements, where smaller and larger are defined by * their relationship to a pivot. Return the index of the pivot * between those elements. Uses the first element of the array * as the pivot. */ public int partition(Object[] stuff, int lb, int ub) { // STUB. Can you figure it out? return 0; } // partition(Object[])
Set pivot to the first element of the subarray Set left to the start of the subarray Set right to the end of the subarray Move left and right toward each other, Swap their contents when you observe that one side is "wrong" (something on the left is larger than the pivot, something on the right is larger than the pivot)
Wednesday, 23 August 2000
Thursday, 24 August 2000
Monday, 24 October 2000
Back to More Efficient Sorting Algorithms. On to Introduction to Data Structures.
[Current] [News] [Glance] [Search] [Instructions] [Links] [Handouts] [Project] [Outlines] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [EIJ] [API]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2000F/Outlines/outline.30.html
Source text last modified Wed Oct 25 10:05:37 2000.
This page generated on Fri Oct 27 08:20:02 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu