Download presentation

Presentation is loading. Please wait.

Published bySandra Landry Modified over 2 years ago

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

2
Involves repeated passes(scans) through the data in the array. 9.1The Bubble Sort

3
This involves comparing successive pairs of items

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

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

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

7
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

8
i = 0; j = i + 1; 0 i 1 jij SWAP

9
i++; j = i + 1; 0 i 1 j ij

10
i++; j = i + 1; 1 i 2 j ij

11
SWAP 2 i 3 j ij

12
2 i 3 j ij i++; j = i + 1;

13
3 i 4 j ij SWAP

14
3 i 4 j ij i++; j = i + 1;

15
4 i 5 j ij SWAP

16
4 i 5 j ij i = 0; j = i + 1; 1 Pass completed

17
0 i 1 j ij i++ = 0; j = i + 1; Start of 2nd pass … and so on

18
When does the program know when to stop? “Test that no interchanges have occurred in a pass.”

19
Bubble sort is not regarded as being efficient.

20
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.

21
... 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.

22
Insertion Sort

23
This works by finding the smallest in the array and putting it into position [0]. 9.3 The Selection Sort

24
The “remaining array” is then sorted in a similar way. More efficient way of sorting than bubble sort. Example follows

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

26
0 i 0 min 1 j if (a[j] < a[min]) min = j; iminj

27
j++; 0 i 1 min 1 j i j

28
0 i 1 2 j i j if (a[j] < a[min]) min = j;

29
0 i 2 min 2 j i j j++;

30
0 i 2 min 3 j i j if (i != min) swap(a[i],a[min]) SWAP

31
0 i 2 min 3 j i j i++; min = i; j = i + 1;

32
1 i 1 min 2 j i j if (a[j] < a[min]) min = j; Sort the “remaining array” in similar way

33
1 i 1 min 2 j i j j++;

34
1 i 1 min 3 j i j if (i != min) swap(a[i],a[min]) i.e.false

35
1 i 1 min 3 j i j i++; min = i; j = i + 1;

36
imin j TERMINATE 2 i 2 min 3 ji < (a.length-1) i.e false

37
This is known as a Partition Exchange Sort and was invented by C.A.Hoare in The Quick Sort Method In general faster than the bubble and selection sorts.

38
This involves partitioning a set of data into subsets with respect to: an ordering relation particular item - a “pivot”

39
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

40
pivot = a[0]bottom = 0top = 7 bottomtop Refer to algorithm(steps 3 and 4) & carry out “dry runs” in tutorials

41
... ensure elements with indices above top are greater than pivot and elements with indices below bottom are less than pivot bottomtop At the end of the first partition the array is “more sorted” than before

42
bottomtopbottomtop Repeat the partition on each set....

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google