Professional Documents
Culture Documents
Lesson 9:
Sorting
CIS 068
Overview
Algorithmic Description and Analysis of
Selection Sort
Bubble Sort
Insertion Sort
Merge Sort
Quick Sort
CIS 068
10 36 8 35 1
10 36 1 35 8
1 8 10 35 36
Sorting is crucial to databases, databases are crucial to data-management, data-management is crucial to economy, economy is ... sorting seems to be pretty important ! The question is WHAT (name or age ?) and HOW to sort. This lesson will answer the latter one.
CIS 068
Sorting
Quadratic Algorithms
CIS 068
Quadratic Algorithms
Selection Sort
CIS 068
CIS 068
last = n-1
Algorithm:
For i=0 .. last -1 find smallest element M in subarray i .. last if M != element at i: swap elements Next i
( this is for BASIC-freaks !)
CIS 068
n * (n-1)/2 =
(n - n )/2 O(n)
O(n)
Overall (worst case) O(n) + O(n) = O(n) (quadratic sort)
CIS 068
Quadratic Algorithms
Bubble Sort
CIS 068
CIS 068
CIS 068
CIS 068
Quadratic Algorithms
Insertion Sort
CIS 068
CIS 068
4 passes
Pass 3
CIS 068
1 12 39 3 42
1 < value
= value
1 12 39 3 42
1
value
12 39 42
1 3 12 39 42
CIS 068
CIS 068
Contra
If array is presorted If array is in total disorder If array is in total disorder
Sorting
n*Log(n) Algorithms
CIS 068
n Log(n) Algorithms
Merge Sort
CIS 068
9 split 9 9 9 9 12 12 12 9 merge
12 19 16 12 19 16 19 16 19 16
1 1 1 1 1 1
25 25 25
4 4
3 3 4 4 3 4 25
CIS 068
3 3 4
25 25 3
16 19 12 16 19
12 16 19 25
CIS 068
CIS 068
Solution: dont pass leftArray and rightArray, but only indices defining the bounds
CIS 068
n Log(n) Algorithms
Quick Sort
CIS 068
pivot element
9 3 3 3 3
12 12 12 1 1
8 8 8 8 8 8
16 16 16 16 16
1 1 1 1
25 10 25 10 25 10
3 3
25 10 12 25 10 3 3
16 25 10
CIS 068
Comparison to Merge Sort: Comparable best case performance No extra memory needed
CIS 068
Review of Algorithms
Selection Sort An algorithm which orders items by repeatedly looking through remaining items to find the least one and moving it to a final location
Bubble Sort
Sort by comparing each adjacent pair of items in a list in turn, swapping the items if necessary, and repeating the pass through the list until no swaps are done
Insertion Sort
Sort by repeatedly taking the next item and inserting it into the final data structure in its proper order with respect to items already inserted.
Merge Sort
An algorithm which splits the items to be sorted into two groups, recursively sorts each group, and merges them into a final, sorted sequence
Quick Sort
An in-place sort algorithm that uses the divide and conquer paradigm. It picks an element from the array (the pivot), partitions the remaining elements into those greater than and less than this pivot, and recursively sorts the partitions.
CIS 068
Review
There are thousands of different sorting algorithms out there Some of them (the most important ones) were presented Later we will meet another sorting algorithm using trees
lots of images of these slides were taken from the textbook, for further details read there (Software Design & Data Structures in Java by Elliot B. Koffman + Paul A. T. Wolfgang) !
CIS 068