computer programming sorting and sorting algorithms 1

38
Computer Programming Sorting and Sorting Algorithms 1

Post on 21-Dec-2015

271 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Computer Programming Sorting and Sorting Algorithms 1

Computer Programming

Sorting and Sorting Algorithms

1

Page 2: Computer Programming Sorting and Sorting Algorithms 1

Introduction

• The problem:Given an array a[0], a[1], … a[n-1],reorder entries so thata[0] <= a[1] <= … <= a[n-1]

Before: 4 5 2 6 12 3 5 1

After: 1 2 3 4 5 5 6 12

An extremely common operation

2

Page 3: Computer Programming Sorting and Sorting Algorithms 1

The Structure of the data

• In practice, the values to be sorted are usually part of a collection of data called a record

• Each record contains a key, which is the value to be sorted, and the remainder of the record consists of satellite data

• We must usually move not only the key but the satellite data also

• If there is a large amount of satellite data we sometimes sort an array of pointers (or indices) to the records rather than the records themselves. Why?

3

Page 4: Computer Programming Sorting and Sorting Algorithms 1

Sorting

• Whether we sort individual numbers or large records that contain numbers is irrelevant to the method we use– Anything else is an implementation detail

• When focusing on the sorting problem we assume that the input consists only of numbers

• Translating an algorithm for sorting numbers into a program to sort records can be tricky but is an implementation consideration only

4

Page 5: Computer Programming Sorting and Sorting Algorithms 1

Selection Sort

• There are a number of ways of sorting a list of N numbers

• The first we will look at is called the Selection Sort algorithm

• The algorithm can be stated as follows : – Find the smallest element of A and swap it with

the element in the first position of A– Find the next smallest element in A and swap it

with the element in the second position of A– Continue until A is sorted

5

Page 6: Computer Programming Sorting and Sorting Algorithms 1

Selection Sort Algorithm

Selection-Sort(A)for i = 0 to length(A) –2

smallestplace = ifor j = i +1 to length(A) - 1

if A[j] < A[smallestplace] thensmallestplace = j

endifendforswap(A[i], A[smallestplace])

endforendalg

6

Page 7: Computer Programming Sorting and Sorting Algorithms 1

Example 1

• After the first iteration of the loop:

7

0 1 2 3 4 5

9 8 7 6 5 4

0 1 2 3 4 5

4 8 7 6 5 9

After the second iteration of the loop:

0 1 2 3 4 5

4 5 7 6 8 9

A =

A =

A =

Page 8: Computer Programming Sorting and Sorting Algorithms 1

Example 1

• After third iteration of the loop:

8

0 1 2 3 4 5

4 5 6 7 8 9

0 1 2 3 4 5

4 5 6 7 8 9

0 1 2 3 4 5

4 5 6 7 8 9

A =

A =

A =

After fourth iteration of the loop:

And finally the iteration of the loop:

Page 9: Computer Programming Sorting and Sorting Algorithms 1

Example 2

9

0 1 2 3 4

56 43 78 12 23A=

0 1 2 3 4

12 43 78 56 23

0 1 2 3 4

12 23 78 56 43

0 1 2 3 4

12 23 43 56 78

0 1 2 3 4

12 23 43 56 78

1

2

3

4

Page 10: Computer Programming Sorting and Sorting Algorithms 1

Discussion

• Selection sort is a “common sense” algorithm• It is efficient only for small amounts of data• We must loop through every unsorted

element each time• This double loop structure is inefficient for

large data sets• Also, the algorithm has no way of recognising

when the data set is sorted• This makes it inefficient for partially sorted

data

10

Page 11: Computer Programming Sorting and Sorting Algorithms 1

In Place Sorting

• The algorithm we looked at uses an in-place sort– That is, we have one array and seek to sort the

data within it• In-place sorting is commonly used• We can implement sorting algorithms using a

second array• That is, one input array holds the original data

and an output array holds the same data in sorted order

11

Page 12: Computer Programming Sorting and Sorting Algorithms 1

Bubble Sort

• Bubble sort works by comparing each adjacent pair of items in a list in turn, swapping the items if necessary, and repeating the pass through the list until no swaps are done.

• It is sometimes called sinking sort or exchange sort• It can work from either the first item moving larger

items towards the back of the list, or from the back moving smaller items towards the front

• Performance can be improved by modifying it to a Bi-directional bubble sort, i.e. One pass from front to back, the next from back to front

12

Page 13: Computer Programming Sorting and Sorting Algorithms 1

Bubble Sort

13

Bubble-Sort(A) for i = 0 to length(A)-2

for j = 0 to length(A) – i - 2 if A[j] > A[j+1] then swap(A[j], A[j+1])

endif endfor endforendalg

Page 14: Computer Programming Sorting and Sorting Algorithms 1

Bubble Sort Example

14

Go along the list, compare 2 consecutive elements A[j] and A[j+1]. Swap their value if A[j] > A[j+1].

The list is now sorted but the algorithm does not know that. It will continue looping until i= A(length)-2

Page 15: Computer Programming Sorting and Sorting Algorithms 1

Discussion

• This implementation starts from the front and moves the larger values towards the back

• Each pass through the array places at least one item in its correct position

• You always do N-1 passes through the array – even if it is already sorted!

• Why are there not N passes?

15

Page 16: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort

• Insertion sort, sometimes known as linear insertion sort, works the way many people sort a hand of playing cards

• We start with an empty left hand and the cards face down on the table

• We then remove one card at a time from the table and insert it into the correct position in the left hand

• To find the correct position for a card, we compare it with each of the cards already in the hand, from right to left, when we find the correct position, we insert the card

16

Page 17: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Algorithm

Insertion-Sort(A) for j = 1 to length[A] - 1

temp = A[j] i = j – 1 while i >= 0 and A[i] > temp

A[i+1] = A[i] i = i – 1

endwhile A[i+1] = temp

endforendalg

17

Page 18: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

18

A[0] A[1] A[2] A[3] A[4]

20 10 15 5 30sort (A, 5)

j=1

temp = 10i=0

i>=0 and A[i] > temp, so A[i+1] = A[i]

Page 19: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

19

A[0] A[1] A[2] A[3] A[4]

20 20 15 5 30sort (A, 5)

j=1

temp = 10i=0

Page 20: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

20

A[0] A[1] A[2] A[3] A[4]

20 20 15 5 30sort (A, 5)

j=1

temp = 10i=-1

i ! >= 0, so A[i+1] = temp

Page 21: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

21

A[0] A[1] A[2] A[3] A[4]

10 20 15 5 30sort (A, 5)

j=2

temp = 15i=1

Page 22: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

22

A[0] A[1] A[2] A[3] A[4]

10 20 15 5 30sort (A, 5)

j=2

temp = 15i=1

i>=0 and A[i] > temp, so A[i+1] = A[i]

Page 23: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

23

A[0] A[1] A[2] A[3] A[4]

10 20 20 5 30sort (A, 5)

j=2

temp = 15i=1

i>=0 and A[i] > temp, so A[i+1] = A[i]

Page 24: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

24

A[0] A[1] A[2] A[3] A[4]

10 20 20 5 30sort (A, 5)

j=2

temp = 15i=0

Page 25: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

25

A[0] A[1] A[2] A[3] A[4]

10 20 20 5 30sort (A, 5)

j=2

temp = 15i=0

A[i] !> temp, so A[i+1] = temp

Page 26: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

26

A[0] A[1] A[2] A[3] A[4]

10 15 20 5 30sort (A, 5)

j=2

temp = 15i=0

Page 27: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

27

A[0] A[1] A[2] A[3] A[4]

10 15 20 5 30sort (A, 5)

j=3

temp = 5

i=2

i>=0 and A[i] > temp, so A[i+1] = A[i]

Page 28: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

28

A[0] A[1] A[2] A[3] A[4]

10 15 20 20 30sort (A, 5)

j=3

temp = 5

i=2

Page 29: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

29

A[0] A[1] A[2] A[3] A[4]

10 15 20 20 30sort (A, 5)

j=3

temp = 5

i=1

i>=0 and A[i] > temp, so A[i+1] = A[i]

Page 30: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

30

A[0] A[1] A[2] A[3] A[4]

10 15 15 20 30sort (A, 5)

j=3

temp = 5

i=1

Page 31: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

31

A[0] A[1] A[2] A[3] A[4]

10 15 15 20 30sort (A, 5)

j=3

temp = 5

i=0

Page 32: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

32

A[0] A[1] A[2] A[3] A[4]

10 15 15 20 30sort (A, 5)

j=3

temp = 5

i=0

i>=0 and A[i] > temp, so A[i+1] = A[i]

Page 33: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

33

A[0] A[1] A[2] A[3] A[4]

10 10 15 20 30sort (A, 5)

j=3

temp = 5

i=0

Page 34: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

34

A[0] A[1] A[2] A[3] A[4]

10 10 15 20 30sort (A, 5)

j=3

temp = 5

i=0

Page 35: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

35

A[0] A[1] A[2] A[3] A[4]

10 10 15 20 30sort (A, 5)

j=3

temp = 5

i=-1

i ! >=0, so A[i+1] = temp

Page 36: Computer Programming Sorting and Sorting Algorithms 1

Insertion Sort Example

36

A[0] A[1] A[2] A[3] A[4]

5 10 15 20 30sort (A, 5)

j=3

temp = 5

i=-1

The list is now sorted but the algorithm does not know that. It will continue looping until j= A(length)-1

Page 37: Computer Programming Sorting and Sorting Algorithms 1

Discussion

• This implementation starts from the front and moves the larger values towards the back

• Each pass through the array places one item in its correct position within the sorted part of the array

• Again you always do N-1 passes through the area – even if it is already sorted!

37

Page 38: Computer Programming Sorting and Sorting Algorithms 1

Summary

• Sorting data is a very common operation. We usually sort on one key, with satellite data attached to the key

• Bubble Sort and Insertion Sort, along with Selection Sort sometimes known as the ‘simple’ sorts

• Bubble Sort at each run through places one item in its correct position

• Selection at each run through places one item in the correct position within the sorted part of the array

• Insertion Sort maintains a sorted list by inserting the next item into its correct position at each iteration

• These algorithms do not do well with partially sorted data

38