Or Den a Mien To
-
Upload
angieloren -
Category
Documents
-
view
228 -
download
0
Transcript of Or Den a Mien To
-
8/8/2019 Or Den a Mien To
1/28
An Introduction to Sorting
Chapter 9
-
8/8/2019 Or Den a Mien To
2/28
2
Chapter ContentsSelection Sort Iterative Selection Sort
Recursive Selection Sort
The Efficiency of Selection Sort
Insertion Sort Iterative Insertion Sort
Recursive Insertion Sort
The Efficiency of Insertion Sort
Insertion Sort of a Chain of Linked Nodes
Shell Sort The Java Code
The Efficiency of Shell Sort
Comparing the Algorithms
-
8/8/2019 Or Den a Mien To
3/28
3
Selection Sort
Sorting: Arrange things into eitherascending or descending order
Task: rearrange books on shelf by height
Shortest book on the left
Approach:
Look at books, select shortest book
Swap with first book Look at remaining books, select shortest
Swap with second book
Repeat
-
8/8/2019 Or Den a Mien To
4/28
4
Selection Sort
Before and after exchanging shortest book
and the first book.
-
8/8/2019 Or Den a Mien To
5/28
5
Selection Sort
A selection sort of an
array of integers intoascending order.
-
8/8/2019 Or Den a Mien To
6/28
6
Iterative Selection Sort
Iterative algorithm for selection sort
Algorithm selectionSort(a, n)
// Sorts the firstnelements of an array a.
for (index = 0; index < n 1; index++)
{ indexOfNextSmallest = the index of the smallest value among
a[index], a[index+1], . . . , a[n1]
Interchange the values ofa[index]anda[indexOfNextSmallest]
//Assertion: a[0]ea[1]e. . . ea[index], and these are the smallest
// of the original array elements.
// The remaining array elements begin ata[index+1].
}
-
8/8/2019 Or Den a Mien To
7/28
7
Recursive Selection Sort
Recursive algorithm for selection sort
Algorithm selectionSort(a, first, last)
//Sorts the array elements
a[first]
through
a[last]
recursively.
if(first < last)
{ indexOfNextSmallest = the index of the smallest value among
a[first], a[first+1], . . . , a[last]
Interchange the values ofa[first]anda[indexOfNextSmallest]
//Assertion: a[0]ea[1]e. . . ea[first]and these are the smallest
// of the original array elements.
// The remaining array elements begin ata[first+1].
selectionSort(a, first+1, last)
}
-
8/8/2019 Or Den a Mien To
8/28
8
The Efficiency of Selection SortIterative method for loop executes n 1 times
For each of n 1 calls, the indexOfSmallest is invoked,
lastis n-1, and firstranges from 0 to n-2.
For each indexOfSmallest, compares last firsttimes
Total operations: (n 1) + (n 2) + + 1 = n(n 1)/2
= O(n2)
It does not depends on the nature of the data in
the array.Recursive selection sort performs same
operations
Also O(n2)
-
8/8/2019 Or Den a Mien To
9/28
9
Insertion Sort
If only one book, it is sorted.
Consider the second book, if shorter than firstone
Remove second book Slide first book to right
Insert removed book into first slot
Then look at third book, if it is shorter than 2nd
book Remove 3rd book
Slide 2nd book to right
Compare with the 1st book, if is taller than 3rd, slide 1st
to right, insert the3rd
book into first slot
-
8/8/2019 Or Den a Mien To
10/28
10
Insertion Sort
The placement of the
third book during an
insertion sort.
-
8/8/2019 Or Den a Mien To
11/28
11
Insertion Sort
Partitions the array into two parts. One part is sorted and initially
contains the first element.
The second part contains the remaining elements.
Removes the first element from the unsorted part and inserts it into its
proper sorted position within the sorted part by comparing with element
from the end of sorted part and toward its beginning.
The sorted part keeps expanding and unsorted part keeps shrinking byone element at each pass
-
8/8/2019 Or Den a Mien To
12/28
12
Iterative Insertion Sort
Iterative algorithm for insertion sortAlgorithm insertionSort(a, first, last)
// Sorts the array elements a[first]through a[last]iteratively.
for (unsorted=first+1through last)
{ firstUnsorted = a[unsorted]
insertInOrder(firstUnsorted, a, first, unsorted-1)
}
Algorithm insertInOrder(element, a, begin, end)
// Inserts elementinto the sorted array elements a[begin]through a[end].
index = endwhile ((index >= begin)and(element < a[index]))
{ a[index+1] = a[index] // make room
index- -
}
//Assertion: a[index+1]is available.
a[index+1] = element // insert
-
8/8/2019 Or Den a Mien To
13/28
13
Iterative Insertion Sort
An insertion sort inserts the next unsorted
element into its proper location within the
sorted portion of an array
-
8/8/2019 Or Den a Mien To
14/28
14
Iterative Insertion Sort
An insertion sort of an array of integers into
ascending order
-
8/8/2019 Or Den a Mien To
15/28
15
Recursive Insertion Sort
Algorithm for recursive insertion sort
Algorithm insertionSort(a, first, last)
// Sorts the array elements a[first]through a[last]recursively.
if(the array contains more than one element)
{ Sort the array elements a[first]through a[last-1]
Insert the last elementa[last]into its correct sorted position
within the rest of the array}
-
8/8/2019 Or Den a Mien To
16/28
16
Recursive Insertion Sortpublic static void insertionSort( Comparable[] a, int first, int last){
If ( first < last)
{
//sort all but the last element
insertionSort( a, first, last -1 );
//insert the last element in sorted order from first through last positions
insertInOrder(a[last], a, first, last-1);
}}
insertInorder( element, a, first, last)
If (element >= a[last])
a[last+1] = element;
else if (first < last)
{
a[last+1] = a[last];insertInOrder(element, a, first, last-1);
}
else // first == last and element < a[last]
{
a[last+1] = a[last];
a[last] = element
}
-
8/8/2019 Or Den a Mien To
17/28
17
Recursive Insertion Sort
Inserting the first unsorted
element into the sorted
portion of the array.
(a) The element is last
sorted element;
(b) The element is < than
last sorted element
-
8/8/2019 Or Den a Mien To
18/28
18
Efficiency of Insertion Sort
Best time efficiency is O(n)
Worst time efficiency is O(n2)
If array is closer to sorted order Less work the insertion sort does
More efficient the sort is
Insertion sort is acceptable for smallarray sizes
-
8/8/2019 Or Den a Mien To
19/28
19
Insertion Sort of Chain of Linked Nodes
A chain of integers sorted into ascending order.
-
8/8/2019 Or Den a Mien To
20/28
20
Insertion Sort of Chain of Linked Nodes
During the traversal of a chain to locate the
insertion point, save a reference to the node
before the current one.
-
8/8/2019 Or Den a Mien To
21/28
21
Insertion Sort of Chain of Linked Nodes
Breaking a chain of nodes into two pieces as
the first step in an insertion sort:
(a) the original chain; (b) the two pieces
Efficiency of
insertion sort ofa chain is O(n2)
-
8/8/2019 Or Den a Mien To
22/28
22
Shell Sort
A variation of the insertion sort
But faster than O(n2)
Done by sorting subarrays of equallyspaced indices
Instead of moving to an adjacent location
an element moves several locations away
Results in an almost sorted array
This array sorted efficiently with ordinary
insertion sort
-
8/8/2019 Or Den a Mien To
23/28
23
Shell Sort
Donald Shell suggested that the initial separation
between indices be n/2 and halve this value at eachpass until it is 1.
An array has 13 elements, and the subarrays formed
by grouping elements whose indices are 6 apart.
-
8/8/2019 Or Den a Mien To
24/28
24
Shell Sort
The subarrays after they are sorted, and the array
that contains them.
-
8/8/2019 Or Den a Mien To
25/28
25
Shell Sort
The subarrays by grouping elements whose indices are
3 apart
-
8/8/2019 Or Den a Mien To
26/28
26
Shell Sort
The subarrays after they are sorted, and the array that
contains them.
-
8/8/2019 Or Den a Mien To
27/28
27
Efficiency of Shell Sort
Efficiency is O(n2) for worst case
Ifn is a power of2
Average-case behavior is O(n1.5)
Shell sort uses insertion sort repeatedly.
Initial sorts are much smaller, the later
sorts are on arrays that are partiallysorted, the final sort is on an array that is
almost entirely sorted.
-
8/8/2019 Or Den a Mien To
28/28
28
Comparing the Algorithms
Best Average Worst
Case Case Case
Selection sort O(n2
) O(n2
) O(n2
)
Insertion sort O(n) O(n2) O(n2)
Shell sort O(n) O(n1.5) O(n1.5) or O(n2)
The time efficiencies of three sorting algorithms,
expressed in Big Oh notation.