Sorting considerthe problem ofsorting a list, x 1 , x 2 ,..., x n arrange the elem entsso thatthey (orsom e key fieldsin them )are in ascending order x 1 <= x 2 ,<= ... <= x n orin descending order x 1 >= x 2 >=...>= x n Som e O (n 2 )sorting schem es easy to understand and to im plem ent notvery efficient, especially forlarge data sets Three categories: selection sorts , exchange sorts , and insertion sorts .
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Sorting
consider the problem of sorting a list, x1 , x2 ,..., xn
arrange the elements so that they (or some key fields in them) are inascending order x1 <= x2 ,<= ... <= xn or indescending order x1 >= x2 >=...>= xn
Some O(n2) sorting schemeseasy to understand and to implementnot very efficient, especially for large data sets
Three categories:selection sorts,exchange sorts, andinsertion sorts.
Selection Sort
basic idea:make a number of passes through the list or a part of the list and,on each pass, select one element to be correctly positioned.
For example, on each pass through a sublist, the smallest element in thissublist might be found and then moved to its proper location.
Given the following list is to be sorted into ascending order:67, 33, 21, 84, 49, 50, 75
Scan the list to locate the smallest element and find it in position 3Interchange this element with the first element
properly positioning smallest element at the beginning of the list21 , 33 , 67 , 84 , 49 , 50 , 75
Now in all subsequent scans, the first element need not be looked at!!
Selection SortContinue the sort by scanning the sublist of elements from position 2 on tofind the smallest element
Exchange it with the second element (itself in this case) properly positioning the next-to-smallest element in position 2
In all subsequent scans, the first two elements need not examined!
Continue in this manner,locating the smallest element in the sublist of elements from position i
on and interchanging it with the ith element,
until sublist consists only of the last two elements, which results in anexchange or not and thus completes the sort.
The first pass through the list is now complete. guaranteed that on this pass, the largest element in the list will “sink” to
the end of the list, since it will obviously be moved past all smaller elements.
Notice also that some of the smaller items have “bubbled up” toward theirproper positions nearer the front of the list.
Scan the list again, leaving out the last item ( already in its proper position).
Bubble Sort Algorithm
1. Initialize numPairs to n - 1./* numPairs is the number of pairs to be compared on the current pass */
2. Do the following:a. Set last equal to 1.
/* last marks the location of the last element involved in an interchange */
b. For i = 1 to numPairs:If x i > xi+1 :
i. Interchange x i and x i+1.ii. Set last equal to i.
c. Set numPairs equal to last - 1.while numPairs > 0.
Bubble Sort Complexity
Worst case for bubble sort occurs when the list elements are in reverse order only one item (the largest) is positioned correctly on each pass
On the first pass through the list, n - 1 comparisons and interchangesare made, and only the largest element is correctly positioned.
On the next pass, the sublist consisting of the first n - 1 elements is scanned;there are n – 2 comparisons and interchanges; and the next largest elementsinks to position n - 1.
Continue until the sublist consisting of the first two elements is scanned
Total of (n - 1) + (n - 2) + … + 1 = n(n - 1) / 2 comparisons and interchangesworst-case computing time for bubble sort is O(n 2 ).
Insertion Sort
Insertion sorts are based on the process ofrepeatedly inserting a new element into already sorted list
At the ith stage, xi is inserted into its proper place among the alreadysorted x1, x2 ,..., xi-1.
Compare xi with each of these elements, starting from the right end, andshift them to the right as necessary.
Use array position 0 to store a copy of xi to prevent “falling off the leftend” in these right-to-left scans.
Insertion Sort Algorithm
For i = 2 to n do:/* Insert x[i] into its proper position among x[1], . . . , x[i - 1] */
a. Set nextElement equal to x[i].b. Set x[0] equal to nextElement.c. Set j equal to i.d. While nextElement < x[j - 1] do:
// Shift element to the right to open a spoti. Set x[j] equal to x[j - 1].ii. Decrement j by 1.
// Now drop nextElement into the open spote. Set x[j ] equal to nextElement.
Shifting elements is really grossly inefficient.
Insertion Sort, however, works well with linked list implementations.It is essentially the same algorithm as constructing an ordered list
Insertion Sort Example
Given, 67, 33, 21, 84, 49, 50, 75.
Only the sorted sublist produced at each stage is shown
HeapsA heap is a binary tree with the following properties:
1. left-complete: each level of the tree is completely filled, exceptpossibly the bottom level where the nodes are in the leftmost positions.
2. heap-ordered: data item stored in each node is greater than orequal to the data items stored in its children.
Not a heap Heap
22
12
14
24
28
14
12 22
24
28
HeapsTo implement a heap, an array or a vector can be used most effectively.
Simply number the nodes in the heap from top to bottom,number the nodes on each level from left to right andstore the data in the ith node in the ith location of the array.
The completeness property of a heap guarantees that these data items willbe stored in consecutive locations at the beginning of the array.
If heap is the name of the array or vector used, the items in previous heapstored as follows:heap[1] = 24, heap[2] = 14, heap[3] = 28, heap[4] = 12, heap[5] = 22.
in an array implementation, easy to find the children of a given node:children of the ith node are at locations 2*i and 2*i + 1.
Similarly, the parent of the ith node is easily seen to be in location i / 2.
Convert Complete Binary Tree to a Heap
Given a complete binary tree stored in positions r through n of the arrayheap with left and right subtrees that are heaps.
Percolate-Down the largest value
For c = 2 * r to n do: // c is location of left child// Find the largest childa. If c < n and heap[c] < heap[c + 1]
Increment c by 1./* Swap node & largest child if needed, move down to the next subtree */
b. If heap[r] < heap[c]:i. Swap heap[r] and heap[c].ii. Set r = c.iii. Set c = 2 * c.
ElseTerminate repetition.
Apply this percolate-down procedure to the bottom half of the tree
Convert Complete Binary Tree to a Heaptemplate <typename ElementType>void PercolateDown(ElementType x[], int n, int r){
int c = 2*r;bool done = false;while (c < n && !done){
template <typename ElementType>void Heapify(ElementType x[], int n){
for (int r = n/2; r > 0; r--)PercolateDown(x, n, r);
return;}
After application of Heapify(), x is a heap.
Heapsort1. Consider array x as a complete binary tree and
use the Heapify algorithm to convert this tree to a heap.2. For i = n down to 2:
a. Interchange x[1] and x[i],thus putting the largest element in the sublist x[1],...,x[i] at end of sublist.
b. Apply the PercolateDown algorithm to convert the binary treecorresponding to the sublist stored in positions 1 through i - 1 of x.
In PercolateDown, the number of items in the subtree considered at eachstage is one-half the number of items in the subtree at the preceding stage.Thus, the worst-case computing time is O(log 2 n).
Heapify algorithm executes PercolateDown n/2 times: worst-casecomputing time is O(nlog 2 n).
Heapsort executes Heapify one time and PercolateDown n - 1 times;consequently, its worst-case computing time is O(n log 2 n).
Heapsort
template <typename ElementType>void HeapSort(ElementType x[], int n){
QuicksortA more efficient exchange sorting scheme than bubble sort because a typicalexchange involves elements that are far apart fewer interchanges are required to correctly position an element.
Quicksort uses a divide-and-conquer strategya recursive approach to problem-solving in which
the original problem partitioned into simpler sub-problems,each subproblem considered independently.Subdivision continues until subproblems obtained are simple
enough to be solved directly
Choose some element called a pivotPerform a sequence of exchanges so that
all elements that are less than this pivot are to its left andall elements that are greater than the pivot are to its right.
divides the (sub)list into two smaller sublists,each of which may then be sorted independently in the same way.
Quicksort1. If the list has 0 or 1 elements,
return. // the list is sorted
Else do:2. Pick an element in the list to use as the pivot.
3. Split the remaining elements into two disjoint groups:SmallerThanPivot = {all elements < pivot}LargerThanPivot = {all elements > pivot}
4. Return the list rearranged as:Quicksort(SmallerThanPivot), pivot, Quicksort(LargerThanPivot).
Quicksort Example
Given 75, 70, 65, 84, 98, 78, 100, 93, 55, 61, 81, 68 to sort
Select, arbitrarily, the first element, 75, as pivot.Search from right for elements <= 75, stop at first element
>75Search from left for elements > 75, stop at first element <=75Swap these two elements, and then repeat two elements same
The previous SPLIT operation placed pivot 75 so that all elementsto the left were <= 75 and all elements to the right were >75.
75 is now placed appropriately Need to sort sublists on either side of 75
55, 70, 65, 68, 61, 75, 100, 93, 78, 98, 81, 84 pivot 75 Need to sort (independently):
55, 70, 65, 68, 61100, 93, 78, 98, 81, 84
Quicksort performance:O(nlogn) if the pivot results in sublists of approximately the
same size.O(n2) worst-case (list already ordered, elements in reverse)
when Split() repetitively results, for example, in one empty sublist
Quicksorttemplate <typename ElementType>void Split(ElementType x[],int first, int last, int& pos)(
ElementType pivot = x[left]; // pivot elementint left = first, // index for left search
right = last; // index for right searchwhile (left < right){
// Search from right for element <= pivotwhile (x[right] > pivot)
right--;// Search from left for element > pivotwhile (left < right && x[left] <= pivot)
left++;// Interchange elements if searches haven’t metif (left < right)
Swap(x[left], x[right]);}// End of searches; place pivot in correct positionpos = right;x[first] = x[pos];x[pos] = pivot;
}
Quicksort
template <typename ElementType>void Quicksort(ElementType x[], int first, int last){
int pos; // final position of pivotif (first < last) // list has more than one element{
// Split into two sublistsSplit(x, first, last, pos);// Sort left sublistQuicksort(x, first, pos - 1);// Sort right sublistQuicksort(x, pos + 1, last);
}// else list has 0 or 1 element and// requires no sortingreturn;
}
This function is called with a statement of the formQuicksort(x, 1, n);
Quicksort Improvement I
Quicksort is a recursive function
stack of activation records must be maintained by system to managerecursion.
The deeper the recursion is, the larger this stack will become.
The depth of the recursion and the corresponding overhead can be reducedsort the smaller sublist at each stage first
Another improvement aimed at reducing the overhead of recursion is to usean iterative version of Quicksort()
To do so, use a stack to store the first and last positions of the sublists sorted"recursively".
Quicksort Improvement II
An arbitrary pivot gives a poor partition fornearly sorted lists (or lists in reverse)
virtually all the elements go into either SmallerThanPivot orLargerThanPivot
all through the recursive calls. Quicksort takes quadratic time to do essentially nothing at all.
One common method for selecting the pivot is the median-of-three rule,select the median of the first, middle, and last elementsin each sublist as the pivot.
Often the list to be sorted is already partially ordered median-of-three rule will select a pivot closer to the middle of the sublist
than will the “first-element” rule.
Quicksort Improvement III
For small files (n <= 20), quicksort is worse than insertion sort;small files occur often because of recursion.
Use an efficient sort (e.g., insertion sort) for small files.
Better yet, use Quicksort() until sublists are of a small size and thenapply an efficient sort like insertion sort.
MergesortSorting schemes are
internal -- designed for data items stored in main memoryexternal -- designed for data items stored in secondary memory.
Previous sorting schemes were all internal sorting algorithms:required direct access to list elements
( not possible for sequential files) made many passes through the list
(not practical for files)
mergesort can be used both as an internal and an external sort.basic operation in mergesort is merging, that is,combining two lists that have previously been sorted so that theresulting list is also sorted.