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.

Post on 21-Dec-2015

216 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

Transcript

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 more than 4,000 movies– how can I find “The Incredibles”

Sorting Integers

How to sort the integers in this array?

28

18

7

4

10

4

7

10

18

28

Elementary Sorting Algorithms

• Selection Sort

• Insertion Sort

• Bubble Sort

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

– …

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

Selection Sort: Pass One

UNSORTED

28

18

7

4

10

Selection Sort: End Pass One

UNSORTED

4

18

7

28

10

SORTED

Selection Sort: End Pass Two

UNSORTED

4

7

18

28

10

SORTED

Selection Sort: Pass Three

UNSORTED

4

7

10

28

18

SORTED

Selection Sort: End Pass Three

4

7

10

18

28

SORTED

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.

• … …

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

Insertion Sort: Pass One

UNSORTED

28

18

7

4

10

SORTED

Insertion Sort: Pass Two

18

28

7

4

10

SORTEDU

NSORTED

Insertion Sort: Pass Three

7

18

28

4

10

SORTEDU

NSORTED

Insertion Sort: Pass Four

4

7

18

28

10

SORTED

UNSORTED

Insertion Sort: Pass Five

4

7

10

18

28

SORTED

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.

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

Bubble Sort: Pass One

UNSORTED

28

18

7

4

10

Bubble Sort: Pass One

UNSORTED

28

18

7

4

10

Bubble Sort: Pass One

UNSORTED

28

18

4

7

10

Bubble Sort: Pass One

UNSORTED

28

4

18

7

10

Bubble Sort: End Pass One

UNSORTED

4

28

18

7

10

SORTED

Bubble Sort: Pass Two

UNSORTED

4

28

18

7

10

SORTED

Bubble Sort: Pass Two

UNSORTED

4

28

18

7

10

SORTED

Bubble Sort: Pass Two

UNSORTED

4

28

7

18

10

SORTED

Bubble Sort: End Pass Two

UNSORTED

4

7

28

18

10

SORTED

Bubble Sort: Pass Three

UNSORTED

4

7

28

18

10

SORTED

Bubble Sort: Pass Three

4

7

28

10

18

SORTED

UNSORTED

Bubble Sort: End Pass Three

4

7

10

28

18

SORTEDU

NSORTED

Bubble Sort: Pass Four

4

7

10

28

18

SORTEDU

NSORTED

Bubble Sort: End Pass Four

4

7

10

18

28

SORTED

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

A Pascal Program Incorporating a Bubble Sort

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

top related