Top Banner
Algorithms for Sorting Things
39

Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Algorithms for Sorting Things

Page 2: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Why do we need to sort things?

• Internal Telephone Directory– sorted by department then by name

• My local video store holds more than 4,000 movies– how can I find “The Incredibles”

Page 3: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Sorting Integers

How to sort the integers in this array?

28

18

7

4

10

4

7

10

18

28

Page 4: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Elementary Sorting Algorithms

• Selection Sort

• Insertion Sort

• Bubble Sort

Page 5: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Selection Sort

• Main idea:– find the smallest element

– put it in the first position

– find the next smallest element

– put it in the second position

– …

Page 6: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

The algorithm splits the array into two parts: already sorted, and not yet sorted.

On each pass, the algorithm locates the smallest of the unsorted elements, and moves into the correct position

Straight Selection Sort

28

18

7

4

10

Page 7: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Selection Sort: Pass One

UNSORTED

28

18

7

4

10

Page 8: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Selection Sort: End Pass One

UNSORTED

4

18

7

28

10

SORTED

Page 9: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Selection Sort: End Pass Two

UNSORTED

4

7

18

28

10

SORTED

Page 10: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Selection Sort: Pass Three

UNSORTED

4

7

10

28

18

SORTED

Page 11: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Selection Sort: End Pass Three

4

7

10

18

28

SORTED

Page 12: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Insertion Sort

Main Idea:

• Starts by considering the first two elements of the array data, if out of order, swap them

• Consider the third element, insert it into the proper position among the first three elements.

• Consider the forth element, insert it into the proper position among the first four elements.

• … …

Page 13: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

One by one, each as yet unsorted array element is inserted into its proper place with respect to the already sorted elements.

On each pass, this causes the number of already sorted elements to increase by one.

Insertion Sort

28

18

7

4

10

Page 14: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Insertion Sort: Pass One

UNSORTED

28

18

7

4

10

SORTED

Page 15: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Insertion Sort: Pass Two

18

28

7

4

10

SORTEDU

NSORTED

Page 16: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Insertion Sort: Pass Three

7

18

28

4

10

SORTEDU

NSORTED

Page 17: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Insertion Sort: Pass Four

4

7

18

28

10

SORTED

UNSORTED

Page 18: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Insertion Sort: Pass Five

4

7

10

18

28

SORTED

Page 19: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Bubble Sort

• The bubble sort works by comparing each item in the list with the item next to it, and swapping them if required.

• The algorithm repeats this process until it makes a pass all the way through the list without swapping any items (in other words, all items are in the correct order).

• This causes larger values to "bubble" to the end of the list while smaller values "sink" towards the beginning of the list.

Page 20: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Compares neighboring pairs of array elements, starting with the last array element, and swaps neighbors whenever they are not in correct order.

On each pass, this causes the smallest element to “bubble up” to its correct place in the array.

Bubble Sort

28

18

7

4

10

Page 21: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Bubble Sort: Pass One

UNSORTED

28

18

7

4

10

Page 22: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Bubble Sort: Pass One

UNSORTED

28

18

7

4

10

Page 23: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Bubble Sort: Pass One

UNSORTED

28

18

4

7

10

Page 24: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Bubble Sort: Pass One

UNSORTED

28

4

18

7

10

Page 25: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Bubble Sort: End Pass One

UNSORTED

4

28

18

7

10

SORTED

Page 26: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Bubble Sort: Pass Two

UNSORTED

4

28

18

7

10

SORTED

Page 27: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Bubble Sort: Pass Two

UNSORTED

4

28

18

7

10

SORTED

Page 28: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Bubble Sort: Pass Two

UNSORTED

4

28

7

18

10

SORTED

Page 29: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Bubble Sort: End Pass Two

UNSORTED

4

7

28

18

10

SORTED

Page 30: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Bubble Sort: Pass Three

UNSORTED

4

7

28

18

10

SORTED

Page 31: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Bubble Sort: Pass Three

4

7

28

10

18

SORTED

UNSORTED

Page 32: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Bubble Sort: End Pass Three

4

7

10

28

18

SORTEDU

NSORTED

Page 33: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Bubble Sort: Pass Four

4

7

10

28

18

SORTEDU

NSORTED

Page 34: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Bubble Sort: End Pass Four

4

7

10

18

28

SORTED

Page 35: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

C o m p ar e c u r r en t a r r ayitem to n ex t o n e

Ar e th es e tw oar r ay item s in th e

c o r r ec t o r d er ?

S w ap th e tw o ar r ayitem s ar o u n d

Ar e w e a t th e en do f th e a r r ay ?

Yes

N o

N o Yes D id w e n eed tom ak e an yc h an g es ?

Yes

N o

Ar r ay is S o r ted

S tar t

M o v e o n ton ex t a r r ay item M o v e to f ir s t

item in a r r ay

Page 36: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

A Pascal Program Incorporating a Bubble Sort

Page 37: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.
Page 38: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.
Page 39: Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.

Research Topic

• The bubble sort is rather slow because it operates only locally each item moves only one position at a time

• The literature on algorithms discusses a number of alternatives that are more efficient but more complex

• When you fully understand the logic of the bubble sort take a look at these other algorithms– Merge sorts and Quick Sorts