60
Ceng-112 Data Structures I 2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Embed Size (px)

Citation preview

Page 1: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 1

Chapter 11

Sorting

Page 2: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 2

Figure 11-1

Page 3: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 3

Figure 11-2

Buble sort and straight insertion sort are stable, all of the others are unstable.

Page 4: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 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

Page 5: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 5

Figure 11-3

Insertion SortStraight 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.

Page 6: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 6

Figure 11-4

Insertion SortStraight Insertion Sort

Page 7: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 7

algorithm insertionSort( ref list <array>, val last <index>)

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 SortStraight Insertion Sort

Page 8: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 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 SortStraight Insertion Sort

Inside loop is quadraticly dependent to outer loopand outer loop executes n-1 times.

f(n)=n((n+1)/2)O(n2)

Page 9: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 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 SortShell Sort

Page 10: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 10

Figure 11-5

Insertion SortShell 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 14 9 30 21 80 25 70 55

77

62

14

9

30

21

80

25

70

55

Page 11: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 11

Figure 11-6, a

Insertion SortShell Sort

k=last/2k = 10/2=5

Page 12: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 12

Figure 11-6, b

Insertion SortShell Sort

k=5/2

Page 13: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 13

Figure 11-6, c and d

Insertion SortShell Sort k=3/2

Page 14: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 14

A[1] A[1 + K] A[1 + 2K] A[1 + 3K]

Sorted Unsorted

Insertion SortShell Sort

Page 15: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 15

algorithm shellSort( ref list <array>, val last <index>)

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 SortShell Sort

Page 16: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 16

1. incre = last /22. loop (incre not 0)

1. current = 1 + incre2. loop (current <= last)

1. hold = list[current]2. walker = current – incre3. loop (walker >= 1 AND hold.key < list[walker].key)

• list[walker+incre] = list[walker]• walker = walker – incre

4. list[walker + incre] = hold5. current = current + 1

3. incre = incre /23. returnend shellSort

Insertion SortShell Sort

log2n[(n-n/2)+(n-n/4)+..+1]=n.log2nO(n.log2n)

We still need to include the third loop!

O(n1.25)

executes log2n

n - increment times

each time n = n/2

Page 17: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 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

Page 18: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 18

Figure 11-8

Selection SortStraight 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.

Page 19: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 19

Figure 11-9

Selection SortStraight Selection Sort

Page 20: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 20

algorithm selectionSort( ref list <array>, val last <index>)

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 SortStraight Selection Sort

Page 21: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 21

1. current = 12. loop (current < last)

1. smallest = current2. walker = current +13. 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. returnend selectionSort

Selection SortStraight Selection Sort

O(n2)

executes n – 1 times

executes n – 1 times

Page 22: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 22

Figure 11-10

Selection SortHeap Sort

Page 23: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 23

Figure 11-11

Selection SortHeap Sort

Page 24: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 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.

Page 25: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 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.

Page 26: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 26

Figure 11-13

Exchange Sort Bubble Sort

currentwalker

Page 27: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 27

algorithm bubleSort( ref list <array>, val last <index>)

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

Page 28: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 28

• current = 1• sorted = false• loop (current <= last AND sorted false)

1. walker = last2. sorted = true3. loop (walker > current)

1. if (list[walker] < list[walker-1])• sorted = false• exchange (list, walker, walker-1)

2. walker = walker – 1

4. current = current + 11. returnend bubleSort

Exchange Sort Bubble Sort

f(n) = n((n+1)/2)O(n2)

executes n times

executes (n+1)/2 times

Page 29: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 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.

Page 30: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 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.

Page 31: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 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.

Page 32: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 32

Figure 11-14

Exchange Sort Quick Sort

>

>

Page 33: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 33

Figure 11-15>=

Exchange Sort Quick Sort

78 >62

84 >62

45 < 62

From right!21 < 62

14 < 62

97 > 62

From left!

Page 34: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 34

Figure 11-16

Exchange Sort Quick Sort Operation

Page 35: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 35

algorithm quickSort( ref list <array>, val left <index>,

val right <index>)

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

Page 36: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 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 + 11. loop ((list[sortRight].key >=

pivot.key)AND(sortRight<sortLeft))• sortRight = sortRight – 1

2. if (sortLeft < sortRight)1. Exchange(list, sortLeft, sortRight)2. sortLeft = sortLeft + 13. sortRight = sortRight – 1

• (prepare for next phase)1. list[left] = list[sortLeft – 1]• list[sortLeft – 1] = pivot• if (left < sortRight)

• quickSort(list, left, sortRight-1)– if (sortLeft < right)

• quickSort(list, sortLeft, right)• else insertionSort(list, left, right)end quickSort

O(n.log2 n)

Page 37: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 37

Quick Sort medianLeft Algorithm

algorithm medianLeft(ref sortData <array>, val left <index>, val right <index>)

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) / 22. 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)• Returnend medianLeft

Page 38: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 38

algorithm quickInsertion( ref list <array>, val first <index>, val last <index>)

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 quickInsertion Algorithm

Page 39: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 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 quickInsertion Algorithm

Page 40: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 40

Quick Sort Example

Page 41: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 41

Merge Sort

Page 42: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 42

Merge Sort

Page 43: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 43

Page 44: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 44

Page 45: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 45

Merge Sort

Page 46: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 46

Merge Sort

Page 47: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 47

Page 48: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 48

Page 49: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 49

Merging Pseudocode

Page 50: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 50

Page 51: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 51

Page 52: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 52

Page 53: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 53

Page 54: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 54

Page 55: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 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 outputC[1,..,k] working array to hold counts

Counting_Sort(A, B, k)1. // initialize the count array2. for i=1 to k3. C[i]=04. for j=1 to length[A]5. C[ A[j] ]=C[ A[j] ] + 16. // C[i] now contains the number of elements equal to k.7. for i=2 to k8. 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 111. B[ C[ A[j] ] ] = A[j]• C[ A[j] ] = C[ A[j] ] – 1• // Array B holds now the sorted sequence.

Page 56: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 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

Page 57: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 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]

Page 58: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 58

Sample for Counting Sort

for j= length[A] downto 1 B[ C[ A[j] ] ] = A[j] C[ A[j] ] = C[ A[j] ] – 1

Page 59: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 59

Sample for Counting Sort

Page 60: Ceng-112 Data Structures I2006 1 Chapter 11 Sorting

Ceng-112 Data Structures I 2006 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