Presentation is loading. Please wait.

Presentation is loading. Please wait.

SORTING & SEARCHING - Bubble SortBubble Sort - Insertion SortInsertion Sort - Quick SortQuick Sort - Binary SearchBinary Search 2 nd June 2005 Thursday.

Similar presentations


Presentation on theme: "SORTING & SEARCHING - Bubble SortBubble Sort - Insertion SortInsertion Sort - Quick SortQuick Sort - Binary SearchBinary Search 2 nd June 2005 Thursday."— Presentation transcript:

1 SORTING & SEARCHING - Bubble SortBubble Sort - Insertion SortInsertion Sort - Quick SortQuick Sort - Binary SearchBinary Search 2 nd June 2005 Thursday Lower Sixth Form Students

2 BUBBLE SORT A slow method & useful when there is a small number of items to be sorted. It is where the values to be sorted are listed vertically the small values bubble up to the top or the large values sink to the bottom depending on the version of the algorithm used. To save space we shall write the values from left to right. 2/17 Next Home

3 For example, to sort the following values into ascending order: 15 3 11 2 8 19 6 - The first two values are compared and exchanged. The next two values are then compared and so on. After the first past the values will be in the following order: 3 11 2 8 15 6 19 BUBBLE SORT 3/17 Next Home

4 After the 2 nd pass: 3 2 8 11 6 15 19 After the 3 rd pass: 2 3 8 6 11 15 19 After the 4 th pass: 2 3 6 8 11 15 19 -On the 5 th pass, no swaps are necessary because the sort is complete. -TRY IT YOUR SELF NOW! BUBBLE SORT 4/17 Next Home

5 INSERTION SORT Insertion sort is faster than the bubble sort but is also relatively slow and suited to store a small number of items. The easiest way to understand this sort is to take 5 cards each with a number written on them and place them in a random sequence in a line. 5/17 Next Home

6 Starting with the 2 nd card, place it temporarily to the left of position 1 (position 0) and starting just to the left of the gap, compare each card up to the gap with the card in position 0, moving it along one place if it is greater. Then replace the card from position 0 in the gap. INSERTION SORT 53862 POSITION 012345 6/17 Next Home

7 On the next pass, nothing changes. On the third pass, INSERTION SORT 35862 POSITION 012345 35682 7/17 Next Home

8 On the fourth and final pass, The insertion sort is faster than bubble sort. It takes a minute to sort an array of 2000 items but using bubble sort takes 5 to 6 minutes. INSERTION SORT 23568 8/17 Next Home

9 QUICK SORT The quick sort is a very fast sort invented by C.Hoare. It based on the general principle that exchanges should be made between items which are a large distance apart in the array holding them. Quicksort uses a complex recursive algorithm which starts by comparing the first and last elements in the array. For large arrays, it can be hundreds of times faster than the simple bubble sort. 9/17 Next Home

10 QUICK SORT Interactive Example of Quick Sort: http://ciips.ee.uwa.edu.au/~morris/ Year2/PLDS210/qsort.html 10/1 7 Next Home

11 BINARY SEARCH Binary search is used for searching an ordered array. In a binary search, the ordered array is divided into three parts: a middle item, the lower part of the array and the upper part. 11/1 7 Next Home

12 The middle item is examined to see if it is equal to the wanted item. If it is not, then if it is greater than the wanted item, the upper half of the array is of no further interest. The number of items being searched is therefore halved and the process repeated until the last item is examined, with either the upper half or lower half of the items searched being eliminated at each pass. BINARY SEARCH 12/1 7 Next Home

13 Binary search uses binary tree which shows data structure that has zero or more nodes organised in a hierarchical way. BINARY SEARCH 13/1 7 Next Home Root Level 0 Level 1 Level 2 Nodes

14 3 types of traversing a binary tree: (i) Pre-order traversal (ii) In-order traversal (iii) Post-order traversal - Traversing a binary tree refer to the stage at which the node is visited. BINARY SEARCH 14/1 7 Home Next

15 Pre-order traversal D B A C F E G In-order traversal A B C D E F G Post-order traversal A C B E G F D BINARY SEARCH D G E C A F B 15/1 7 Next Home

16 Interactive Example of Binary Search: http://babbage.clarku.edu/~achou/ cs160/bst_animation/BST- Example.html 16/1 7 Next Home BINARY SEARCH

17 YOUR TASKS TASK ONE Find from any relevant website the interactive exercise of every one of the method above. TASK TWO Find the research_notes on every method and SUBMIT after your break-term holiday. 17/1 7 Home THANK YOU FOR YOUR ATTENTION T.Fina’s Website


Download ppt "SORTING & SEARCHING - Bubble SortBubble Sort - Insertion SortInsertion Sort - Quick SortQuick Sort - Binary SearchBinary Search 2 nd June 2005 Thursday."

Similar presentations


Ads by Google