1 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here Bubble sort Selection sort
2 Selection Sort Algorithm 1.Locate smallest element in array and exchange it with element in position 0. 2.Locate next smallest element in array and exchange it with element in position 1. 3.Continue until all elements are in order.
3 Selection Sort Example Array numlist contains 1.Smallest element is 2. Exchange 2 with element in 1 st array position (i.e. element 0) Now in order
4 Selection Sort – Example (continued) 2.Next smallest element is 3. Exchange 3 with element in 2 nd array position. 3.Next smallest element is 11. Exchange 11 with element in 3 rd array position Now in order
5 Selection Sort Tradeoffs Benefit Easy to understand Disadvantage Best and Average case same as Worst case
6 Selection Sort Algorithm On the first pass through the outer loop of the insertion sort the inner loop compares the second element to the first element If the second element is smaller, it is swapped with the first element. During the second pass through the outer loop the third element is compared to the second element if smaller than the second element, it is swapped with the second element. Then the second element is compared to the first element, and swapped if smaller. Continue for remaining elements.
7 Insertion Sort Example Array numlist contains 1.Second element 2 is smaller than first element 11. Exchange 2 with element in 1 st array position (i.e. element 0). 2.Next compare third element 29 to second element is larger than 11, so move on to fourth element Now in order
8 Insertion Sort – Example (continued) 3.Next look at fourth element 3. Exchange 3 with element in 3 rd array position. 4.Next compare 3 to 11. Exchange 11 with 3 array position
The insertion sort algorithm can also be applied to linked lists In a linked list, traversal is in only one direction starting at the first node Insertion Sort: Linked List-Based
The average number of comparisons and the average number of item assignments in an insertion sort algorithm are: 1/4 n 2 + O(n) = O(n 2 ) Analysis: Insertion Sort
AlgorithmNumber of Comparisons Number of Swaps Selection Sort n(n-1) = O(n 2 ) 2 3(n-1) = O(n) Insertion Sort ¼* n 2 + O(n) = O(n 2 ) ¼* n 2 + O(n) = O(n 2 ) Average Case Behavior for a list of length n
The quick sort algorithm uses the divide-and-conquer technique to sort a list The list is partitioned into two sublists, and the two sublists are then sorted and combined into one list in such a way that the combined list is sorted Quick Sort: Array-Based Lists
The general algorithm is: if (list size is greater than 1) { 1. Partition the list into two sublists, say lowerSublist and upperSublist. 2. Quick sort lowerSublist. 3. Quick sort upperSublist. 4. Combine the sorted lowerSublist and sorted upperSublist. } Quick Sort: Array-Based Lists
Analysis of Quick Sort Algorithm for List of length n Number of Comparisons Number of Swaps Average Case (1.39)n*log 2 n+O(n) = O(n*log 2 n) (0.69)*n*log 2 n+O(n) = O(n*log 2 n) Worst Case n 2 /2 - n/2 = O(n 2 )n 2 /2+ 3n/2 - 2 = O(n 2 )
Merge sort uses the divide-and-conquer technique to sort a list It partitions the list into two sublists, and then combines the sorted sublists into one sorted list It partitions the list into nearly equal sizes For example, consider the list: List: Merge sort partitions this list into two sublists as follows first sublist: second sublist: Merge Sort: Linked List-Based
16 Merge sort algorithm
Divide Because the data are stored in a linked list, we do not know the length of the list To find the middle of the list we traverse the list with two pointers, say middle and current Merge Once the sublists are sorted the next step in the merge sort is to merge the sorted sublists Sublists are merged by comparing the elements of the sublists and adjusting the pointer of the nodes with the smaller info Divide and Merge