4
D1: Quick D1: Quick Sort Sort

D1: Quick Sort. The quick sort is an algorithm that sorts data into a specified order. For a quick sort, select the data item in the middle of the list

Embed Size (px)

Citation preview

Page 1: D1: Quick Sort. The quick sort is an algorithm that sorts data into a specified order. For a quick sort, select the data item in the middle of the list

D1: Quick D1: Quick SortSort

Page 2: D1: Quick Sort. The quick sort is an algorithm that sorts data into a specified order. For a quick sort, select the data item in the middle of the list

D1: Quick Sort

The quick sort is an algorithm that sorts datainto a specified order.For a quick sort, select the data item in the middle of the list (the pivot). Then all the items smaller than this are put to its left, and all the larger items go to its right. This creates two sublists – the process is then repeated on these, as if they were two separate lists. This splits the main list still further. Repeat the process until all the data items are either a pivot, or a sublist of one.If there are an even number of data items, there will be two data items in the middle of the list. Select the right-most one as the pivot.

Page 3: D1: Quick Sort. The quick sort is an algorithm that sorts data into a specified order. For a quick sort, select the data item in the middle of the list

D1: Quick Sort

Example: 2 8 5 3 7 9 6 4

The list of numbers shown above is to be sorted into ascending order. Apply quick sort to obtain the sorted list. You must make your pivots clear.

Working

2 8 5 3 7 9 6 4

Why 7 and not 3?

2 5 3 6 4 7 8 9

2 3 5 6 4 7 8 9

2 3 5 4 6 7 8 9

2 3 4 5 6 7 8 9

Old pivots have a different shape.

Stop.

Page 4: D1: Quick Sort. The quick sort is an algorithm that sorts data into a specified order. For a quick sort, select the data item in the middle of the list

D1: Quick Sort

Notes

• When you move a data item ‘over’ a pivot, it must move as little as possible. Put them just past the pivot (see the previous example).

• Write “Stop” when you have finished with the sort.

• Sorts can be done in descending order, too. Read the question carefully.

• Many of the questions on past papers for this type of sort feature data in words.