By saad malik
Download
1 / 20

Introduction to Sorting - PowerPoint PPT Presentation


  • 173 Views
  • Uploaded on

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)

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Introduction to Sorting' - hayley


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
By saad malik

By Saad Malik

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 }

What is Sorting?


Why sort and examples

Consider according to specified criterion.:

Sorting Books in Library (Dewey system)

Sorting Individuals by Height (Feet and Inches)

Sorting Movies in Blockbuster (Alphabetical)

Sorting Numbers (Sequential)

Why Sort and Examples


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

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

Types of Sorting Algorithms

  • Bubble Sort

  • Selection Sort

  • Insertion Sort

  • Merge Sort

  • Shell Sort

  • Heap Sort

  • Quick Sort

  • Radix Sort

  • Swap Sort


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.

Review of Complexity


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

Complexity (cont.)


O n n n
O(n), internal factors such as the time needed to run an algorithm. Ω(n), & Θ(n)

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


Common big oh s

Time complexity internal factors such as the time needed to run an algorithm.

Example

constant

O(1)

Adding to the front of a linked list

N

log

O(log

)

Finding an entry in a sorted array

N

linear

O(

)

Finding an entry in an unsorted array

N

N

n-log-n

O(

log

)

Sorting n items by ‘divide-and-conquer’

2

N

quadratic

O(

)

Shortest path between two nodes in a graph

3

N

cubic

O(

)

Simultaneous linear equations

1

1

5

5

8

8

(Binary) Finding 8:

(Linear) Finding 8:

9

9

21

21

22

22

50

50

Common Big-Oh’s

Front

Initial:

1

0

Final:

6

3

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


Big oh to primary sorts
Big-Oh to Primary Sorts internal factors such as the time needed to run an algorithm.

  • Bubble Sort = n²

  • Selection Sort = n²

  • Insertion Sort = n²

  • Merge Sort = n log(n)

  • Quick Sort = n log(n)


Time efficiency
Time Efficiency internal factors such as the time needed to run an algorithm.

  • 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)


Time efficiency improvements
Time Efficiency Improvements internal factors such as the time needed to run an algorithm.

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


Stable sort algorithms

Ann internal factors such as the time needed to run an algorithm.

98

Ann

98

Bob

90

Joe

98

Dan

75

Bob

90

Joe

98

Sam

90

Pat

86

Pat

86

Sam

90

Zöe

86

Zöe

86

Dan

75

original array

stably sorted

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

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


Unstable sort algorithms

Ann internal factors such as the time needed to run an algorithm.

98

Joe

98

Bob

90

Ann

98

Dan

75

Bob

90

Joe

98

Sam

90

Pat

86

Zöe

86

Sam

90

Pat

86

Zöe

86

Dan

75

original array

unstably sorted

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

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


Selection sorting
Selection Sorting internal factors such as the time needed to run an algorithm.

Step:

  • 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

20

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


Pseudo code for insertion sorting
Pseudo-code for Insertion Sorting internal factors such as the time needed to run an algorithm.

  • 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


Insert action i 1
Insert Action: i=1 internal factors such as the time needed to run an algorithm.

temp

8

20

8

5

10

7

i = 1, first iteration

8

20

20

5

10

7

---

8

20

5

10

7

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


Insert action i 2
Insert Action: i=2 internal factors such as the time needed to run an algorithm.

temp

5

8

20

5

10

7

i = 2, second iteration

5

8

20

20

10

7

5

8

8

20

10

7

---

5

8

20

10

7

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


Insert action i 3
Insert Action: i=3 internal factors such as the time needed to run an algorithm.

temp

10

5

8

20

10

7

i = 3, third iteration

10

5

8

20

20

7

---

5

8

10

20

7

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


Insert action i 4
Insert Action: i=4 internal factors such as the time needed to run an algorithm.

temp

7

5

8

10

20

7

i = 4, forth iteration

7

5

8

10

20

20

7

5

8

10

10

20

7

5

8

8

10

20

---

5

7

8

10

20

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


Sorting webpage
Sorting Webpage internal factors such as the time needed to run an algorithm.

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