Quadratic: the time that it takes to sort an array is proportional to the. square of the number of elements.

Similar documents
Searching, Sorting. Arizona State University 1

LECTURE 17. Array Searching and Sorting

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms

Sorting Pearson Education, Inc. All rights reserved.

Programming in OOP/C++

C/C++ Programming Lecture 18 Name:

Sorting. Quicksort analysis Bubble sort. November 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Abstract Data Type: Stack

Analysis of Algorithms

SCJ2013 Data Structure & Algorithms. Bubble Sort. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

CS 231 Data Structures and Algorithms Fall Algorithm Analysis Lecture 16 October 10, Prof. Zadia Codabux

Principles of Programming. Chapter 6: Arrays

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

COMP 250. Lecture 7. Sorting a List: bubble sort selection sort insertion sort. Sept. 22, 2017

KF5008 Algorithm Efficiency; Sorting and Searching Algorithms;

Algorithm Performance. (the Big-O)

Measuring algorithm efficiency

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Algorithmic Analysis. Go go Big O(h)!

Computers in Engineering COMP 208. Where s Waldo? Linear Search. Searching and Sorting Michael A. Hawker

Sorting. Bringing Order to the World

Today. CISC101 Reminders & Notes. Searching in Python - Cont. Searching in Python. From last time

while (condition) { body_statements; for (initialization; condition; update) { body_statements;

CS 261 Data Structures. Big-Oh Analysis: A Review

Merge Sort Roberto Hibbler Dept. of Computer Science Florida Institute of Technology Melbourne, FL

Problems with simple variables

Subject: Computer Science

CS 201, Fall 2016 Oct 28th Exam 2. Question 1. [5 points] Consider the following static method:

Sorting. Bubble Sort. Selection Sort

Algorithm Analysis. Big Oh

CS2223: Algorithms D- Term, Homework I. Teams: To be done individually. Due date: 03/27/2015 (1:50 PM) Submission: Electronic submission only

Solutions to Chapter 8

What is sorting? Lecture 36: How can computation sort data in order for you? Why is sorting important? What is sorting? 11/30/10

CISC 1100: Structures of Computer Science

What is an algorithm? CISC 1100/1400 Structures of Comp. Sci./Discrete Structures Chapter 8 Algorithms. Applications of algorithms

ASYMPTOTIC COMPLEXITY

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Pseudo code of algorithms are to be read by.

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

CS256 Applied Theory of Computation

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Sorting algorithms: Bubble, Selection, and Tree Sort

Chapter 7 Multidimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Vector: A series of scalars contained in a column or row. Dimensions: How many rows and columns a vector or matrix has.

SORTING AND SEARCHING

Elementary Sorting Algorithms

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

Chapter 7 Multidimensional Arrays

Elementary maths for GMT. Algorithm analysis Part II

Arrays. Storing Multiple Values

Sorting Algorithms. + Analysis of the Sorting Algorithms

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

! Search: find an item in an array, return the. ! Sort: rearrange the items in an array into some. ! There are various methods (algorithms) for

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

LINEAR INSERTION SORT

Week 12: Running Time and Performance

Data Structures and Algorithms

ASYMPTOTIC COMPLEXITY

Using Templates to Introduce Time Efficiency Analysis in an Algorithms Course

Plan of the lecture. Quick-Sort. Partition of lists (or using extra workspace) Quick-Sort ( 10.2) Quick-Sort Tree. Partitioning arrays

Weka ( )

Programming II (CS300)

Example Algorithms. CSE 2320 Algorithms and Data Structures Alexandra Stefan. University of Texas at Arlington. Last updated 9/7/2016

(6-2) Efficiency of Algorithms D & D Chapter 20. Instructor - Andrew S. O Fallon CptS 122 (July 11, 2018) Washington State University

CS 3410 Ch 5 (DS*), 23 (IJP^)

Searching & Sorting in Java Bubble Sort

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

CS : Data Structures

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

Armstrong Atlantic State University Engineering Studies MATLAB Marina Sorting Primer

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Checking for duplicates Maximum density Battling computers and algorithms Barometer Instructions Big O expressions. John Edgar 2

Sorting Algorithms part 1

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Columns A[0] A[0][0] = 20 A[0][1] = 30

106B Final Review Session. Slides by Sierra Kaplan-Nelson and Kensen Shi Livestream managed by Jeffrey Barratt

Algorithm Analysis. Performance Factors

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY

Lecture 13: AVL Trees and Binary Heaps

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Benefits of object-orientationorientation

17 February Given an algorithm, compute its running time in terms of O, Ω, and Θ (if any). Usually the big-oh running time is enough.

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Search,Sort,Recursion

1 a = [ 5, 1, 6, 2, 4, 3 ] 4 f o r j i n r a n g e ( i + 1, l e n ( a ) 1) : 3 min = i

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved

CSCI 136 Data Structures & Advanced Programming. Lecture 7 Spring 2018 Bill and Jon

UNIT 1 ANALYSIS OF ALGORITHMS

Data Structures and Algorithms

CH 8. HEAPS AND PRIORITY QUEUES

UNIT 3: ANALYSIS OF SIMPLE ALGORITHMS

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

C++ PROGRAMMING SKILLS Part 4: Arrays

CH. 8 PRIORITY QUEUES AND HEAPS

csci 210: Data Structures Program Analysis

CS 137 Part 7. Big-Oh Notation, Linear Searching and Basic Sorting Algorithms. November 10th, 2017

Transcription:

ITEC 136 Business Programming Concepts Week 12, Part 01 Overview 1 Week 12 Overview Week 11 review Associative Arrays Common Array Operations Inserting shifting elements right Removing shifting elements left Copying deep vs. shallow Searching linear vs. binary Appending to an Array

Week 12 Overview Week 11 review Deep Copy vs. Shallow Copy Linear Search no sort required Binary Search needs sorted array Outcomes Week 12 Overview Demonstrate the execution of selection, insertion and bubble sorts. Profile and analyze the performance of selection, insertion, and bubble sorts. Work with multi-dimensional arrays. 4

ITEC 136 Business Programming Concepts Week 12, Part 02 Sorting 5 Sorting an array Many different array sorting algorithms with many different tradeoffs. Quadratic sorting algorithms: selection sort, insertion sort, and bubble sort. 6

Sorting an array Many different array sorting algorithms with many different tradeoffs. Quadratic sorting algorithms: selection sort, insertion sort, and bubble sort. Quadratic: the time that it takes to sort an array is proportional to the square of the number of elements. 7 Selection sort Divide array into two parts: sorted left and unsorted right. From the unsorted right, find the smallest element. Swap it with the border element. Repeat until all is sorted. 8

Selection sort Pass 0 1 2 3 4 5 6 7 0 16 11 21 32 41 20 3 9 1 2 3 4 5 6 7 border unsorted sorted selected 9 Selection sort Pass 0 1 2 3 4 5 6 7 0 16 11 21 32 41 20 3 9 1 3 11 21 32 41 20 16 9 2 3 9 21 32 41 20 16 11 3 3 9 11 32 41 20 16 21 4 3 9 11 16 41 20 32 21 5 3 9 11 16 20 41 32 21 6 3 9 11 16 20 21 32 41 7 3 9 11 16 20 21 32 41 border unsorted sorted selected 10

Selection sort function selectionsort(arr, left, right) for (var i = left; i < right; ++i) var min = i; for (var j = i; j < right; ++j) if (arr[min] > arr[j]) min = j; Finds the index of the smallest element var temp = arr[min]; arr[min] = arr[i]; arr[i] = temp; Swaps the border element with the smallest element 11 Selection sort Efficiency: how many comparisons take place? // a portion of the selection sort for (var i = 0; i < arr.length; ++i) var min = i; for (var j = i; j < arr.length; ++j) if (arr[min] > arr[j]) min = j; 12

Selection sort 13 Efficiency: how many comparisons take place? // a portion of the selection sort for (var i = 0; i < arr.length; ++i) Outer loop executes n times where n is the array length. var min = i; for (var j = i; j < arr.length; ++j) if (arr[min] > arr[j]) min = j; Inner loop executes n, n-1, n-2, 1 times depending on i. Selection sort Efficiency: how many comparisons take place? The orange and yellow boxes in the previous slide show the number of comparisons. 14

Selection sort Efficiency: how many comparisons take place? The orange and yellow boxes in the previous slide show the number of comparisons. This is half of an n*n grid. Thus the number of comparisons grows as the square of the array length. 15 Selection sort Efficiency: how many comparisons take place? If n is the length of the array, then the number of comparisons (f(n)) mathematically is approximately: f ( n) = n 2 16

Selection sort Efficiency: how many comparisons take place? If n is the length of the array, then the number of comparisons (f(n)) mathematically is approximately: 2 f ( n) = n A quadratic equation. Thus, selection sort is a quadratic algorithm. 17 Insertion sort Divide array into two parts: sorted left and unsorted right. Insert the border element into the sorted left where it belongs, shifting elements to the right as needed. Repeat until all is sorted. 18

Insertion sort Pass 0 1 2 3 4 5 6 7 0 16 11 21 32 41 20 3 9 1 2 3 4 5 6 7 border unsorted sorted 19 Insertion sort Pass 0 1 2 3 4 5 6 7 0 16 11 21 32 41 20 3 9 1 11 16 21 32 41 20 3 9 2 11 16 21 32 41 20 3 9 3 11 16 21 32 41 20 3 9 4 11 16 21 32 41 20 3 9 5 11 16 20 21 32 41 3 9 6 3 11 16 20 21 32 41 9 7 3 9 11 16 20 21 32 41 border unsorted sorted 20

Insertion sort function insertionsort(arr, left, right) for (var i=left + 1; i<right; ++i) var j; var temp = arr[i]; for (j = i - 1; j >= left && arr[j] > temp; --j) arr[j + 1] = arr[j]; arr[j + 1] = temp; 21 Insertion sort function insertionsort(arr, left, right) for (var i=left + 1; i<right; ++i) var j; var temp = arr[i]; The index i is the border between sorted and unsorted. for (j = i - 1; j >= left && arr[j] > temp; --j) arr[j + 1] = arr[j]; arr[j + 1] = temp; Place the border element where it belongs Moves every element greater than the border one index right. 22

Insertion sort Efficiency: how many element shifts could take place? 23 Bubble sort Divide array into two parts: sorted right and unsorted left. Compare the leftmost element against its neighbor to the right. If the two are out of order, swap them. Do this for each pair in the row. Repeat for each row until all is sorted. 24

Bubble sort Pass 0 1 2 3 4 5 6 7 0 16 11 21 32 41 20 3 9 1 2 3 4 5 6 7 unsorted sorted 25 Bubble sort Pass 0 1 2 3 4 5 6 7 0 16 11 21 32 41 20 3 9 1 11 16 21 32 20 3 9 41 2 11 16 21 20 3 9 32 41 3 11 16 20 3 9 21 32 41 4 11 16 3 9 20 21 32 41 5 11 3 9 16 20 21 32 41 6 3 9 11 16 20 21 32 41 7 3 9 11 16 20 21 32 41 unsorted sorted 26

Bubble sort function bubblesort(arr, left, right) var didswap = true; var last = right - 1; while (didswap) didswap = false; for (var i=left; i<last; ++i) if (arr[i] > arr[i + 1]) didswap = true; --last; // swap arr[i] with arr[i + 1] (omitted) 27 Bubble sort 28 function bubblesort(arr, left, right) var didswap = true; Loop terminates when var last = right - 1; we don t swap anything while (didswap) in a pass (i.e. is sorted). didswap = false; for (var i=left; i<last; ++i) if (arr[i] > arr[i + 1]) didswap = true; --last; // swap arr[i] with arr[i + 1] (omitted) Always correctly place the largest element at index last. If they re out of order, swap them and set the flag to true

Bubble sort Efficiency: how many swaps could take place? 29 Profiling sorting algorithms Timing each sorting algorithm on the same set of data multiple times. Plot the data and draw some conclusions. 30

Profiling sorting algorithms Code in the course: SortTimings.html Elements Insertion Selection Bubble 200 16 14 31 400 62 47 125 800 94 78 250 1600 297 375 953 3200 1266 1562 3782 6400 5297 6422 15453 12800 20517 25626 62160 31 70,000 60,000 Insertion Selection Bubble 50,000 Time (milliseconds) 40,000 30,000 20,000 10,000 0 0 2,000 4,000 6,000 8,000 10,000 12,000 14,000 Number of elements 32

70,000 Insertion 60,000 Selection Bubble Time (milliseconds) 50,000 40,000 30,000 20,000 10,000 0 0 2,000 4,000 6,000 8,000 10,000 12,000 14,000 All these algorithms are slow, however, insertion sort is best of the worst because it moves the least data. Likewise, bubble sort is the worst of the worst because it moves the most data. Number of elements 33 ITEC 136 Business Programming Concepts Week 12, Part 03 Multi-dimensional arrays 34

Multi-dimensional Arrays Multi-dimensional arrays 35 Higher dimensions 1-D: consists of columns 2-D: rows and columns 3-D: depth, rows, and columns 4-D: No 3-space corollary Key idea: 1D arrays are processed with a single loop. 2D arrays are processed with nested loops Multi-dimensional Arrays Multi-dimensional arrays Multi-dimensional arrays are arrays that contain other arrays as data. Ex: matrix in mathematics var matrix = [ [ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10], [11, 12, 13, 14, 15] ]; alert(matrix.length + " by " + matrix[0].length); 36

Multi-dimensional Arrays Multi-dimensional arrays function makematrix(rows, cols) var result = new Array(rows); for (var i = 0; i < rows; ++i) result[i] = new Array(cols); return result; 37 Multi-dimensional Arrays Multi-dimensional arrays function makematrix(rows, cols) var result = new Array(rows); for (var i = 0; i < rows; ++i) result[i] = new Array(cols); return result; An array gets placed inside another array. 38

Multi-dimensional Arrays Multi-dimensional arrays function multiplicationtable(rows, cols) var matrix = makematrix(rows, cols); for (var i = 0; i < matrix.length; ++i) for (var j = 0; j < matrix[i].length; ++j) matrix[i][j] = (i + 1) * (j + 1); return matrix; 39 Multi-dimensional Arrays Multi-dimensional arrays function multiplicationtable(rows, cols) var matrix = makematrix(rows, cols); for (var i = 0; i < matrix.length; ++i) for (var j = 0; j < matrix[i].length; ++j) matrix[i][j] = (i + 1) * (j + 1); return matrix; Use double subscripts to access the element This is the number of rows This is the number of columns within a row 40

Multi-dimensional Arrays Multi-dimensional arrays Key idea: 1D arrays are generally processed with a single loop. 2D arrays are generally processed with nested loops. 41 Questions? 42

Next Week Object-based based programming Constructors! Properties! Methods! Oh my! Exception handling 43 ITEC 136 Business Programming Concepts Week 12, Part 04 Self Quiz 44

Self Quiz Write a function issorted that receives an array as a parameter and returns true if the array is in sorted order, and false otherwise. Write a function randomize that takes an array as a parameter and swaps randomly selected elements in the array. 45 Self Quiz Write a function addmatrix that takes two 2D arrays of the same dimension as parameters. It should return a new array with matching dimensions containing the sum i.e. 4 1 5 2 3 8 10 6 14 3 9 7 4 4 0 1 18 4 14 9 7 12 0 7 46

Self Quiz Write a function transpose that takes a 2D array as a parameter and returns an new array with the rows and columns exchanged. i.e. 11 12 13 14 21 22 23 24 11 21 12 22 13 23 14 24 47 Self Quiz Demonstrate on paper the sequence of passes through bubble sort, insertion sort, and selection sort for the following array of data 0 1 2 3 4 5 6 7 8 9 10 11 10 2 3 6 9 1 5 4 12 8 7 11 48

Self Quiz How would you reverse the order (ascending vs. descending) of the sorting functions studied this week? If an insertion sort of 100,000 elements takes 14 seconds, how long would a sort of 300,000 elements take? 49 ITEC 136 Business Programming Concepts Week 12, Part 05 Upcoming deadlines 50

Upcoming Deadlines Due March 30 Pre-class exercise 13 Homework 10 51