data structures( 数据结构 ) course 11: advanced sorting
TRANSCRIPT
Data Structures(Data Structures( 数据结数据结构构 ))
Course 11: Advanced SortingCourse 11: Advanced Sorting
2西南财经大学天府学院
VocabularyVocabularySort (排序 )
Internal sort (内排序 )External sort (外排序 )Ascending order (升序 ) Descending order (降序 )
Insertion sort (插入排序 )Straight insertion sort (直接插入排序 )Shell sort (希尔排序 )
Selection sort (选择排序 )Straight selection sort (直接选择排序 )Heap sort (堆排序 )
Exchange sort (交换排序 )Bubble sort (冒泡排序 )Quick sort (快速排序 )
Stability (稳定性 )Pass (一趟排序 )
3西南财经大学天府学院
Highlights in this Highlights in this chapterchapter
General sort conceptSort orderSort stabilitySort efficiencypasses
Insertion sortsSelection sortsExchange sorts
4西南财经大学天府学院
11-1 General sort concept11-1 General sort concept
Internal sort External sortSort orderSort stabilitySort efficiencypasses
All of data are held in primary memory during the sorting process. It are classified as :
– Insertion Sort– Selection sort– Exchange sort– Merging sort– Distribution sort
Uses primary storage (内存 ) for the data currently being sorting and secondary storage (外存 ) for any data that will not fit in primary memory.
Data may be sorted in either ascending or descending order. The sort order identifies the sequence of the data
A measure of the relative efficiency of a sort. It is usually an estimate of the number of comparisons and moves required to order an unordered list.Each traversal of the during the sorting process.
5西南财经大学天府学院
11-2 Insertion sorts11-2 Insertion sorts
Insertion sort: in each pass of an insertion sort, one or more pieces of data are inserted into their correct location in an order list. In this section we study two insertion sorts, the straight insertion sort and the shell sort.
6西南财经大学天府学院
Straight insertion sortsStraight insertion sorts
In the straight insertion sort, the list at any moment is divided into two sublists: sorted and unsorted. In each pass, the first element of the unsort sublist is transferred to the sorted sublist by inserting it at the appropriate place.
Unsorted
23 45 78 8 32 56 After pass 2
Sorted
Unsorted
8 23 45 78 32 56 After pass 3
Sorted
8 23 32 45 78 56 After pass 4
Sorted
Unsorted
23 78 45 8 32 56 After pass 1
Sorted
8 23 32 45 56 78 After pass 5Sorted
Sorted
• Straight insertion sort example
• Algorithm insertionSort
Algorithm insertionSort ( ref list <array>, val last < index >)
Sort list[0…last] using insertion sort. The array is divided into sorted and unsorted lists. With each pass, the first element in the unsorted list is inserted into the sorted list. Pre list must contain at least one element
last is an index to last element in the list Post list has been rearranged1 Current = 12 Loop (Current <= last)
1 hold = list[Current ]2 walker = Current - 13 loop (walker >= 0 and hold.key < list[walker].key)
1 list[walker + 1] = list[walker]2 walker = walker – 1
4 end loop5 list[walker + 1] = hold6 current = current + 1
3 end loop4 returnEnd insertionSort
To improve the efficiency,we use a hold area.
Inner loop: start with high end of the sorted list and work toward the beginning of the sorted area.
7西南财经大学天府学院
Shell sortsShell sorts
The shell sorts is an improve version of the straight insertion sort, in which diminishing partition are used to sort data. The segments in a shell sort:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
A[0] A[0+k] A[0+2*k] A[0+3*k]K=3
Segment 1
Segment 1
Segment 1
A[1] A[1+k] A[1+2*k]
A[2] A[2+K] A[2+2*k]
increment
A pass: (k = 3) after pass through the data, the data in three segments are ordered 1 compare element A[0] and A[3], if out of sequence then changed 2 compare element A[1] and A[4], if out of sequence then changed 3 compare element A[2] and A[5], if out of sequence then changed 4 compare element A[3] and A[6], if out of sequence then changed 5 compare element A[4] and A[7], if out of sequence then changed 6 compare element A[5] and A[8], if out of sequence then changed 7 compare element A[6] and A[9], if out of sequence then changed
8西南财经大学天府学院
77 62 14 9 30 21 80 25 70 55
(a) First increment : k = 5
21 62 14 9 30 77 80 25 70 55
21 62 14 9 30 77 80 25 70 55
21 62 14 9 30 77 80 25 70 55
21 62 14 9 30 77 80 25 70 55
21 62 14 9 30 77 80 25 70 55
Diminishing increments in shell Diminishing increments in shell sortsort
(b) second increment : k = 221 62 14 9 30 77 80 25 70 55
14 62 21 9 30 77 80 25 70 55
14 9 21 62 30 77 80 25 70 55
14 9 21 62 30 77 80 25 70 55
14 9 21 62 30 77 80 25 70 55
14 9 21 62 30 77 80 25 70 55
14 9 21 25 30 62 80 77 70 55
14 9 21 25 30 62 70 77 80 55
14 9 21 25 30 55 70 62 80 77
(c) Third increment : k = 114 219 25 5530 70 8062 77
9 2114 25 5530 70 8062 77
9 2114 25 5530 70 8062 77
9 2114 25 5530 70 8062 77
9 2114 25 5530 70 8062 77
9 2114 25 5530 70 8062 77
9 2114 25 5530 70 8062 77
9 2114 25 5530 62 8070 779 2114 25 5530 62 8070 77
9 2114 25 5530 62 7770 80
9西南财经大学天府学院
algorithm Shellsortalgorithm Shellsort
Algorithm ShellSort ( ref list <array>, val last < index >)
Data in list[0], list[1], …, list[last] are sorted in place.
After the sort, their keys will be in order, list[0] <= list[1]<= … <= list[last].
Pre list is an unsorted array of records last is an index to last record in array
Post list is ordered on list[i].key
1 incre = last / 22 Loop (incre not 0)
1 Current = incre 2 Loop (Current <= last) 1 hold = list[Current ] 2 walker = Current - incre 3 loop (walker >= 0 and hold.key < list[walker].key)
1 list[walker + incre] = list[walker]
2 walker = walker – incre 4 end loop 5 list[walker + incre] = hold 6 current = current + 13 end loop4 incre = incre / 2
• End loop• returnEnd ShellSort
To reduce the number of elements that appear in multiple segments, we must consider the increment of shell sort. The better is use the prime number, but in most cases is use the simple series we proposed in this algorithm
10西南财经大学天府学院
insertion sort algorithm efficiencyinsertion sort algorithm efficiency
Straight insertion sort
2 Loop (Current <= last)……3 loop (walker >= 0 and hold.key < list[walker].key)
f(n) =( n-1) *(n+1)/2 In big-O notation: O(n2 )• Shell sort
2 Loop (incre not 0)1 Current = incre 2 Loop (Current <= last)
…… 3 loop (walker >= 0 and hold.key < list[walker].key)
In big-O notation: O(n1.25 )
The out loop executes n-1 times.The inner loop executes from 0 to current times.
The out loop is logarithmic, it excutes it executes log2n times.The first inner loop executes n-increment times. The innermost loop is the most difficult to analyze.
11西南财经大学天府学院
11-3 Selection sorts11-3 Selection sorts
Selection sort: In each pass of an selection sort, we select the smallest item of unsorted list and place it in the sorted list. In this section we study two selection sorts, the straight selection sort and the heap sort.
12西南财经大学天府学院
• Straight selection sort example
Straight selection sortsStraight selection sorts
In the straight selection sort, the list at any moment is divided into two sublists: sorted and unsorted. In each pass, we select the smallest element from the unsorted sublist and exchange it with the element at the beginning of the unsort data.
8 23 32 45 56 78 After pass 5Sorted
Sorted
• Algorithm selectionSort
Unsorted
8 78 45 23 32 56 After pass 1
Unsorted
8 23 45 78 32 56 After pass 2
Sorted
Unsorted
8 23 32 78 45 56 After pass 3
Sorted
8 23 32 45 78 56 After pass 4
Sorted Unsorted
Algorithm selectionSort ( ref list <array>, val last < index >)
Sort list[0…last] by selecting smallest element in unsorted portion of array and exchange it with element at the beginning of the unsorted list.
Pre list must contain at least one element last is an index to last element in the list
Post list has been rearranged smallest to largest1 Current = 02 Loop (Current < last)
1 smallest = Current 2 walker = Current + 13 loop (walker <= last)
1 if (list[walker] < list[smallest]) 1 smallest = walker2 walker = walker + 1
4 end loop5 exchange (list, current, smallest)6 current = current + 1
3 end loop4 returnEnd selectionSort
13西南财经大学天府学院
selection sort algorithm efficiencyselection sort algorithm efficiency
Straight selection sort
2 Loop (Current < last)……3 loop (walker <= last)
big-O notation: O(n2 )
The out loop executes n-1 times.The inner loop also executes n/2 times.
14西南财经大学天府学院
11-4 Exchange sorts11-4 Exchange sorts
Exchange sort: In each pass of an exchange sort, we exchange elements that are out of order, until the entire list is sorted. In this section we study two exchange sorts, the bubble sort and the most efficient general-purpose sort quick sort.
15西南财经大学天府学院
• Bubble sort example
Bubble sortsBubble sorts
In the bubble sort, the list at any moment is divided into two sublists: sorted and unsorted. In each pass, the smallest element is bubbled from the unsorted sublist and moved to the sorted sublist.
23 78 45 8 56 32 Original list
Unsorted
• Algorithm bubbleSort
Unsorted
8 23 78 45 32 56 After pass 1
Unsorted
8 23 32 78 45 56 After pass 2
Sorted
Unsorted
8 23 32 45 78 56 After pass 3
Sorted
8 23 32 45 56 78 After pass 4Sorted
Sorted Unsorted
Algorithm bubbleSort ( ref list <array>, val last < index >)
Sort an array, list[0…last], using bubble sort. Adjacent element are compared and exchanged until list is completely ordered.
Pre list must contain at least one element last is an index to last element in the list
Post list has been rearranged smallest to largest1 Current = 02 Sorted = false3 Loop (Current < last and sorted false)
1 walker = last2 sorted = true3 loop (walker > current)
1 if (list[walker] < list[walker - 1]) 1 sorted = false 2 exchange(list, walker, walker – 1)2 end if3 walker = walker - 1
4 end loop5 current = current + 1
3 end loop4 returnEnd bubbleSort
Any exchange means list is not sorted.
Each iteration is one sort pass.
If any exchange is not made in a passs, then we know the list is already sorted and the sort can stop.
16西南财经大学天府学院
Quick sortsQuick sorts
In the Quick sort, we select an element as pivot, the list is divided into three group: a partition of elements whose keys are less than the pivot’s key, the pivot element that is place in correct location, and a partition of elements greater than or equal pivot’s key. The sorting then continues by quick sorting the left partition. If the left partition is sorted, then quick sorting the right partition.
• Quick sort partitions
After second partitioning
< pivot >= pivotpivotAfter third partitioning
Sorted After fourth partitioning
Sorted
After seventh partitioning
Sorted
After sixth partitioning
Sorted
After fifth partitioning
Sorted < pivot >= pivotpivot
17西南财经大学天府学院
In addition to basic algorithm, two supporting algorithm are required: determine the pivot, and the straight insertion sort.
Find the median value of an array, sortData[left…right], and place it in the location sortData[left].
Sort list[first…last] using insertion sort. The list is divided into sorted and unsorted lists. With each pass, the first element in the unsorted list is inserted into the sorted list. This is a special version of the insertion sort modified for use with quick sort.
• determinie median of three
• Straight insertion module
Quick Sorts AlgorithmQuick Sorts AlgorithmAlgorithm quickinsertionSort ( ref list <array>,
val fist < index >, val last < index >)
Pre list must contain at least one elementfirst is an index to first element in the list
last is an index to last element in the list Post list has been rearranged1 Current = first + 12 Loop (Current <= last)
1 hold = list[Current ]2 walker = Current - 13 loop (walker >= first and hold.key < list[walker].key)
1 list[walker + 1] = list[walker]2 walker = walker – 1
4 end loop5 list[walker + 1] = hold6 current = current + 1
3 end loop4 returnEnd insertionSort
Algorithm medianLeft ( ref sortDatat <array>, val left < index >, val right < index >)
Pre sortDatat is an array of at least three elements left and right are the boundaries of array
Post median value located and place at sortData[left]
1 mid = (left + right) / 22 If (sortData[left].key > sortData[mid].key)
1 exchange (sortData, left, mid)3 end if4 If (sortData[left].key > sortData[right].key)
1 exchange (sortData, left, right)5 end if6 If (sortData[mid].key > sortData[right].key)
1 exchange (sortData, mid, right)7 end ifMedian is in middle location, exchange with left.8 exchange (sortData, left, mid)9 returnEnd medianLeft
18西南财经大学天府学院
• Quick sort pivot
Quick Sorts Algorithm Quick Sorts Algorithm (continue)(continue)62 21 14 97 87 78 74 85 76 45 84 22
22 21 14 97 87 78 74 85 76 45 84 62
22 21 14 97 87 62 74 85 76 45 84 78
Exchange
62 21 14 97 87 22 74 85 76 45 84 7897
sortLeft
sortRight
45
62
62 21 14 45 87 62 74 85 76 97 84 7887
sortLeft
sortRight
22
Exchange
62
62 21 14 45 22 87 74 85 76 97 84 78
sortRight sortLeft
Move62
22 21 14 45 62 87 74 85 76 97 84 78
< pivot
62
pivot >= pivot
• Quick sort operation
78 21 14 97 87 62 74 85 76 45 84 22
6222 21 14 45 87 74 85 76 97 84 78
22
14 21 45
14 21
21
45
78
76 74 85 97 84 87
74 76
76
84 85 97
84 85
85
97
87
19西南财经大学天府学院
An array ,list [left…right] is sorted using recursion.Pre list is an array of data to be sorted
left and right identify the first and last elements of the list, respectivelyPost list is sorted
• Quick sort algorithm: quickSort
Quick Sorts Algorithm Quick Sorts Algorithm (continue)(continue)Algorithm quickSort ( ref list <array>,
val left < index >, val right < index
>)1 If (( right – left ) > minSize
1 medianLeft (list, left, right)2 pivot = list[left]3 sortLeft = left + 14 sortRight = right5 loop ( sortLeft <= sortright) 1 loop (list[sortLeft].key < pivot.key)
1 sortLeft = sortLeft + 1 2 end loop 3 loop (list[sortRight].key >= pivot.key
1 sortRight = sortRight – 1 4 end loop 5 if (sortLeft <= sortRight )
1 exchange (list, sortLeft, sortRight )
2 sortLeft = sortLeft +1 3 sortRight = sortRight –1
6 end if6 end loop
7 list [left] = list [sortLeft - 1]8 list [sortLeft - 1] = pivot9 if (left < sortRight ) 1 quickSort (list, left, sortRight - 1)10 end if11 if (sortLeft < right ) 1 quickSort (list, sortLeft, right)12 end if
2 Else1 insertionSort (list, left, right)
3 end ifEnd quickSort
20西南财经大学天府学院
Exchange Sorts Algorithm Exchange Sorts Algorithm efficiencyefficiency
Bubble sort
3 Loop (Current <= last and sorted false)……3 loop (walker > Current )
f(n) = n (n+1)/2 In big-O notation: O(n2 )
• Quick sort
f(n) = n (log2n + log2n) =2nlog2n, In big-O notation: O(nlog2n)
5 Loop (Current <= sortRight)…… 1 loop (list[sortLeft].key < pivot.key)
…… 3 loop (list[sortRight].key >= pivot.key
……6 end loop
21西南财经大学天府学院
Summary of this chapterSummary of this chapterOne of the most common application in computer science is sorting.Sorts are generally classified as either internal or external.
In an internal sort, all of the data are held in primary storage during the sorting process.An external sort uses primary storage for the data currently being sorting and secondary storage for any data that will not fit in primary memory.
Data may be sorted in either ascending or descending order.Sort stability is an attribute of sort indicating that data with equal keys maintain their relative input order in the output.Sort efficiency is a measure of the relative efficiency of a sort.Each traversal of the during the sorting process is referred to as a pass.Internal sorting can be divided into three broad categories: insertion, selection, and exchange.
22西南财经大学天府学院
Summary of this chapter Summary of this chapter (Continue)(Continue)
Two methods of insertion sorting were discussed in this chapter: straight insertion sort and shell sort.
In the straight insertion sort, the list at any moment is divided into two sublists: sorted and unsorted. In each pass, the first element of the unsort sublist is transferred to the sorted sublist by inserting it at the appropriate place.The shell sort algorithm is an improved version of the straight insertion sort, in which the process uses difference increments to sort the list. In each increment, the list is divided into segments, which are sorted independent of each other.
Two methods of selection sorting were discussed in this chapter: straight selection sort and heap sort.
In the straight selection sort, the list at any moment is divided into two sublist: sorted and unsorted. In each pass, the process selects the smallest element from the unsorted sublist and exchange it with the element at the beginning of the unsorted sublist.
23西南财经大学天府学院
Summary of this chapter Summary of this chapter (Continue)(Continue)Two methods of exchange sorting were discussed in this
chapter: bubble sort and quick sort. In the bubble sort, the list at any moment is divided into two sublists: sorted and unsorted. In each pass, the smallest element is bubble up from the unsorted sublist and moved to the sorted sublist.The quick sort is the new version of exchange sort in which the list is continuously divided into smallest sublists and exchange takes place between elements that are out of order. Each pass of the quick sort selects a pivot and divides list into three groups: a partition of elements whose key is less than the pivot element that is place in its ultimate correct position, and a partition of elements greater than or equal to the pivot’s key. The sorting then continues by quick sorting the left partition followed by quick sorting the right partition.
The efficiency of straight insertion, straight selection, and bubble sort is O(n2).The efficiency of shell sort is O(n1.25), and The efficiency of quickl sort is O(nlog2n).
24西南财经大学天府学院
ExercisesExercisesAn array contains the elements shown below. The first two elements have been sorted using a straight insertion sort, what would be the value of the elements in the array after three more passes of the straight insertion sort algorithm?3,13,7,26,44,23,98,57An array contains the elements shown below. Show the contents of the array after it has gone through a one-increment of the shell sort. The increment factor K=323,3,7,13,89,7,66,2,6,44,18,90,98,57An array contain the elements shown below. The first two elements have been sorted using a straight selection sort. What would be the value of the elements in the array after three more passes of the selection sort algorithm?7,8,26,44,13,23,98,57
25西南财经大学天府学院
ExercisesExercisesAn array contains the elements shown below. using a quick sort show the contents of the array after the first pivot has been placed in its correct location. Identify the three sublists that exist at that point .44,78,22,7,98,56,34,2,38,35,45After two passes of a sorting algorithm ,the following array: 47,3,21,32,56,92has been rearranged as shown below. 3,21,47,32,56,92which sorting algorithm is being used.(straight insertion, buble, straight selection)
26西南财经大学天府学院
ExercisesExercisesAfter two passes of a sorting algorithm ,the following array: 80,72,66,44,21,33has been rearranged as shown below. 21,33,80,72,66,44which sorting algorithm is being used. .(straight insertion, buble, straight selection)After two passes of a sorting algorithm ,the following array: 47,3,66,32,56,92has been rearranged as shown below. 3,47,66,32,56,92which sorting algorithm is being used. .(straight insertion, buble, straight selection)
27西南财经大学天府学院
HomeworkHomeworkRealize straight insertion and straight selection algorithm using array data shown below. 7,8,26,44,13,23,98,57