200 likes | 287 Views
-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.
E N D
-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 • Simple sorting algorithms • Bubble sort • Selection sort • Insertion sort • Sorting algorithm efficiency
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.
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
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
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
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
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
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
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
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
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
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
Insertion sort: More examples outer inner
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
Nobody ever uses Bubble Sort!!! Efficiency of sorting algorithms
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
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
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