190 likes | 738 Views
Introduction to Sorting Algorithms. Sort : arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here Bubble sort Selection sort. Selection Sort Algorithm. Locate smallest element in array and exchange it with element in position 0.
E N D
Introduction to Sorting Algorithms • Sort: arrange values into an order • Alphabetical • Ascending numeric • Descending numeric • Two algorithms considered here • Bubble sort • Selection sort
Selection Sort Algorithm • Locate smallest element in array and exchange it with element in position 0. • Locate next smallest element in array and exchange it with element in position 1. • Continue until all elements are in order.
Selection Sort Example Array numlistcontains • Smallest element is 2. Exchange 2 with element in 1st array position (i.e. element 0). Now in order
Now in order Now in order Selection Sort – Example(continued) • Next smallest element is 3. Exchange 3 with element in 2nd array position. • Next smallest element is 11. Exchange 11 with element in 3rd array position.
Selection Sort Tradeoffs • Benefit • Easy to understand • Disadvantage • Best and Average case same as Worst case
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.
Insertion Sort Example Array numlistcontains • Second element 2 is smaller than first element 11. Exchange 2 with element in 1st array position (i.e. element 0). • Next compare third element 29 to second element 11. 29 is larger than 11, so move on to fourth element. Now in order
Insertion Sort – Example(continued) • Next look at fourth element 3. Exchange 3 with element in 3rd array position. • Next compare 3 to 11. Exchange 11 with 3 array position.
Insertion Sort: Linked List-Based • 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
Analysis: Insertion Sort • The average number of comparisons and the average number of item assignments in an insertion sort algorithm are: 1/4 n2 + O(n) = O(n2)
Quick Sort: Array-Based Lists • 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. }
Merge Sort: Linked List-Based • 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: 35 28 18 45 62 48 30 38 • Merge sort partitions this list into two sublists as follows first sublist: 35 28 18 45 second sublist: 62 48 30 38
Divide and Merge • 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