170 likes | 222 Views
Sorting Algorithm. Zhen Jiang West Chester University zjiang@wcupa.edu. Outline. Introduction to sorting Selection sorting Bubble sorting Insertion sorting Shell sorting Heap sorting Merge sorting Quick sorting. For instance, 100 print jobs, # of pages is random
E N D
Sorting Algorithm Zhen Jiang West Chester University zjiang@wcupa.edu
Outline • Introduction to sorting • Selection sorting • Bubble sorting • Insertion sorting • Shell sorting • Heap sorting • Merge sorting • Quick sorting
For instance, • 100 print jobs, # of pages is random • Three queues (1-9, 10-49, and 50-100 pages) • 3 printers (each prints out 1 page per second) • Pick a job from queue 1 first • If none, pick a job from queue 2, and then 3. • Each job arrived at the beginning and be cashed into the queue according to its length (page #). Its waiting time is counted from the beginning until when it is served by a printer. • Develop a program to simulate this process and print out the total waiting time of all jobs.
For example 3, 2, 4, 5, 4, 2, 4. • Waiting 0, 0, 0, 2, 3, 4, 6. Total = 15 seconds.
Smallest first of 3, 2, 4, 5, 4, 2, 4? • Waiting 0, 0, 2, 6, 2, 0, 3. Total = 13 seconds. • Sorting is needed!
Selection sort • https://www.youtube.com/watch?v=cqh8nQwuKNE • See SelectionSort.java in project materials • Due to the unchecked call to compareTo, there is warning message in the compiling. Please ignore it.
Bubble sort • https://www.youtube.com/watch?v=F13_wsHDIG4 • See SelectionSort.java in project materials
Insertion sort, p272 • https://www.youtube.com/watch?v=lCDZ0IprFw4 • https://www.youtube.com/watch?v=eTvQIbB-AuE • See InsertionSort.java in project materials
Shell Sort, p274 • https://www.youtube.com/watch?v=ddeLSDsYVp8 • See ShellSort.java in the project materials
Heap sorting, p278 • https://www.youtube.com/watch?v=LbB357_RwlY • Sorting_out
Mergesort, p282 • https://www.youtube.com/watch?v=iMT7gTPpaqw • https://www.youtube.com/watch?v=qdv3i6X0PiQ • See MergeSort.java in the project materials
Quicksort, p288 • https://www.youtube.com/watch?v=B4URnLNITgw • https://www.youtube.com/watch?v=ZHVk2blR45Q&t=13s • See QuickSort.java in the project materials
Summary • What is the target to select in selection sort? • What is that bubble in the bubble sort? • What is to insert and where to insert in the insertion sort? • Why the shell sort is better than the insertion sort? • When the heap sort can perform over than quick and merge sorts? • Why merge sort has a performance of O(n2)? • Why quick sort is the quickest and how to ensure this?
Reference answer • The i^th min/max value in i^th iteration • The larger value, may be replaced in the middle of process by an even larger value • Current value in sort and its location in the sorted values. • The data preparation in a bipartite graph, which has a less cost than the following insertion sort in the 2nd phase • When the partial sorting results are needed, e.g., top 3 • Total log n levels + n new location at each level • The use of pivot