NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

48
NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT SEARCH AND SORT 1

Transcript of NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

Page 1: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

CHAPTER 7

SEARCH AND SORTSEARCH AND SORT

1

Page 2: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Search

Ⅰ. Internal v.s External search

Ⅱ. Static v.s Dynamic search

2

Page 3: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Ⅲ. Partial key v.s whole key search

Ⅳ. Actual key v.s Transformation key

Search-Linear SearchBinary SearchFibonacci SearchInterpolation Search

3

Search

Page 4: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

If list has n records, with list[i].key referring to the key value for record i, then we can search the list by examining the key values list[0].key,…,list[n-1].key, in that order, until the correct record is located, or we have examined all the records in the list. Since we examine the records in sequence, this searching technique is known as a sequential search.

4

1 2 3 4 ... ... n-2 n-1 n

… …

2

1)....321(

n

n

n

Linear Search

Average comparison

Page 5: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

• This search begins by comparing searchnum and list[middle].key where middle .There are three possible outcomes:

searchnum< list[middle].key: In this case, we discard the records between list[middle] and list[n-1], and continue the search with the records between list[0] and list[middle-1].

5

2

)1(

n

Binary Search

Page 6: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

searchnum = list[middle].key:

In this case, the search terminates successfully.

searchnum > list[middle].key: In this case, we discard the records between list[0] and list[middle] and continue the search with the records between list[middle+1] and list[n-1].

6

Binary Search

Page 7: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Procedure BinSearch (f: afile ; var i:integer ; n,k:integer)

Var done : boolean ; l,u,m: integer ;Begin

l:=1 ; u:=n ; i:=0 ; done:=false ; while ((l<=u) and(not done)) do Begin

m:=(l+u) div 2 ; case compare( k , f[m].key) of

〝 > 〞 : l := m+1 {Look in upper half}

〝 = 〞 : Begin

i:=m ; done:= true ; End ; 〝 < 〞 : u:=m-1 ; {Look in lower half}

end ; {of case}

End ; {of while}

End ; {of BinSearch}

7

Binary Search

Page 8: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Number =12

Decision tree is

8

Binary Search

Page 9: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Binary

1.Time Complexity

∵T(n) = T(n/2)+1 , T(1) = 1

∴T(n) = O( )

2.If n is small Sequential Search

n is large Binary Search

9

n2log

Binary Search

Page 10: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Fibonacci Search F0=0 , F1 = 1 , Fi = Fi-1+Fi-2,i 2≧

0,1,1,2,3,5,8,13,21,34,55,…

〝 Fa+m = n+1 〞

(1)n:record number

(2)Fa: the largest Fibonacci number and

it is n+1 )≦ (3)m: 0≧ and it is natural number

10

Fibonacci Search

Page 11: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

(1)If n= 33, Fa = ? m = ?

Steps 1.Find the largest Fibonacci number ( ≦n+1≦ 34 )→ F9

Steps 2.m= (n+1)-Fa = 34-34=0

11

n 0 1 2 3 4 5 6 7 8 9

Fn 0 1 1 2 3 5 8 13 21 34

Fibonacci Search

Page 12: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Advantage & Disadvantage:

12

Fibonacci Search

Page 13: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Compare and

f[u].key , f[l].key is the largest value and the smallest value

Campare (k , f[l].key)

〝 = 〞 : find

〝 < 〞 :u=(l+i)-1

〝 > 〞 :l=(l+i)+1

13

)1(].[].[

].[

lukeylfkeyuf

keylfki

keylf ].1[ k

Interpolation Search

Page 14: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

CategoriesCategories:

(1) Internal or External Sorting?

(2) Stable or Unstable Sorting?

(3) Time Complexity

14

Sorting

Page 15: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

The step of 26 、 5 、 49 、 13 、 6

Ans: (1)5 、 26 、 49 、 13 、 6

(2)5 、 26 、 49 、 13 、 6

(3)5 、 13 、 26 、 49 、 6

(4)5 、 6 、 13 、 26 、 49

15

Insertion Sort

Page 16: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Algorithm

(1)Insert(r, A[ ],i) Dubroutines

(2)Insort(A[ ],n) main

(1)

Procedure Insert(r , A[ ], i)

Var j : integer ;Begin

j:= i ;While r.key<list[j].key Do Begin

list[j+1]:=list[j] ; j:= j-1 ;End ; List[j+i]:= r ;End ;

16

Page 17: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

(2)

Procedure Insort(Var list : afile ; n:Integer) ;Var j: Integer ;Begin

list[0].key:= -∞ ; for j:= 2 to n do

insert (list[j], list, j-1) ;End ;

17

Insertion Sort

Page 18: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Ⅰ. Time complexity :

worst case & average case = O( )

best case= O(n) n-1 Comparative

Ⅱ. Insertion Sort is stable

e.g ……5… 5+ …

after:……5…… 5+ …

Ⅲ. Space Complexity = O(1)

18

2n

Insertion Sort

Page 19: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

64 25 12 22 11

-> 11 25 12 22 64

-> 11 12 25 22 64

-> 11 12 22 25 64 -> 11 12 22 25 64

19

Selection Sort

Page 20: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Procedure SelectSort(R,n)Begin For i:= 1 to n-1 do Begin m := i ; For j = i+1 to n do If kj< km then m := j ; End ; {of For loop} If i < > m then Begin Swap (Ri,Rm) ; End ;End ; {of SelectSort}

20

Selection Sort

Page 21: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Ⅰ. Time complexity:

Best 、 Average 、 Worst case=O(n2)

Ⅱ. Space complexity : O(1)

Ⅲ. unstable sort

e.g.

pass1:

pass2:

21

2.5.5

5.5.2

5.5.2

Selection Sort

Page 22: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Bubble sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.

e.g. Step of 26 、 5 、 47 、 19 、 6

pass1 :5 、 26 、 19 、 6 、 47

pass2 :5 、 19 、 26 、 6 、 47

pass3 :5 、 6 、 19 、 26 、 47

pass4 :5 、 6 、 19 、 26 、 4722

Bubble Sort

Page 23: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Procedure BubbleSort(R, n)Begin for i=1 to (n-1) do begin f = 0; for j=1 to (n-1) do if R[j+1].key<R[j].key then [swap(Rj, Rj+1); f=1; ] if f=0 then exit // No swap: exit// end;End; {of BubbleSort}

23

Bubble Sort

Page 24: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Ⅰ. Time complexity :

Best case : O(n)

Worst case : O(n2)

Average case : O(n2)

Ⅱ . Bubble Sort is a stable sort

Ⅲ. Space complexity : O(1)

24

Bubble Sort

Page 25: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

25

Given: (R0, R1, R2, R3, ……….., Rn-3, Rn-2 Rn-1)

After first pass: R1, ..…, RS(i)-1, R0, RS(i), RS(i)+1, …, RS(n-1)

Pivot key

i j

two partitions

Quick Sort

Page 26: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

R1 R2 R3 R4 R5 R6 R7 R8 R9 R10

26 5 37 1 61 11 59 15 48 19

11 5 19 1 15 26 59 61 48 37

1 5 11 19 15 26 59 61 48 37

1 5 11 15 19 26 59 61 48 37

1 5 11 15 19 26 48 37 59 61

1 5 11 15 19 26 37 48 59 61

1 5 11 15 19 26 37 48 59 61

1 5 11 15 19 26 37 48 59 61

26

Quick Sort

Page 27: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

void procedure QuickSort(list, m, n) if (m<n) then i=m, j=n+1, p.k.=list[m].key Repeat repeat i=i+1 until list[i].key ≥p.k. repeat j=j-1 until list[i].key ≤p.k. if (i<j) then swap(list[i], list[j]) Until i ≥ j swap(list[m], list[j]) QuickSort(list, m, j-1) QuickSort(list, j+1, n)End ; {of if}End ; {of Qsort}

27

Quick Sort

Page 28: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Ⅰ. Time complexity:

Best case

T(n)= c*n + 2T( )

= 4*T ( )+2cn

= n*T ( )+

= n+ cnlogn

= O(nlogn)28

2

n

4

n

n

n cnnlog

Quick Sort

Page 29: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Worst caseT(n) cn + T(n-1)≦ ≦ cn + T( cn + T(n-2)) ≦ 2cn + T(n-2) ≦ 3cn + T(n-3) . . ≦ (n-1)cn + T(1) = O( )

29

2n

Quick Sort

Page 30: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Average case-> Time complexity is O(nlogn)

Ⅱ. Quick Sort is unstable

Ⅲ. Space complexity: O(logn)~O(n)

30

Quick Sort

Page 31: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

It merges the sorted lists(list[i],…,list[m]) and (list[m+1],…,list[n]), into a single sorted list,(sorted[i],…,sorted[n]).

Merge sort:•Iterative•Recursive

31

Merge Sort

Page 32: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

e.g. 26 、 5 、 77 、 1 、 61 、 11 、 59 、 15 、 48 、 19

[26] [5] [77] [1] [61] [11] [59] [15] [48] [19]

[ 5 、 26 ] [ 1 、 77 ] [ 11 、 61 ] [ 15 、 59 ] [19 、 48 ]

[ 1 、 5 、 26 、 77 ] [11 、 15 、 59 、 61] [19 、 48]

[1 、 5 、 11 、 15 、 19 、 26 、 48 、 59 、 61 、 77]

32

Iterative

Page 33: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Ⅰ. Time complexity:

Best , Worst , Average case is O(nlogn)

Ⅱ . stable

Ⅲ. Require O(n) Space

33

)(nT1n 1, if

1 ,)2

(2 nifcnn

T

Iterative

Page 34: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

26 、 5 、 77 、 1 、 61 、 11 、 59 、 15 、 48、 19

[5 、 26] [77] [1 、 61] [11 、 59] [15] [19 、 48]

[5 、 26 、 77] [1 、 61] [11 、 15 、 59] [19 、 48]

[1 、 5 、 26 、 61 、 77] [11 、 15 、 19 、 48、 59]

[1 、 5 、 11 、 15 、 19 、 26 、 48 、 59 、 61、 77]

34

Recursive

Page 35: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Procedure rMergeSort (Var x:afile ; l, u:Integer ; Var p:Integer )

Begin

If l u Then p:=l≧ else Begin

mid := (1+u) div 2 ; rMergeSort(x,l,mid,q) ; //left

rMergeSort(x,mid+1,u,r) ; //right

ListMerge(x,q,r,p) ; //merge

End ; {of if}

End ; {of rMergeSort}

35

Recursive

Page 36: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Heap sort begins by building a heap out of the data set, and then removing the largest item and placing it at the end of the partially sorted array.

After removing the largest item, it reconstructs the heap, removes the largest remaining item, and places it in the next open position from the end of the partially sorted array. This is repeated until there are no items left in the heap and the sorted array is full.

36

Heap Sort

Page 37: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

37

Example: 35 、 21 、 37 、 15 、 52 、 15+ 、 5 、 40

Heap Sort

Page 38: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

38

Heap Sort

1. 2.

output 52 output 40

Page 39: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

3. 4.

output 37 output 35

39

Heap Sort

Page 40: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

5. 6.

output 21 output 15

40

Heap Sort

Page 41: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

7. 8.

output 15+ output 5

41

Heap Sort

Page 42: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Ⅰ. Time complexity: Best, Average, Worst case is O(nlogn)

Ⅱ. Unstable method

Ⅲ. Space complexity: O(1)

42

Heap Sort

Page 43: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

• LSD radix sort(1)If r is base -> r buckets(2)If m digit -> m steps

43

Radix Sort

Page 44: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

179 、 208 、 306 、 93 、 859 、 984 、 55 、 9 、 271 、 33 Radix sort

Pass 1.

(Single-digit)

Merge:271 、 93 、 33 、 984 、 55 、 306 、 208 、 179 、 859 、 9

44

0 1 2 3 4 5 6 7 8 9

9

33 859

271 93 984 55 306 208 179

Radix Sort

Page 45: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Pass 2. (tens'digit)

Merge:306 、 208 、 9 、 33 、 55 、 859 、 271 、 179 、 984 、 93

Pass 3. (hundreds'digit)

Merge:9 、 33 、 55 、 93 、 179 、 208 、 271 、 306 、 859 、 984

45

0 1 2 3 4 5 6 7 8 9

93

55

33 271

9 179 208 306 859 984

0 1 2 3 4 5 6 7 8 9

9

208 859 179

306 33 55 271 984 93

Radix Sort

Page 46: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Ⅰ. Time complexity :

d is the largest number of keys, n=data, r = base

Ⅱ. Space complexity :

→ 〝 Bucket size 〞 = r * bucket*n

Ⅲ. Stable

46

))(( rndO

)( nrO

Radix Sort

Page 47: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

47

Summary

Page 48: NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 7 SEARCH AND SORT 1.

NCUE CSIE Wireless Communications and Networking Laboratory

Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed〝 Fundamentals of Data Structures in C 〞 , W. H. Freeman & Co Ltd, 1992.

Ellis Horowitz, Sartaj Sahni, and Dinesh Mehta〝 Fundamentals of Data Structures in C++ 〞 Silicon Pr, 2006

Richard F.Gilberg, Behrouz A. Forouzan, 〝 Data Structures: A Pseudocode Approach with C 〞 , SBaker & Taylor Books, 2004

Fred Buckley, and Marty Lewinter 〝 A Friendly Introduction to Graph Theory 〞 Prentice Hall, 2002

〝資料結構 - 使用 C 語言〞蘇維雅譯,松崗, 2004 〝資料結構 - 使用 C 語言 〞 蔡明志編著,全華, 2004 〝資料結構 ( 含精選試題 ) 〞洪逸編著,鼎茂, 2005

48

Reference