SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE...

Click here to load reader

  • date post

    17-Dec-2015
  • Category

    Documents

  • view

    241
  • download

    5

Embed Size (px)

Transcript of SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE...

  • Slide 1
  • SORTING ROUTINES
  • Slide 2
  • OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT
  • Slide 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.
  • Slide 4
  • B UBBLE S ORT The Bubble sort uses an incremental approach. The following shows the sequence of steps in a Bubble Sort:
  • Slide 5
  • B UBBLE S ORT 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.
  • Slide 6
  • B UBBLE S ORT 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).
  • Slide 7
  • B UBBLE SORT
  • Slide 8
  • S ELECTION 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:
  • Slide 9
  • S ELECTION SORT
  • Slide 10
  • Slide 11
  • 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.
  • Slide 12
  • I NSERTION 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:
  • Slide 13
  • I NSERTION SORT
  • Slide 14
  • Slide 15
  • 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(n 2 ).
  • Slide 16
  • Q UICK 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.
  • Slide 17
  • Q UICK 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.
  • Slide 18
  • Q UICK SORT
  • Slide 19
  • 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.
  • Slide 20
  • M ERGE 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:
  • Slide 21
  • M ERGE SORT
  • Slide 22
  • The merge sort is often implemented recursively as illustrated with the following code:
  • Slide 23
  • B IG 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.
  • Slide 24
  • B IG O SUMMARY
  • Slide 25
  • L ESSON 41 SORTING ROUTINES
  • Slide 26
  • WHAT AM I?????
  • Slide 27
  • W HAT AM I ?
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • SORTING ROUTINES