algorithms

22
Algorithms Algorithms Sorting and Merging Sorting and Merging

Upload: zenia-chaney

Post on 31-Dec-2015

38 views

Category:

Documents


3 download

DESCRIPTION

Algorithms. Sorting and Merging. Learning Objectives. Explain the difference between insertion sort and merge sort. Describe algorithms for implementing insertion sort and merge sort methods. Sorting Methods. Insertion Sort Quick Sort Merge Sort. 1.Insertion Sort. Sorting. - PowerPoint PPT Presentation

TRANSCRIPT

AlgorithmsAlgorithmsSorting and MergingSorting and Merging

Learning ObjectivesLearning Objectives

Explain the differences between insertion sort, quick sort and merge sort.

Describe algorithms for implementing insertion sort, quick sort and merge sort methods.

Sorting MethodsSorting Methods

1.1. Insertion SortInsertion Sort

2.2. Quick SortQuick Sort

3.3. Merge SortMerge Sort

1.1. Insertion SortInsertion Sort

SortingSorting

Ordering values from a single list in Ordering values from a single list in an ascending or descending order.an ascending or descending order. E.g. E.g.

Ascending numericAscending numeric 3 5 6 8 12 16 253 5 6 8 12 16 25

Descending alphabeticDescending alphabetic Will Rose Mattu Juni Hazel Dopu AnneWill Rose Mattu Juni Hazel Dopu Anne

Example Example – Sort the list below into ascending order– Sort the list below into ascending order

20 47 12 53 32 84 85 96 45 1820 47 12 53 32 84 85 96 45 18

1.1. Start with 2Start with 2ndnd number: number: 47 > 2047 > 20 so no so no change in the order is made. change in the order is made.

2.2. 12 < 4712 < 47 , , 12 < 2012 < 20 so insert 12 before 20: so insert 12 before 20: 12 20 47 53 32 84 85 96 45 1812 20 47 53 32 84 85 96 45 18

4.4. This is continued until the last number is This is continued until the last number is inserted in its correct position. inserted in its correct position.

Key for table on the next slideKey for table on the next slide

BlueBlue PredecessorsPredecessors

RedRed Value being comparedValue being compared

Example ContinuedExample Continued20 47 12 53 32 84 85 96 45 18 Original list, start with second number.

20 47 12 53 32 84 85 96 45 18 No change needed.

20 47 12 53 32 84 85 96 45 18 Now compare 12 with its predecessors.

12 20 47 53 32 84 85 96 45 18 Insert 12 before 20.

12 20 47 53 32 84 85 96 45 18 Move to next value.

12 20 47 53 32 84 85 96 45 18 53 is in the correct place.

12 20 47 53 32 84 85 96 45 18 Move to the next value.

12 20 32 47 53 84 85 96 45 18 Insert it between 20 and 47

12 20 32 47 53 84 85 96 45 18 Move to the next value.

12 20 32 47 53 84 85 96 45 18 84 is in the correct place.

12 20 32 47 53 84 85 96 45 18 Move to the next value.

12 20 32 47 53 84 85 96 45 18 85 is in the correct place.

12 20 32 47 53 84 85 96 45 18 Move to the next value.

12 20 32 47 53 84 85 96 45 18 96 is in the correct place.

12 20 32 47 53 84 85 96 45 18 Move to the next value.

12 20 32 45 47 53 84 85 96 18 Insert 45 between 32 and 47.

12 20 32 45 47 53 84 85 96 18 Move to the next value.

12 18 20 32 45 47 53 84 85 96 Insert 18 between 12 and 20.

General MethodGeneral Method

1.1. Compare each value in turn with the Compare each value in turn with the values before it in the list. values before it in the list.

2.2. Insert the value into its correct position.Insert the value into its correct position.

3.3. Repeat until the last value is compared Repeat until the last value is compared with the rest of the list.with the rest of the list.

2.2. Quick SortQuick Sort

Quick SortQuick Sorthttps://www.youtube.com/watch?v=3OLTJlwyIqQ

Note that I have 2 issues with this explanation though:Note that I have 2 issues with this explanation though:1.1. Why compare the left and right pointer values again after swapping Why compare the left and right pointer values again after swapping

their values? their values? They have just been compared, that’s why they were swapped, so why They have just been compared, that’s why they were swapped, so why

compare again?!compare again?!

2.2. Why bother examining sub arrays with 1 element? Why bother examining sub arrays with 1 element? One element cannot be out of order, you cannot move only 1 element One element cannot be out of order, you cannot move only 1 element

anywhere”!?anywhere”!?

Also try “playing” with my “Also try “playing” with my “Quick Sort Demonstrator”.”. Note that I have resolved the 2 issues mentioned above by:Note that I have resolved the 2 issues mentioned above by:

Moving the left or right pointer, as appropriate, if a swap has occurred; Moving the left or right pointer, as appropriate, if a swap has occurred; before comparing again.before comparing again.Not sorting sub arrays with only one element (just Not sorting sub arrays with only one element (just stating that they are stating that they are already in their sorted positions and moving on).already in their sorted positions and moving on).

Insertion Vs Quick SortInsertion Vs Quick Sort

The The QuicksortQuicksort method is one of the method is one of the fastestfastest sorting algorithms for sorting algorithms for sorting sorting large lists large lists ofof data.  data. 

The The InsertionInsertion sortsort method is a method is a fastfast sorting algorithm for sorting sorting algorithm for sorting very small very small listslists that are that are already somewhat sortedalready somewhat sorted. . 

Other Quick Sort ExplanationsOther Quick Sort Explanations

If you do some further research of the “Quick Sort” method, If you do some further research of the “Quick Sort” method, you will find that there appears to be a number of ways to you will find that there appears to be a number of ways to explain it.explain it.

One issue appears to be how to choose the pivot:One issue appears to be how to choose the pivot:1. Always pick the first element as pivot (as previously demonstrated).

2. Always pick the last element as pivot (similar to what has previously been

demonstrated).

3. Pick a random element as the pivot.https://www.youtube.com/watch?v=y_G9BkAm6B8

4. Pick the median as the pivot.Commonly dismissed due to the amount of time it takes to find the median.Commonly dismissed due to the amount of time it takes to find the median.

It is also possible to consider methods 3 & 4 above as the same as what has It is also possible to consider methods 3 & 4 above as the same as what has previously been demonstrated, as long we swap the random or median previously been demonstrated, as long we swap the random or median element with the first element before starting.element with the first element before starting.

http://geeksquiz.com/quick-sort/

http://me.dt.in.th/page/Quicksort/

Quick Sort Further Research LinksQuick Sort Further Research Links

Animations:Animations: https://www.youtube.com/watch?v=y_G9BkAm6B8 http://me.dt.in.th/page/Quicksort/ http://upload.wikimedia.org/wikipedia/commons/9/9c/Quicksort-http://upload.wikimedia.org/wikipedia/commons/9/9c/Quicksort-

example.gifexample.gif http://sorting.at/http://sorting.at/ http://en.wikipedia.org/wiki/File:Sorting_quicksort_anim.gifhttp://en.wikipedia.org/wiki/File:Sorting_quicksort_anim.gif

Diagram:Diagram: http://www.dreamincode.net/forums/uploads/post-71096-http://www.dreamincode.net/forums/uploads/post-71096-

1204823957.jpg1204823957.jpg

Written Explanations:Written Explanations: http://www.java2novice.com/java-sorting-algorithms/quick-sort/http://www.java2novice.com/java-sorting-algorithms/quick-sort/ http://en.wikipedia.org/wiki/Quicksorthttp://en.wikipedia.org/wiki/Quicksort http://geeksquiz.com/quick-sorthttp://geeksquiz.com/quick-sort//

3.3. Merge SortMerge Sort

MergingMerging

Taking two lists which have been sorted Taking two lists which have been sorted into the same order and putting them into the same order and putting them together to form a single sorted list. together to form a single sorted list. E.g. E.g.

Bharri Emi Kris Mattu Parrash Roger WillBharri Emi Kris Mattu Parrash Roger Will

andand Annis Chu Liz Medis SteAnnis Chu Liz Medis Ste

Merge to:Merge to: Annis Bharri Chu Emi Kris Liz Mattu Medis Annis Bharri Chu Emi Kris Liz Mattu Medis

Parrash Roger Ste Will Parrash Roger Ste Will

ExampleExample

2 4 7 10 152 4 7 10 15

&&

3 5 12 14 18 263 5 12 14 18 26

1.1. Compare the first values in each list.Compare the first values in each list. 2 < 3 so put it in a new list.2 < 3 so put it in a new list.

New = 2New = 2

Example ContinuedExample Continued

2.2. Since 2 came from the 1Since 2 came from the 1stst list we now list we now use the next value in the first list and use the next value in the first list and compare it with the 1compare it with the 1stst number from the number from the second list (as we have not yet used it). second list (as we have not yet used it).

3 < 4 so 3 is placed in the new list.3 < 4 so 3 is placed in the new list.

New = 2 3New = 2 3

Example ContinuedExample Continued

3.3. As 3 came from the 2As 3 came from the 2ndnd list we use the list we use the next number in the 2next number in the 2ndnd list and compare it list and compare it with the next unused number in the 1with the next unused number in the 1stst list. list.

4.4. Repeat until one of the lists is exhausted Repeat until one of the lists is exhausted then copy the rest of the other list into then copy the rest of the other list into the new list.the new list.

Key for table on the next slideKey for table on the next slide

RedRed Values being comparedValues being compared

Example ContinuedExample Continued

First List Second List New List

2 4 7 10 15 3 5 12 14 18 26 2

2 4 7 10 15 3 5 12 14 18 26 2 3

2 4 7 10 15 3 5 12 14 18 26 2 3 4

2 4 7 10 15 3 5 12 14 18 26 2 3 4 5

2 4 7 10 15 3 5 12 14 18 26 2 3 4 5 7

2 4 7 10 15 3 5 12 14 18 26 2 3 4 5 7 10

2 4 7 10 15 3 5 12 14 18 26 2 3 4 5 7 10 12

2 4 7 10 15 3 5 12 14 18 26 2 3 4 5 7 10 12 14

2 4 7 10 15 3 5 12 14 18 26 2 3 4 5 7 10 12 14 15

2 4 7 10 15 3 5 12 14 18 26 2 3 4 5 7 10 12 14 15 18

2 4 7 10 15 3 5 12 14 18 26 2 3 4 5 7 10 12 14 15 18 26

MethodMethod

1.1. Compare first values in each list.Compare first values in each list.2.2. Place smaller value in new list.Place smaller value in new list.3.3. Take next value in the list which the smaller Take next value in the list which the smaller

value came from and compare with the 1value came from and compare with the 1stst value of the other list.value of the other list.

4.4. Place smaller value in new list.Place smaller value in new list.5.5. Take next value in the list which the smaller Take next value in the list which the smaller

value came from and compare with the next value came from and compare with the next unused value of the other list.unused value of the other list.

6.6. Repeat until one list is exhausted Repeat until one list is exhausted then copy then copy the rest of the other list into the new list.the rest of the other list into the new list.