42
Consider an array of n values to be sorted into ascending order. Sorting

Consider an array of n values to be sorted into ascending order. Sorting

Embed Size (px)

Citation preview

Page 1: Consider an array of n values to be sorted into ascending order. Sorting

Consider an array of n values to be sorted into ascending order.

Sorting

Page 2: Consider an array of n values to be sorted into ascending order. Sorting

Involves repeated passes(scans) through the data in the array.

9.1 The Bubble Sort

Page 3: Consider an array of n values to be sorted into ascending order. Sorting

This involves comparing successive pairs of items

7 19 2 83

Page 4: Consider an array of n values to be sorted into ascending order. Sorting

For example, if the first is larger than the second, then they are interchanged, otherwise they’re left alone.

7 19 2 83

Page 5: Consider an array of n values to be sorted into ascending order. Sorting

This guarantees that after the first pass, the largest item is moved(“bubbled”) to the end of the array.

7 19 2 83

Page 6: Consider an array of n values to be sorted into ascending order. Sorting

After the second pass, the next largest item is placed in end position of the array but one, and so on .....

Page 7: Consider an array of n values to be sorted into ascending order. Sorting

After a maximum of n passes all the elements will have been bubbled into their correct places and the array will be sorted.

Example follows

Page 8: Consider an array of n values to be sorted into ascending order. Sorting

7 3 9 2 8 1[0] [1] [2] [3] [4] [5]

i = 0;j = i + 1;

0

i

1

j

i j

SWAP

Page 9: Consider an array of n values to be sorted into ascending order. Sorting

i++;j = i + 1;

0

i

1

j

3 7 9 2 8 1[0] [1] [2] [3] [4] [5]

i j

Page 10: Consider an array of n values to be sorted into ascending order. Sorting

i++;j = i + 1;

1

i

2

j

3 7 9 2 8 1[0] [1] [2] [3] [4] [5]

i j

Page 11: Consider an array of n values to be sorted into ascending order. Sorting

SWAP 2

i

3

j

3 7 9 2 8 1[0] [1] [2] [3] [4] [5]

i j

Page 12: Consider an array of n values to be sorted into ascending order. Sorting

2

i

3

j

3 7 2 9 8 1[0] [1] [2] [3] [4] [5]

i j

i++;j = i + 1;

Page 13: Consider an array of n values to be sorted into ascending order. Sorting

3

i

4

j

3 7 2 9 8 1[0] [1] [2] [3] [4] [5]

i j

SWAP

Page 14: Consider an array of n values to be sorted into ascending order. Sorting

3

i

4

j

3 7 2 8 9 1[0] [1] [2] [3] [4] [5]

i j

i++;j = i + 1;

Page 15: Consider an array of n values to be sorted into ascending order. Sorting

4

i

5

j

3 7 2 8 9 1[0] [1] [2] [3] [4] [5]

i j

SWAP

Page 16: Consider an array of n values to be sorted into ascending order. Sorting

4

i

5

j

3 7 2 8 1 9[0] [1] [2] [3] [4] [5]

i j

i = 0;j = i + 1;

1 Pass completed

Page 17: Consider an array of n values to be sorted into ascending order. Sorting

0

i

1

j

3 7 2 8 1 9[0] [1] [2] [3] [4] [5]

i j

i++ = 0;j = i + 1;

Start of 2nd pass

… and so on

Page 18: Consider an array of n values to be sorted into ascending order. Sorting

When does the program know when to stop?

“Test that no interchanges have occurred in a pass.”

Page 19: Consider an array of n values to be sorted into ascending order. Sorting

Bubble sort is not regarded as being efficient.

Page 20: Consider an array of n values to be sorted into ascending order. Sorting

Given n elements, n-1 comparisons are needed per pass [*general case].

* implementation dependent

If n passes are required(worst case) then, n(n - 1) comparisons are needed in all.

Page 21: Consider an array of n values to be sorted into ascending order. Sorting

... Cards are inserted into their correct positions as they are dealt to a player.

9.2 The Insertion Sort

Same way in which a deck of cards might be sorted.

Page 22: Consider an array of n values to be sorted into ascending order. Sorting

Insertion Sort

Page 23: Consider an array of n values to be sorted into ascending order. Sorting

This works by finding the smallest in the array and putting it into position [0].

9.3 The Selection Sort

Page 24: Consider an array of n values to be sorted into ascending order. Sorting

The “remaining array” is then sorted in a similar way.

More efficient way of sorting than bubble sort.

Example follows

Page 25: Consider an array of n values to be sorted into ascending order. Sorting

3 2 1[0] [1] [2]

0

i

0

min

1

ji = 0;min = i;j = i + 1;

Page 26: Consider an array of n values to be sorted into ascending order. Sorting

0

i

0

min

1

jif (a[j] < a[min]) min = j;

3 2 1[0] [1] [2]

i min j

Page 27: Consider an array of n values to be sorted into ascending order. Sorting

j++; 0

i

1

min

1

j

3 2 1[0] [1] [2]

i min j

Page 28: Consider an array of n values to be sorted into ascending order. Sorting

0

i

1

min

2

j

3 2 1[0] [1] [2]

i min j

if (a[j] < a[min]) min = j;

Page 29: Consider an array of n values to be sorted into ascending order. Sorting

0

i

2

min

2

j

3 2 1[0] [1] [2]

i min j

j++;

Page 30: Consider an array of n values to be sorted into ascending order. Sorting

0

i

2

min

3

j

3 2 1[0] [1] [2]

i min j

if (i != min) swap(a[i],a[min])

SWAP

Page 31: Consider an array of n values to be sorted into ascending order. Sorting

0

i

2

min

3

j

1 2 3[0] [1] [2]

i min j

i++;min = i;j = i + 1;

Page 32: Consider an array of n values to be sorted into ascending order. Sorting

1

i

1

min

2

j

1 2 3[0] [1] [2]

i min j

if (a[j] < a[min]) min = j;

Sort the “remaining array” in similar way

Page 33: Consider an array of n values to be sorted into ascending order. Sorting

1

i

1

min

2

j

1 2 3[0] [1] [2]

i min j

j++;

Page 34: Consider an array of n values to be sorted into ascending order. Sorting

1

i

1

min

3

j

1 2 3[0] [1] [2]

i min j

if (i != min) swap(a[i],a[min])i.e.false

Page 35: Consider an array of n values to be sorted into ascending order. Sorting

1

i

1

min

3

j

1 2 3[0] [1] [2]

i min j

i++;min = i;j = i + 1;

Page 36: Consider an array of n values to be sorted into ascending order. Sorting

1 2 3[0] [1] [2]

i min j

TERMINATE

2

i

2

min

3

ji < (a.length-1)i.e false

Page 37: Consider an array of n values to be sorted into ascending order. Sorting

This is known as a Partition Exchange Sort and was invented by C.A.Hoare in 1962

9.4 The Quick Sort Method

In general faster than the bubble and selection sorts.

Page 38: Consider an array of n values to be sorted into ascending order. Sorting

This involves partitioning a set of data into subsets with respect to:

• an ordering relation

• particular item - a “pivot”

Page 39: Consider an array of n values to be sorted into ascending order. Sorting

A = {44,55,12,42,94,6,18,47}

B = {18,6,12} C = {94,55,44,47}

{42}

{6} {18}

{12} {94}

{55,44,47}

{47}

{44} {55}

... repeatedly partition until sets with a single member, reassembling gives sorted set

Page 40: Consider an array of n values to be sorted into ascending order. Sorting

37 25 57 48 12 92 86 33[0] [1] [2] [3] [4] [5] [6] [7]

pivot = a[0] bottom = 0 top = 7

bottom top

Refer to algorithm(steps 3 and 4) & carry out “dry runs” in tutorials

Page 41: Consider an array of n values to be sorted into ascending order. Sorting

... ensure elements with indices above top are greater than pivot and elements with indices below bottom are less than pivot

33 25 12 37 48 92 86 57[0] [1] [2] [3] [4] [5] [6] [7]

bottom top

At the end of the first partition the array is “more sorted” than before

Page 42: Consider an array of n values to be sorted into ascending order. Sorting

33 25 12 37 48 92 86 57

bottom top bottom top

Repeat the partition on each set....