CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms - PowerPoint PPT Presentation

1 / 46
About This Presentation
Title:

CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms

Description:

CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu_at_spu.ac.th – PowerPoint PPT presentation

Number of Views:130
Avg rating:3.0/5.0
Slides: 47
Provided by: ICT70
Category:

less

Transcript and Presenter's Notes

Title: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms


1
CSE 221/ICT221 Analysis and Design of
AlgorithmsLecture 05Analysis of time
Complexity of Sorting Algorithms
  • Dr.Surasak Mungsing
  • E-mail Surasak.mu_at_spu.ac.th

2
Sorting Algorithms
  • Bin Sort
  • Radix Sort
  • Insertion Sort
  • Shell Sort
  • Selection Sort
  • Heap Sort
  • Bubble Sort
  • Quick Sort
  • Merge Sort

CSE221/ICT221 Analysis and Design of Algorithms
3
CSE221/ICT221 Analysis and Design of Algorithms
4
Bin Sort
(a) Input chain
(b) Nodes in bins
(c) Sorted chain
CSE221/ICT221 Analysis and Design of Algorithms
5
Radix Sort with r10 and d3
6
Insertion Sort Concept
7
CSE221/ICT221 Analysis and Design of Algorithms
8
Shell Sort Algorithm
CSE221/ICT221 Analysis and Design of Algorithms
9
Shell Sort Algorithm
CSE221/ICT221 Analysis and Design of Algorithms
10
Shell Sort Algorithm
CSE221/ICT221 Analysis and Design of Algorithms
11
CSE221/ICT221 Analysis and Design of Algorithms
12
Shell Sort Demohttp//e-learning.mfu.ac.th/mflu/
1302251/demo/Chap03/ShellSort/ShellSort.html
13
Selection Sort Concept
CSE221/ICT221 Analysis and Design of Algorithms
14
(No Transcript)
15
Heap Sort Algorithm
CSE221/ICT221 Analysis and Design of Algorithms
16
CSE221/ICT221 Analysis and Design of Algorithms
17
Bubble Sort Concept
18
(No Transcript)
19
Quick sort
The fastest known sorting algorithm in practice.
CSE221/ICT221 Analysis and Design of Algorithms
20
Quick sort
CSE221/ICT221 Analysis and Design of Algorithms
21
Quick sort
22
Quick Sort Partitions
CSE221/ICT221 Analysis and Design of Algorithms
23
Quick sort
CSE221/ICT221 Analysis and Design of Algorithms
24
External Sort A simple merge
CSE221/ICT221 Analysis and Design of Algorithms
25
Merge Sort
CSE221/ICT221 Analysis and Design of Algorithms
26
Merge Sort
CSE221/ICT221 Analysis and Design of Algorithms
27
CSE221/ICT221 Analysis and Design of Algorithms
28
Sort Algorithm Analysis
29
Analysis of Insertion Sort
  • for (int i 1 i lt a.length i)
  • // insert ai into a0i-1
  • int t ai
  • int j
  • for (j i - 1 j gt 0 t lt aj j--)
  • aj 1 aj
  • aj 1 t

CSE221/ICT221 Analysis and Design of Algorithms
30
Complexity
  • Space/Memory
  • Time
  • Count a particular operation
  • Count number of steps
  • Asymptotic complexity

CSE221/ICT221 Analysis and Design of Algorithms
31
Comparison Count
  • for (int i 1 i lt a.length i)
  • // insert ai into a0i-1
  • int t ai
  • int j
  • for (j i - 1 j gt 0 t lt aj j--)
  • aj 1 aj
  • aj 1 t

CSE221/ICT221 Analysis and Design of Algorithms
32
Comparison Count
  • for (j i - 1 j gt 0 t lt aj j--)
  • aj 1 aj
  • How many comparisons are made?

CSE221/ICT221 Analysis and Design of Algorithms
33
Comparison Count
  • for (j i - 1 j gt 0 t lt aj j--)
  • aj 1 aj
  • number of compares depends on
  • as and t as well as on i

CSE221/ICT221 Analysis and Design of Algorithms
34
Comparison Count
  • Worst-case count maximum count
  • Best-case count minimum count
  • Average count

CSE221/ICT221 Analysis and Design of Algorithms
35
Worst-Case Comparison Count
  • for (j i - 1 j gt 0 t lt aj j--)
  • aj 1 aj

a 1, 2, 3, 4 and t 0 ? 4 compares a
1,2,3,,i and t 0 ? i compares
CSE221/ICT221 Analysis and Design of Algorithms
36
Worst-Case Comparison Count
  • for (int i 1 i lt n i)
  • for (j i - 1 j gt 0 t lt aj j--)
  • aj 1 aj

total compares 1 2 3 (n-1)

(n-1)n/2
O(n2)
CSE221/ICT221 Analysis and Design of Algorithms
37
Average-Case Comparison Count
  • for (int i 1 i lt n i)
  • for (j i - 1 j gt 0 t lt aj j--)
  • aj 1 aj

O(n2)
CSE221/ICT221 Analysis and Design of Algorithms
38
Analysis of Quick Sort
CSE221/ICT221 Analysis and Design of Algorithms
39
MainQuick SortRoutine
private static void quicksort( Comparable a,
int left, int right ) / 1/ if(
left CUTOFF lt right ) / 2/
Comparable pivot median3( a, left, right
) // Begin partitioning /
3/ int i left, j right - 1 / 4/
for( ) / 5/
while( a i .compareTo( pivot ) lt 0 )
/ 6/ while( a --j .compareTo(
pivot ) gt 0 ) / 7/ if( i lt j
) / 8/ swapReferences( a, i, j
) else / 9/
break /10/
swapReferences( a, i, right - 1 ) // Restore
pivot /11/ quicksort( a, left, i - 1
) // Sort small elements /12/
quicksort( a, i 1, right ) // Sort large
elements else // Do an
insertion sort on the subarray /13/
insertionSort( a, left, right )
CSE221/ICT221 Analysis and Design of Algorithms
40
Worst-Case Analysis
??????????????? run quick sort ???????????????????
????? recursive call 2 ????? linear time
???????????????? pivot ????????? basic quick sort
relation ??????? T(n) T(i) T(n-i-1) cn
?????? Worst- case ???? ?????? pivot
??????????????????? ????????????????? recursion
??? T(n) T(n-1) cn ngt1 T(n-1)
T(n-2)c(n-1) T(n-2) T(n-3)c(n-2)
T(2) T(1)c(2) ?????????????? T(n) T(1)
c
O(n2)
CSE221/ICT221 Analysis and Design of Algorithms
41
Best-Case Analysis
  • The pivot is in the middle T(n) 2 T(n/2) cn
  • Divide both sides by n


T(n) c n logn n
O(n log n)
CSE221/ICT221 Analysis and Design of Algorithms
42
Average-Case Analysis (1/4)
Total time T(N) T(i) T(N-i-1) c N ..(1)
..(2)
Therefore
..(3)
..(4)
..(5)
CSE221/ICT221 Analysis and Design of Algorithms
43
Average-Case Analysis (2/4)
..(6)
NT(N) (N-1) T(N-1) 2T(N-1) 2cN - c
(5) (4)
CSE221/ICT221 Analysis and Design of Algorithms
44
Average-Case Analysis (3/4)
Sun equations (8) to (11)
..(12)
CSE221/ICT221 Analysis and Design of Algorithms
45
Average-Case Analysis (4/4)
In summary, time complexity of Quick sort
algorithm for Average-Case is T(n) O(n log n)
CSE221/ICT221 Analysis and Design of Algorithms
46
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com