introduction to sorting algorithms l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction to Sorting Algorithms PowerPoint Presentation
Download Presentation
Introduction to Sorting Algorithms

Loading in 2 Seconds...

play fullscreen
1 / 17

Introduction to Sorting Algorithms - PowerPoint PPT Presentation


  • 206 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Introduction to Sorting Algorithms' - cody


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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
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
Selection Sort Example

Array numlistcontains

  • Smallest element is 2. Exchange 2 with

element in 1st array position (i.e. element 0).

Now in order

selection sort example continued

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
Selection Sort Tradeoffs
  • Benefit
    • Easy to understand
  • Disadvantage
    • Best and Average case same as Worst case
selection sort algorithm6
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
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
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
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
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
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 lists13
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: 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 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