1 / 17

Introduction to Sorting Algorithms

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.

cody
Download Presentation

Introduction to Sorting Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 • 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.

  3. Selection Sort Example Array numlistcontains • Smallest element is 2. Exchange 2 with element in 1st array position (i.e. element 0). Now in order

  4. 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.

  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 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

  8. 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.

  9. 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

  10. 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)

  11. Average Case Behavior for a list of length n

  12. 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

  13. 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. }

  14. Analysis of Quick Sort Algorithm for List of length n

  15. 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

  16. Merge sort algorithm

  17. 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

More Related