The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998

18.04.2013 Views

Approaches to Sorting Next: Data Structures Up: Data Structures and Sorting Previous: Applications of Sorting Approaches to Sorting Sorting is a natural laboratory for studying basic algorithm design paradigms, since many useful techniques lead to interesting sorting algorithms. Indeed, we introduce several techniques here that will be described further in subsequent chapters. Consider the algorithms below as case studies for each of the relevant techniques. ● Data Structures ● Incremental Insertion ● Divide and Conquer ● Randomization ● Bucketing Techniques Algorithms Mon Jun 2 23:33:50 EDT 1997 file:///E|/BOOK/BOOK/NODE31.HTM [19/1/2003 1:28:28]

Data Structures Next: Incremental Insertion Up: Approaches to Sorting Previous: Approaches to Sorting Data Structures Perhaps the most dramatic algorithmic improvement made possible by using appropriate data structures occurs in sorting. Selection sort is a simple-to-code algorithm that repeatedly extracts the smallest remaining element from the unsorted part of the set: SelectionSort(A) For i = 1 to n do Return(Sort) Sort[i] = Find-Minimum from A Delete-Minimum from A Selection sort is typically implemented by partitioning the input array into sorted and unsorted regions. To find the smallest item, we perform a linear sweep through the unsorted portion of the array. The smallest item is then swapped with the ith item in the array before moving on the next iteration. Selection sort performs n iterations, where the average iteration takes n/2 steps, for a total of time. But what if we improve the data structure? It takes O(1) time to remove a particular item from an unsorted array once it has been located, but O(n) time to find the smallest item. These two are exactly the operations supported by priority queues. So what happens if we replace the data structure with a better priority queue implementation, either a heap or a balanced binary tree. Operations within the loop now take time each, instead of O(n). By using such a priority queue implementation, selection sort is sped up to from . The name typically given to this algorithm, heapsort, obscures the relationship between them, but heapsort is nothing but an implementation of selection sort with the right data structure. file:///E|/BOOK/BOOK/NODE32.HTM (1 of 2) [19/1/2003 1:28:29]

Approaches to Sorting<br />

Next: Data Structures Up: Data Structures and Sorting Previous: Applications of Sorting<br />

Approaches to Sorting<br />

Sorting is a natural laboratory for studying basic algorithm design paradigms, since many useful<br />

techniques lead to interesting sorting algorithms. Indeed, we introduce several techniques here that will<br />

be described further in subsequent chapters. Consider the algorithms below as case studies for each of the<br />

relevant techniques.<br />

● Data Structures<br />

● Incremental Insertion<br />

● Divide and Conquer<br />

● Randomization<br />

● Bucketing Techniques<br />

<strong>Algorithm</strong>s<br />

Mon Jun 2 23:33:50 EDT 1997<br />

file:///E|/BOOK/BOOK/NODE31.HTM [19/1/2003 1:28:28]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!