20
Introduction to Sorting By Saad Malik

Introduction to Sorting

  • Upload
    hayley

  • View
    47

  • Download
    2

Embed Size (px)

DESCRIPTION

By Saad Malik. Introduction to Sorting. Sorting: an operation that segregates items into groups according to specified criterion. A = { 3 1 6 2 1 3 4 5 9 0 } A = { 0 1 1 2 3 3 4 5 6 9 }. What is Sorting?. Consider : Sorting Books in Library (Dewey system) - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction to Sorting

Introduction to Sorting

By Saad Malik

Page 2: Introduction to Sorting

What is Sorting?

Sorting: an operation that segregates items into groups according to specified criterion.

A = { 3 1 6 2 1 3 4 5 9 0 }

A = { 0 1 1 2 3 3 4 5 6 9 }

Page 3: Introduction to Sorting

Why Sort and Examples

Consider:

● Sorting Books in Library (Dewey system) ● Sorting Individuals by Height (Feet and Inches)● Sorting Movies in Blockbuster (Alphabetical)● Sorting Numbers (Sequential)

Page 4: Introduction to Sorting

Types of Sorting Algorithms

There are many, many different types of sorting algorithms, but the primary ones are:

● Bubble Sort● Selection Sort● Insertion Sort● Merge Sort● Shell Sort ● Heap Sort

● Quick Sort● Radix Sort● Swap Sort

Page 5: Introduction to Sorting

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.

Page 6: Introduction to Sorting

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

Page 7: Introduction to Sorting

●An algorithm or function T(n) is O(f(n)) whenever T(n)'s rate of growth is less than or equal to f(n)'s rate.

●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).

O(n), Ω(n), & Θ(n)

Page 8: Introduction to Sorting

10

Common Big-Oh’s

Time complexity Example

O(1) constant Adding to the front of a linked listO(log N) log Finding an entry in a sorted arrayO(N) linear Finding an entry in an unsorted arrayO(N log N) n-log-n Sorting n items by ‘divide-and-conquer’O(N2) quadratic Shortest path between two nodes in a graphO(N3) cubic Simultaneous linear equations

(Binary) Finding 8:

9

50

22

21

8

5

1

(Linear) Finding 8:

9

50

22

21

8

5

1Front

Initial:

Final: 63

http://www.cs.sjsu.edu/faculty/lee/cs146/23FL3Complexity.ppt

Page 9: Introduction to Sorting

Big-Oh to Primary Sorts

● Bubble Sort = n²● Selection Sort = n²● Insertion Sort = n²● Merge Sort = n log(n)●Quick Sort = n log(n)

Page 10: Introduction to Sorting

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 inexecuting 10% of the code

• So, how do we locate the critical 10%?

• software metrics tools• global counters to locate bottlenecks (loop executions, function calls)

Page 11: Introduction to Sorting

Time Efficiency Improvements

Possibilities (some better than others!)

• Move code out of loops that does not belong there (just good programming!)

• Remove any unnecessary I/O operations (I/O operations are expensive time-wise)

• Code so that the compiled code is more efficient

Moral - Choose the most appropriate algorithm(s) BEFORE program implementation

Page 12: Introduction to Sorting

Stable sort algorithms

● 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

Bob

Ann

Joe

Zöe

Dan

Pat

Sam

90

98

98

86

75

86

90

original array

Bob

Ann

Joe

Zöe

Dan

Pat

Sam

90

98

98

86

75

86

90

stably sorted

www.cis.upenn.edu/~matuszek/cit594-2002/ Slides/searching.ppt

Page 13: Introduction to Sorting

Unstable sort algorithms

● An unstable sort may or may not keep equal elements in the same order

● Stability is usually not important, but sometimes it is important

Bob

Ann

Joe

Zöe

Dan

Pat

Sam

90

98

98

86

75

86

90

original array

Bob

Ann

Joe

Zöe

Dan

Pat

Sam

90

98

98

86

75

86

90

unstably sorted

www.cis.upenn.edu/~matuszek/cit594-2002/ Slides/searching.ppt

Page 14: Introduction to Sorting

Selection SortingStep:● 1. select the smallest element ● among data[i]~ data[data.length-1];● 2. swap it with data[i];● 3. if not finishing, repeat 1&2

20 8 5 10 7

5 8 20 10 7

5 7 20 10 8

5 7 8 10 20

5 7 8 10 20rio.ecs.umass.edu/ece242/slides/lect-sorting.ppt

Page 15: Introduction to Sorting

Pseudo-code for Insertion Sorting

● Place ith item in proper position:

– 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

Page 16: Introduction to Sorting

Insert Action: i=1

20 8 5 10 7

20 20 5 10 7

8

temp

8

i = 1, first iteration

8 20 5 10 7---

rio.ecs.umass.edu/ece242/slides/lect-sorting.ppt

Page 17: Introduction to Sorting

Insert Action: i=2

8 20 5 10 7

8 20 20 10 7

temp

5

5

i = 2, second iteration

8 8 20 10 75

5 8 20 10 7---

rio.ecs.umass.edu/ece242/slides/lect-sorting.ppt

Page 18: Introduction to Sorting

Insert Action: i=3

5 8 20 10 7

5 8 20 20 7

temp

10

10

i = 3, third iteration

5 8 10 20 7---

rio.ecs.umass.edu/ece242/slides/lect-sorting.ppt

Page 19: Introduction to Sorting

Insert Action: i=4

5 8 10 20 7

5 8 10 20 20

5 8 10 10 20

5 8 8 10 20

7

temp

7

7

7

i = 4, forth iteration

5 7 8 10 20---

rio.ecs.umass.edu/ece242/slides/lect-sorting.ppt

Page 20: Introduction to Sorting

Sorting Webpage

http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html