SORTING ROUTINES
OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT
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.
B UBBLE S ORT The Bubble sort uses an incremental approach. The following shows the sequence of steps in a Bubble Sort:
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.
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).
B UBBLE SORT
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:
S ELECTION 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.
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:
I NSERTION 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(n 2 ).
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.
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.
Q UICK 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.
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:
M ERGE SORT
The merge sort is often implemented recursively as illustrated with the following code:
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.
B IG O SUMMARY
L ESSON 41 SORTING ROUTINES
WHAT AM I?????
W HAT AM I ?
SORTING ROUTINES