Professional Documents
Culture Documents
By Saad Malik
What is Sorting?
Sorting: an operation that segregates items into groups according to specified criterion.
A={3162134590}
A={0112334569}
Sorting Books in Library (Dewey system) Sorting Individuals by Height (Feet and Inches) Sorting Movies in Blockbuster (Alphabetical) Sorting Numbers (Sequential)
Review of Complexity
Most of the primary sorting algorithms run on different space and time complexity.
Time Complexity is defined to be the time the computer takes to run a program (or algorithm in our case).
Space complexity is defined to be the amount of memory the computer needs to run a program.
Complexity (cont.)
Complexity in general, measures the algorithms efficiency in internal factors such as the time needed to run an algorithm. External Factors (not related to complexity): Size of the input of the algorithm Speed of the Computer Quality of the Compiler
An algorithm or function T(n) is (f(n)) whenever T(n)'s rate of growth is greater than or equal to f(n)'s rate.
An algorithm or function T(n) is (f(n)) if and only if the rate of growth of T(n) is equal to f(n).
Common Big-Ohs
Time complexity constant O(1) O(log N) log linear O(N ) O(N log N) n-log-n quadratic O(N 2) cubic O(N 3) Example Adding to the front of a linked list Finding an entry in a sorted array Finding an entry in an unsorted array Sorting n items by divide-and-conquer Shortest path between two nodes in a graph Simultaneous linear equations
1 5 8 9 21 22 50
Initial: 1 0 Final: 6 3
http://www.cs.sjsu.edu/faculty/lee/cs146/23FL3Complexity.ppt
= n = n = n = n log(n) = n log(n)
Time Efficiency
How do we improve the time efficiency of a program?
The 90/10 Rule 90% of the execution time of a program is spent in executing 10% of the code So, how do we locate the critical 10%? software metrics tools global counters to locate bottlenecks (loop executions, function calls)
A stable sort keeps equal elements in the same order This may matter when you are sorting data according to some characteristic Example: sorting students by test scores
98
Dan 75
Joe Pat
Bob 90
Sam 90 Pat Ze
98 86
86 86
Sam 90 Ze
86
Dan 75
An unstable sort may or may not keep equal elements in the same order Stability is usually not important, but sometimes it is important
Ann 98
Bob 90 Dan 75 Joe Pat
Joe
98
98 86
86 86
Sam 90 Ze
86
Dan 75
original array
unstably sorted
www.cis.upenn.edu/~matuszek/cit594-2002/ Slides/searching.ppt
Selection Sorting
Step: 1. select the smallest element among data[i]~ data[data.length-1]; 2. swap it with data[i]; 20 8 5 10 7 3. if not finishing, repeat 1&2
5 5 8 20 10 7 7 20 10 8
5 7 8 10 20
rio.ecs.umass.edu/ece242/slides/lect-sorting.ppt 5 7 8 10 20
temp = data[i] shift those elements data[j] which greater than temp to right by one position place temp in its proper position
rio.ecs.umass.edu/ece242/slides/lect-sorting.ppt
20 20 5 10 7
---
8 20 5 10 7
rio.ecs.umass.edu/ece242/slides/lect-sorting.ppt
rio.ecs.umass.edu/ece242/slides/lect-sorting.ppt
10
---
5 8 20 20 7
5 8 10 20 7
rio.ecs.umass.edu/ece242/slides/lect-sorting.ppt
7
7 7
5 8 10 20 20
5 5 8 10 10 20 8 8 10 20
---
5 7 8 10 20
rio.ecs.umass.edu/ece242/slides/lect-sorting.ppt
Sorting Webpage
http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html