Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 2. Starter Look at the hand out you have been given. Can you sort the numbers into ascending order. What mental or physical methods did you use.

Similar presentations


Presentation on theme: "Lesson 2. Starter Look at the hand out you have been given. Can you sort the numbers into ascending order. What mental or physical methods did you use."— Presentation transcript:

1 Lesson 2

2 Starter Look at the hand out you have been given. Can you sort the numbers into ascending order. What mental or physical methods did you use to sort them?

3 Algorithms - Sorting  Sorting algorithms are very frequently used in programming.  There are many situations in which data needs to be sorted.  There are many different sorting algorithms out there that have different level of efficiencies.  Some example of sorts:  Bubble  Selection  Insertion  Quick VIDEO – Sorting.mp4

4 Algorithms - Sorting Bubble Sort Algorithm for bubble sort count1 = length of list count2 = length of list -1 FOR 1 to count1 FOR 1 to count2 IF list[count2] > list[count2+1] THEN temp = list[count2] list[count2] = list[count2+1] list[count2+1] = temp ENDIF ENDFOR

5 Algorithms - Sorting Bubble Sort 6213214013 2613214013 2163214013 2136214013 2132614013 2132164013 2132146013 2132140613 2132140163 2132140136 Key Sorted list Items being swapped

6 Algorithms - Sorting Bubble Sort 1232140136 Key Sorted list Items being swapped 1223140136 1221340136 1221304136 1221301436 1221301346 1221301346 Etc....... Using your numbers – use the bubble sort method to sort your numbers

7 Algorithms - Sorting Selection sort Algorithm for selection sort minimum = 99999999 count1 = length of list count2 = length of list positionofminimum = 1 FOR 1 to count1 FOR 1 to count2 IF list(count2) < minimum THEN minimum = list(count2) positionofminimum = count2 ENDIF ENDFOR temp = list(count1) list(count1) = list(positionofminimum) list(positionofminimum) = temp END FOR

8 6213214013 Key Sorted list Items being swapped 0213214613 0123214613 0113224613 0111224633 0111224633 0111224633 0111223643 0111223346 0111223346 Algorithms – Sorting Selection Using your numbers – use the selection sort method to sort your numbers

9 Algorithms – Sorting Insertion Sort Algorithm for insertion sort FOR every item in list Sort into new list by inserting it into correct place, one item at a time. END FOR

10 Algorithms – Sorting Insertion Sort 6213214013 2613214013 1263214013 1236214013 1223614013 1122364013 1122346013 0112234613 0111223463 0111223346 Key Sorted list Using your numbers – use the insertion sort method to sort your numbers

11 Algorithms - Sorting  Look at these examples of how different algorithms can change the speed of the sorting Link 1 – CLICK HERECLICK HERE http://www.sorting-algorithms.com/ Link 2 – CLICK HERECLICK HERE http://www.cs.ubc.ca/~harrison/Java/sorting-demo.html

12 Algorithms - Sorting  Task With a bit of research – compare and contrast the bubble sort, insertion sort, selection sort and Quick sort. Ask yourself the following questions 1)Can you describe how each one works? 2)What is difference between them? 3)Which one is the most efficient? 4)Why do you think one is more efficient than another – what makes is more efficient?


Download ppt "Lesson 2. Starter Look at the hand out you have been given. Can you sort the numbers into ascending order. What mental or physical methods did you use."

Similar presentations


Ads by Google