Download
sorting routines n.
Skip this Video
Loading SlideShow in 5 Seconds..
SORTING ROUTINES PowerPoint Presentation
Download Presentation
SORTING ROUTINES

SORTING ROUTINES

162 Views Download Presentation
Download Presentation

SORTING ROUTINES

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. SORTING ROUTINES

  2. OBJECTIVES • INTRODUCTION • BUBBLE SORT • SELECTION SORT • INSERTION SORT • QUICK SORT • MERGE SORT

  3. INTRODUCTION • What is sorting? • Sorting simply means arranging items in ascending or descending order. • Two types of approaches to sorting are described here: • 1. The incremental approach • 2. The divide-and-conquer approach (typically uses recursion) • Of the two, divide-and-conquer is by far the fastest (in most cases)…but also the most complicated.

  4. Bubble Sort • The Bubble sort uses an incremental approach. • The following shows the sequence of steps in a Bubble Sort:

  5. Bubble Sort • After the first pass we notice that the largest value (5) has “bubbled” its way to the end of the list; however, the array is still not in order. • Continue to repeat this process until no swaps are made. Only then is the list in order. • On each subsequent pass the next largest value will “bubble” its way to its correct position near the end of the list.

  6. Bubble Sort • Very, very slow: • This Bubble Sort is the slowest and most inefficient of all the sorting routines. • It should only be used if you have a very few items to sort (say, 50 items or less).

  7. Bubble sort

  8. Selection sort • The Selection Sort uses an incremental approach. • During the first pass the smallest value is selected from the entire array and swapped with the first element. • On the second pass the smallest value is selected from the array beginning with the 2nd element and swapped with the second element, etc….the above description is for an ascending sort. • The following shows the sequence of steps in a Selection Sort:

  9. Selection sort

  10. Selection sort

  11. Selection sort • Disadvantage: • A disadvantage of the selection sort is that it will not allow an early exit from the entire process if the list becomes ordered in an early pass.

  12. Insertion sort • The Insertion Sort uses an incremental approach. • It works similar to the way you might organize a hand of cards. • The unsorted cards begin face down on the table and are picked up one by one. • As each new unsorted card is picked up, it is inserted into the correct order in your organized hand of cards. • The following shows the sequence of steps in an Insertion Sort:

  13. Insertion sort

  14. Insertion sort

  15. Insertion sort • An advantage: • The Insertion Sort has an advantage over the Selection Sort since it takes advantage of a partially ordered list. • This is evidenced by the fact that in a best case, big O for an Insertion Sort is O(n), whereas for a Selection Sort, it is always O(n2).

  16. Quick sort • Two partitions: • The Quick Sort uses a divide-and-conquer approach. • It begins by breaking the original list into two partitions (sections) based on the value of some “pivot value”. • One partition will eventually contain all the elements with values greater than the pivot value. • The other will eventually contain all the elements with values less than or equal to the pivot value. (This description is not always completely true, but close.) • Repeat this process on each partition. • Notice the word partition above. • This is a salient feature of the Quick Sort. To identify this type of sort, look for the word “partition” (or equivalent term) in a rem or perhaps as a variable name.

  17. Quick sort • Two partitions: • The Quick Sort uses a divide-and-conquer approach. • It begins by breaking the original list into two partitions (sections) based on the value of some “pivot value”. • One partition will eventually contain all the elements with values greater than the pivot value. • The other will eventually contain all the elements with values less than or equal to the pivot value. (This description is not always completely true, but close.) • Repeat this process on each partition. • Notice the word partition above. • This is a salient feature of the Quick Sort. To identify this type of sort, look for the word “partition” (or equivalent term) in a rem or perhaps as a variable name.

  18. Quick sort

  19. Quick sort • Summary of how Quick Sort works: • A “pivot value” is selected. • Usually this is the element at the center position of the array. • Elements in the array are moved such that all elements less than the pivot value are in one half (partition) and all elements larger than or equal to the pivot value are in the other half (partition). • This process is continually repeated on each partition. • The partitions become smaller until they each consist of just a single element. • At that point the array is ordered.

  20. Merge sort • The Merge Sort uses the divide-and-conquer approach. • It begins by placing each element into its own individual list. • Then each pair of adjacent lists is combined into one sorted list. • This continues until there is one big, final, sorted list. The process is illustrated below:

  21. Merge sort

  22. Merge sort • The merge sort is often implemented recursively as illustrated with the following code:

  23. Big o summary • It will probably be easier to learn the Big O designation for each sorting and search routine when simultaneously viewing all of them in a table • Occasionally, “best case” is referred to as the most restrictive or fastest executing case. • Similarly, “worst case” is referred to as the least restrictive or slowest executing case.

  24. Big o summary

  25. Quick Bubble Merge Lesson 41 Selection SORTING ROUTINES Insertion

  26. SORTING ROUTINES WHAT AM I?????

  27. What am i? Bubble

  28. What am i? Selection

  29. What am i? Insertion

  30. What am i? Quick

  31. What am i? Merge

  32. What am i? Merge (con’t)

  33. What am i? Merge

  34. SORTING ROUTINES