Professional Documents
Culture Documents
1
Chapter Objectives
Describe the three kinds of sorting methods
◦ Selection, exchange, and insertion
Study heaps, show how used for efficient selection sort, heapsort
◦ Look at implementation of priority queues using heaps
2
Sorting
Consider list
x1, x2, x3, … xn
We seek to arrange the elements of the list in order
◦ Ascending or descending
3
Categories of Sorting Algorithms
Selection sort
◦ Make passes through a list
◦ On each pass reposition correctly some element
4
Selection Sort
int i, j, minIndex, tmp;
for (i = 0; i < n - 1; i++)
{ minIndex = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[minIndex])
minIndex = j;
if (minIndex != i)
{
tmp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = tmp; }
}
5
Categories of Sorting Algorithms
Exchange sort
◦ Systematically interchange pairs of elements which are out of order
◦ Bubble sort does this
6
Bubble Sort Algorithm
1. Initialize numCompares to n - 1
2. While numCompares != 0, do following
a. Set last = 1 // location of last element in a swap
b. For i = 1 to numCompares
if xi > xi + 1
Swap xi and xi + 1 and set last = i
c. Set numCompares = last – 1
End while
7
Bubble Sort
for(r=0;r<5;r++) //outer loop is for the number of pass
for(c=0;c<5-1;c++) //inner loop will take charge of
the swapping of values
{ if(A[c] > A[c+1])
{ temp = A[c];
A[c] = A[c+1];
A[c+1] = temp;
}
}
8
Categories of Sorting Algorithms
Insertion sort
◦ Repeatedly insert a new element into an already sorted list
9
Algorithm for Linear Insertion Sort
For i = 2 to n do the following
a. set NextElement = x[i] and
x[0] = nextElement
b. set j = i
c. While nextElement < x[j – 1] do following
set x[j] equal to x[j – 1]
decrement j by 1
End wile
d. set x[j] equal to nextElement
End for
10
Example of Insertion Sort
Given list to be sorted
67, 33, 21, 84, 49, 50, 75
◦ Note sequence of steps carried out
11
Comparisons of Sorts
Sort of a randomly generated list of 500 items
◦ Note: times are on 1970s hardware
Algorithm Type of Sort Time (sec)
•Simple selection Selection 69
•Heapsort Selection 18
•Bubble sort Exchange 165
•2 way bubble sort Exchange 141
•Quicksort Exchange 6
•Linear insertion Insertion 66
•Binary insertion Insertion 37
•Shell sort Insertion 11
12
Heaps
A heap is a binary tree with properties:
1. It is complete
• Each level of tree completely filled
• Except possibly bottom level (nodes in left most positions)
13
Figure 9-1
HEAP STRUCTURE
Figure 9-2
Figure 9-3
BASIC HEAP
ALGORITHMS
ReheapUp
The reheapUp operation repairs a “broken” heap by floating the last
element up the tree until it is in its correct location in the heap.
Figure 9-4
Figure 9-5
ReheapDown
The reheapDown operation repairs a “broken” heap by pushing the
root down the tree until it is in its correct location in the heap.
Figure 9-6
Figure 9-7
HEAP DATA STRUCTURE
1. For a node located at index i, its children are found at
1. Left child : 2i + 1
2. Right child: 2i + 2
2. The parent of a node located at index i is located at
[(i - 1)/2].
3. Given the index of a left child, j, its right sibling if any,
is found at j + 1. Conversely, given the index for a
right child, k, its left sibling, which must exist, is
found at k - 1.
4. Given the size, n , of a complete heap, the location of
the first leaf is [(n/2)]. Given the first leaf element,
the location of the last nonleaf element is 1 less.
Figure 9-8
BuildHeap
1 walker =1
2 loop(walker<size)
1 reheap up (heap,walker)
2 walker=walker+1
3 end loop
4 return
end
Figure 9-9
InsertHeap
1 if (heap full)
1 return false
2 end if
3 last=last+1
4 heap[last]=data
5 reheapUp (heap,last)
6 return true
end
Figure 9-10
DeleteHeap
1 if (heap empty)
1 return false
2 end if
3 dataout=heap[0]
4 heap[0]=heap[last]
5 last=last-1
6 reheapDown (heap,0,last)
7 return true
8 end
Figure 9-11
Figure 9-12
Figure 9-15
39
Quicksort
If the list has 0 or 1 elements,
return. // the list is sorted
Else do:
Pick an element in the list to use as the pivot.
Split the remaining elements into two disjoint groups:
SmallerThanPivot = {all elements < pivot}
LargerThanPivot = {all elements > pivot}
40
Quicksort
etc. …
41
Quicksort Performance
O(nlog2n) is the average case computing time
◦ 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
42
Improvements to Quicksort
Quicksort is a recursive function
◦ stack of activation records must be maintained by system to manage
recursion.
◦ The deeper the recursion is, the larger this stack will become.
43
Improvements to Quicksort
Another improvement aimed at reducing the overhead of recursion is
to use an iterative version of Quicksort()
To do so, use a stack to store the first and last positions of the sublists
sorted "recursively".
44
Improvements to Quicksort
An arbitrary pivot gives a poor partition for nearly sorted lists (or lists
in reverse)
Virtually all the elements go into either SmallerThanPivot or
LargerThanPivot
◦ all through the recursive calls.
45
Improvements to Quicksort
Better method for selecting the pivot is the median-of-
three rule,
◦ Select the median of the first, middle, and last elements in
each sublist as the pivot.
46
Improvements to Quicksort
For small files (n <= 20), quicksort is worse than insertion sort;
◦ small files occur often because of recursion.
47
Mergesort
Sorting schemes are either …
◦ internal -- designed for data items stored in main memory
◦ external -- designed for data items stored in secondary memory.
48
Mergesort
Mergesort can be used both as an internal and an external sort.
Basic operation in mergesort is merging,
◦ combining two lists that have previously been sorted
◦ resulting list is also sorted.
49
Merge Algorithm
1. Open File1 and File2 for input, File3 for output
2. Read first element x from File1 and
first element y from File2
3. While neither eof File1 or eof File2
If x < y then
a. Write x to File3
b. Read a new x value from File1
Otherwise
a. Write y to File3
b. Read a new y from File2
End while
4. If eof File1 encountered copy rest of of File2 into File3. If eof
File2 encountered, copy rest of File1 into File3
50
Binary Merge Sort
Given a single file
51
Binary Merge Sort
Merge first one-element "subfile" of F1 with first one-element subfile
of F2
◦ Gives a sorted two-element subfile of F
52
Binary Merge Sort
Split again
Merge again as before
53
Binary Merge Sort
Last splitting gives two files each in order
54