Download presentation
Presentation is loading. Please wait.
Published byShawn Green Modified over 9 years ago
1
Ceng-112 Data Structures I2006 1 Chapter 11 Sorting
2
Ceng-112 Data Structures I2006 2 Figure 11-1
3
Ceng-112 Data Structures I2006 3 Figure 11-2 Buble sort and straight insertion sort are stable, all of the others are unstable.
4
Ceng-112 Data Structures I2006 4 It uses two pieces of data to sort: sorted and unsorted. In each pass of a sort, one or more pieces of data are inserted into their correct location. 1.The straight insertion sort 2.The shell sort Insertion Sort
5
Ceng-112 Data Structures I2006 5 Figure 11-3 Insertion Sort Straight Insertion Sort It will take at most n-1 passes to sort the data. It uses two pieces of data to sort: sorted and unsorted. In each pass, the first element of the the unsorted sublist is transfered to the sorted sublist by insertion it at appropiate place.
6
Ceng-112 Data Structures I2006 6 Figure 11-4 Insertion Sort Straight Insertion Sort
7
Ceng-112 Data Structures I2006 7 algorithm insertionSort( ref list, val last ) Sort list[1..last] using insertion sort. The array is divided into sorted and unsorted lists. With each pass, the first element of unsorted list is inserted into sorted list. Pre List must contain at least one element. last is an index to last element in the list. Post List has been rearranged. Insertion Sort Straight Insertion Sort
8
Ceng-112 Data Structures I2006 8 1.current = 2 2.loop (current <= last) 1.hold = list[current] 2.walker = current -1 3.loop (walker >=1 AND hold.key < list[walker].key) list[walker+1] = list[walker] walker = walker -1 4.list[walker +1] = hold 5.current = current +1 3.return end insertionSort Insertion Sort Straight Insertion Sort Inside loop is quadraticly dependent to outer loop and outer loop executes n-1 times. f(n)=n((n+1)/2) O(n 2 )
9
Ceng-112 Data Structures I2006 9 The creator of this algorithm is Donald L.Shell. The list is divided into K segments, where K is known as the increment value. Each segment contains N/K or less elemets. After each pass through, the data in each segment are ordered. If there is only one segment, then the list is sorted. Insertion Sort Shell Sort
10
Ceng-112 Data Structures I2006 10 Figure 11-5 Insertion Sort Shell Sort K = 3, three segments and K is an increment value. Each segment includes N/K or less elements. If there is only one segment, then the list is sorted. 77 62 149 30 21 80 25 70 55 77 62 14 9 30 21 80 25 70 55
11
Ceng-112 Data Structures I2006 11 Figure 11-6, a Insertion Sort Shell Sort k=last/2 k = 10/2=5
12
Ceng-112 Data Structures I2006 12 Figure 11-6, b Insertion Sort Shell Sort k=5/2
13
Ceng-112 Data Structures I2006 13 Figure 11-6, c and d Insertion Sort Shell Sort k=3/2
14
Ceng-112 Data Structures I2006 14 A[1]A[1 + K] A[1 + 2K] A[1 + 3K] Sorted Unsorted Insertion Sort Shell Sort
15
Ceng-112 Data Structures I2006 15 algorithm shellSort( ref list, val last ) Sort list[1], list[2],..,list[last] are sorted in place. After the sort, their keys will be in order, list[1].key <= list[2].key <=..<=list[last].key. Pre List is an unordered array of records. last is an index to last record in array. Post List is ordered on list[i].key. Insertion Sort Shell Sort
16
Ceng-112 Data Structures I2006 16 1.incre = last /2 2.loop (incre not 0) 1.current = 1 + incre 2.loop (current <= last) 1.hold = list[current] 2.walker = current – incre 3.loop (walker >= 1 AND hold.key < list[walker].key) list[walker+incre] = list[walker] walker = walker – incre 4.list[walker + incre] = hold 5.current = current + 1 3.incre = incre /2 3.return end shellSort Insertion Sort Shell Sort log 2 n[(n-n/2)+(n-n/4)+..+1]=n.log 2 n O(n.log 2 n) We still need to include the third loop! O(n 1.25 ) executes log 2 n n - increment times each time n = n/2
17
Ceng-112 Data Structures I2006 17 Selection Sort We simply select the smallest item in the list and place it in a sorted list. These steps are repeated until all of data have been sorted. 1.Straight Selection Sort 2.Heap Sort
18
Ceng-112 Data Structures I2006 18 Figure 11-8 Selection Sort Straight Selection Sort The list is divided into two sublists, sorted and unsorted. Select the smallest element from unsorted sublist and exchange it with the element at the beginning of the unsorted data. The wall between two sublists moves one element.
19
Ceng-112 Data Structures I2006 19 Figure 11-9 Selection Sort Straight Selection Sort
20
Ceng-112 Data Structures I2006 20 algorithm selectionSort( ref list, val last ) Sort list[1..last] by selecting smallest element in unsorted portion of array and exchanging it with element at the beginning of the unordered list. Pre List is must contain at least one item. last is an index to last record in array. Post List has been rearranged smallest to largest. Selection Sort Straight Selection Sort
21
Ceng-112 Data Structures I2006 21 1.current = 1 2.loop (current < last) 1.smallest = current 2.walker = current +1 3.loop (walker <= last) 1.if ( list[walker] < list[smallest]) smallest = walker 2.walker = walker + 1 smallest selected: exchange with current element! exchange (list, current, smallest) current = current + 1 3.return end selectionSort Selection Sort Straight Selection Sort O(n 2 ) executes n – 1 times
22
Ceng-112 Data Structures I2006 22 Figure 11-10 Selection Sort Heap Sort
23
Ceng-112 Data Structures I2006 23 Figure 11-11 Selection Sort Heap Sort
24
Ceng-112 Data Structures I2006 24 Exchange Sort Exchange sorting, contains: 1.The most common sort in computer science; the buble sort. 2.The most efficient general purpose sort; the quick sort.
25
Ceng-112 Data Structures I2006 25 Figure 11-12 Exchange Sort Bubble Sort The list is divided into two sublists; sorted and unsorted. The smallest element is bubbled from the unsorted to the sorted list. The wall moves one element to the right. This sort requires n-1 passes to sort the data.
26
Ceng-112 Data Structures I2006 26 Figure 11-13 Exchange Sort Bubble Sort current walker
27
Ceng-112 Data Structures I2006 27 algorithm bubleSort( ref list, val last ) Sort an array, list[1..last] using buble sort.Adjacent elements are compared and exchanged until list is completely ordered. Pre List is must contain at least one item. last is an index to last record in array. Post List has been rearranged smallest to largest. Exchange Sort Bubble Sort
28
Ceng-112 Data Structures I2006 28 current = 1 sorted = false loop (current <= last AND sorted false) 1.walker = last 2.sorted = true 3.loop (walker > current) 1.if (list[walker] < list[walker-1]) sorted = false exchange (list, walker, walker-1) 2.walker = walker – 1 4.current = current + 1 1.return end bubleSort Exchange Sort Bubble Sort f(n) = n((n+1)/2) O(n 2 ) executes n times executes (n+1)/2 times
29
Ceng-112 Data Structures I2006 29 Exchange Sort In the buble sort, consecutive items are compared and possibly exchanged on each pass through the list, which means that many exchanges may be needed to move an element to its correct position. Quick sort is exchanged involves elements that are far apart so that fewer exchanges are required to correctly position an element.
30
Ceng-112 Data Structures I2006 30 Hw - 12 Create an array has 40 elements which are selected randomly between 0 and 1000. Print the unsorted array. Use the shell sort to sort the array. Print the sorted array elements again. Load your HW-12 to FTP site until 01 June 07 at 09:00.
31
Ceng-112 Data Structures I2006 31 Exchange Sort Quick Sort Selects an element which is called as “pivot” for each iteration. Divides the list into three groups. 1.The elements of first group which has key values less then key of pivot. 2.The pivot element. 3.The elements of first group which has key values greater then key of pivot. The sorting continues by quick sorting the left partition followed by a quick sort of the right partition.
32
Ceng-112 Data Structures I2006 32 Figure 11-14 Exchange Sort Quick Sort > >
33
Ceng-112 Data Structures I2006 33 Figure 11-15 >= Exchange Sort Quick Sort 78 >62 84 >62 45 < 62 From right! 21 < 62 14 < 62 97 > 62 From left!
34
Ceng-112 Data Structures I2006 34 Figure 11-16 Exchange Sort Quick Sort Operation
35
Ceng-112 Data Structures I2006 35 algorithm quickSort( ref list, val left, val right ) 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 respectively. Post the list is sorted. Exchange Sort Quick Sort
36
Ceng-112 Data Structures I2006 36 1.if ((right – left) > minsize)// quick sort medianLeft( list, left, right) pivot = list(left) sortLeft = left +1 sortRight = right loop (sortLeft <= sortRight) (find keys on left that belongs on right) loop ((list[sortLeft].key < pivot.key)AND(sortLeft < sortRight)) sortLeft = sortLeft + 1 1.loop ((list[sortRight].key >= pivot.key)AND(sortRight<sortLeft)) sortRight = sortRight – 1 2.if (sortLeft < sortRight) 1.Exchange(list, sortLeft, sortRight) 2.sortLeft = sortLeft + 1 3.sortRight = sortRight – 1 (prepare for next phase) list[left] = list[sortLeft – 1] list[sortLeft – 1] = pivot if (left < sortRight) quickSort(list, left, sortRight-1) 1.if (sortLeft < right) 1.quickSort(list, sortLeft, right) else insertionSort(list, left, right) end quickSort O(n.log 2 n)
37
Ceng-112 Data Structures I2006 37 Quick Sort medianLeft Algorithm algorithm medianLeft(ref sortData, val left, val right ) Find the median value of an array, sortData[left, right], and place it in the location sortData[left]. Pre sortData is an array of at least three elements left and right are the boundaries of the array. Post median value located and placed at sortData[left]. Rearrange sortData so median value is in the middle location. 1.mid = (left + right) / 2 2.if (sortData[left].key > sortData[mid].key) exchange(sortData, left, mid) 3.if (sortData[left].key > sortData[right].key) exchange(sortData, left, right) 4.if (sortData[mid].key > sortData[right].key) exchange(sortData, mid, right) (Median in inthe middle location, exchange it with left). exchange(sortData, left, mid) Return end medianLeft
38
Ceng-112 Data Structures I2006 38 algorithm quickInsertion( ref list, val first, val last ) Sort list[1..last] using insertion sort. The array is divided into sorted and unsorted lists. With each pass, the first element of unsorted list is inserted into sorted list. Pre List must contain at least one element. first is an index to the first element in the list. last is an index to last element in the list Post List has been rearranged. Quick Sort quick Insertion Algorithm
39
Ceng-112 Data Structures I2006 39 1.current = first +1 2.loop (current <= last) 1.hold = list[current] 2.walker = current -1 3.loop (walker >=first AND hold.key < list[walker].key) list[walker+1] = list[walker] walker = walker -1 4.list[walker +1] = hold 5.current = current +1 3.return end quickInsertion Quick Sort quick Insertion Algorithm
40
Ceng-112 Data Structures I2006 40 Quick Sort Example
41
Ceng-112 Data Structures I2006 41 Merge Sort
42
Ceng-112 Data Structures I2006 42 Merge Sort
43
Ceng-112 Data Structures I2006 43
44
Ceng-112 Data Structures I2006 44
45
Ceng-112 Data Structures I2006 45 Merge Sort
46
Ceng-112 Data Structures I2006 46 Merge Sort
47
Ceng-112 Data Structures I2006 47
48
Ceng-112 Data Structures I2006 48
49
Ceng-112 Data Structures I2006 49 Merging Pseudocode
50
Ceng-112 Data Structures I2006 50
51
Ceng-112 Data Structures I2006 51
52
Ceng-112 Data Structures I2006 52
53
Ceng-112 Data Structures I2006 53
54
Ceng-112 Data Structures I2006 54
55
Ceng-112 Data Structures I2006 55 Counting Sort The counting sort is a linear algorithm to sort a list. The pseudocode algorith is: Arrays: A[1,..,n] original unsorted array B[1,..,n] array to hold sorted output C[1,..,k] working array to hold counts Counting_Sort(A, B, k) 1.// initialize the count array 2.for i=1 to k 3. C[i]=0 4.for j=1 to length[A] 5. C[ A[j] ]=C[ A[j] ] + 1 6.// C[i] now contains the number of elements equal to k. 7.for i=2 to k 8. C[i] = C[i] + C[i-1] 9.// C[i] now contains the number of elements less than or equal to i. 10.for j= length[A] downto 1 11. B[ C[ A[j] ] ] = A[j] 12. C[ A[j] ] = C[ A[j] ] – 1 13.// Array B holds now the sorted sequence.
56
Ceng-112 Data Structures I2006 56 Sample for Counting Sort for i=1 to k C[i]=0 for j=1 to length[A] C[ A[j] ]=C[ A[j] ] + 1
57
Ceng-112 Data Structures I2006 57 Sample for Counting Sort for j= length[A] downto 1 B[ C[ A[j] ] ] = A[j] C[ A[j] ] = C[ A[j] ] – 1 for i=2 to k C[i] = C[i] + C[i-1]
58
Ceng-112 Data Structures I2006 58 Sample for Counting Sort for j= length[A] downto 1 B[ C[ A[j] ] ] = A[j] C[ A[j] ] = C[ A[j] ] – 1
59
Ceng-112 Data Structures I2006 59 Sample for Counting Sort
60
Ceng-112 Data Structures I2006 60 Lineer time sorting algorithm and no comparisons needed. Assume n elements in range 1 to k. When k = O(n), running time is O(n). Counting Sort
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.