1 / 20

-Arrays- Sorting

-Arrays- Sorting. Introduction to Computer Science II. Week 11. Recall Arrays and Searching. You must know these concepts: Subscript Lower Bound Upper Bound Indices Row Index Column Index 2-Dimensional Array n -Dimensional Array Linear Search Binary Search. Topics covered.

odin
Télécharger la présentation

-Arrays- Sorting

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. -Arrays-Sorting Introduction to Computer Science II Week 11

  2. Recall Arrays and Searching • You must know these concepts: • Subscript • Lower Bound • Upper Bound • Indices • Row Index • Column Index • 2-Dimensional Array • n-Dimensional Array • Linear Search • Binary Search

  3. Topics covered • Simple sorting algorithms • Bubble sort • Selection sort • Insertion sort • Sorting algorithm efficiency

  4. The General Sorting Problem We are given: • A collection of data (e.g. stored in an array). • Collection elements are all of the same type. • A comparison function (e.g. a selection statement). • Given two data elements, determine which should come first. • Using this comparison function is the onlyway we can make such a determination. We should return: • A sorted collection with the same elements as the original one.

  5. Address- -based sorting Comparison-based sorting Proxmap Sort RadixSort Transposition sorting Diminishing increment sorting Insert and keep sorted Priority queue sorting Divide and conquer BubbleSort ShellSort Selection sort QuickSort MergeSort Insertion sort Tree sort Heap sort Sorting algorithms • There are many sorting algorithms. • We will discuss only: • Bubble sort • Selection sort • Insertion sort

  6. Bubble sort • Compare each element (except the last one) with its neighbor to the right. • If they are out of order, swap them. This puts the largest element at the very right. • The last element is now in the correct and final place. • Compare each element (except the last two) with its neighbor to the right. • If they are out of order, swap them. This puts the second largest element next to last. • The last two elements are now in their correct and final places. • Compare each element (except the last three) with its neighbor to the right. • … • Continue as above until you have no unsorted elements on the left

  7. 2 2 2 7 2 2 2 2 2 2 2 2 2 4 7 4 7 2 7 4 5 7 5 7 5 7 5 8 5 8 5 5 4 7 5 8 4 5 5 7 4 4 8 5 5 5 7 4 7 4 7 7 8 8 8 8 4 8 8 4 4 8 4 8 8 2 5 4 7 8 Bubble sort: Example Two loops: outer (done) inner

  8. Bubble sort: More examples • Move from the “beginning” to the “end” • “Bubble” the largest value to the “end” using comparisons and swapping End Start Phase 2

  9. Bubble sort: Efficiency analysis • Let n be the size of the array • The outer loop is executed n-1 times • Each time the outer loop is executed, the inner loop is executed. • Inner loop executes n-1 times at first, linearly dropping to just 1 • On average, inner loop executes about n/2 times for each execution of the outer loop • Number of comparisons = n(n - 1)/2 ~ n2

  10. Selection sort Given an array of lengthn, • Search elements0throughn-1and select the smallest 1.1. Swap it with the element in location 0 • Search elements1throughn-1and select the smallest 2.1. Swap it with the element in location 1 • Search elements2throughn-1and select the smallest 3.1. Swap it with the element in location 2 • Continue this processuntil there is nothing left to search

  11. 0 Selection Sort: How it works • Findsmallest (or largest) item in unsorted part of array • Swap it with first item in the unsorted part of the array • Move the “wall” forward by one item, past sorted item • Repeat until the unsorted part of the array vanishes

  12. 2 2 7 2 2 2 4 7 4 4 8 8 5 5 8 5 7 5 5 8 7 4 7 8 4 Selection Sort: Example and Analysis • The outer loop executesn-1 times • The inner loop executes about n/2 times on average • Work done in the inner loop is constant (swap two array elements) • Number of comparisons =n(n - 1)/2 ~ n2 inner outer

  13. 0 Insertion sort • Find the 1stunsorted item (just past the ‘wall’) • Searchsorted list for 1st item’s proper position • Swap 1st item: Remove from the unsorted list, and Insert into sorted list; move the ‘wall’ Insert Remove

  14. sorted next to be inserted 3 4 7 12 14 33 14 20 21 38 10 55 9 23 28 16 temp less than 10 10 10 12 21 14 14 20 33 38 3 4 7 55 9 23 28 16 sorted Example:One step of Insertion sort

  15. Insertion sort: More examples outer inner

  16. Insertion sort: Analysis • We run once through the outer loop, inserting each of n elements • On average, there are n/2 elements already sorted • The inner loop looks at (and moves) half of these, i.e. it runs n/4 times on average • Hence, the average number of comparisons = n2/4 ~ n2

  17. Nobody ever uses Bubble Sort!!! Efficiency of sorting algorithms

  18. Sorting: Better algorithms • There are many sorting algorithms more efficient than the three we studied: • Mergesort • Shellsort • Quicksort • Heapsort • … • You can “play” with and compare some of the algorithms at: http://tinyurl.com/yde66w

  19. Summary of Arrays and Sorting • Sorting algorithms • Bubble sort: very slow, and should probably never be used for anything • Selection sort: intermediate in speed • Insertion sort: the fastest of the three • Selection sort and, especially, insertion sort are “good enough” for small arrays (size ~10) • More efficient sorting algorithms need more complicated (than arrays) data structures

  20. Reading • Introduction to Computer Science, (Schaum’s Outlines series), by Ramon A. Mata-Toledo et al. ISBN 0-07-134554-X. http://www.ritsumei.ac.jp/is/~walker/Intro_To_Comp_Sci.html Next Lecture • Big Quiz 2 • Scope: All you studied in the second semester

More Related