Armstrong Atlantic State University Engineering Studies MATLAB Marina Sorting Primer

Similar documents
Sorting Pearson Education, Inc. All rights reserved.

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

Chapter 8 Algorithms 1

8 Algorithms 8.1. Foundations of Computer Science Cengage Learning

Armstrong State University Engineering Studies MATLAB Marina Switch-Case Statements Primer

Chapter 2: Complexity Analysis

CSC 273 Data Structures

Comparison of x with an entry in the array

LECTURE 17. Array Searching and Sorting

Programming II (CS300)

Armstrong State University Engineering Studies MATLAB Marina 2D Arrays and Matrices Primer

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

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

CS 103 Unit 8b Slides

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt

Measuring algorithm efficiency

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

Lecture 6 Sorting and Searching

Algorithms and Applications

Programming II (CS300)

COMP Data Structures

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

Lesson 12 - Operator Overloading Customising Operators

Sorting is ordering a list of objects. Here are some sorting algorithms

Same idea, different implementation. Is it important?

CS 2412 Data Structures. Chapter 10 Sorting and Searching

Faster Sorting Methods

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

Analysis of Algorithms

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

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

ECE 2574: Data Structures and Algorithms - Big-O Notation. C. L. Wyatt

Sorting and Searching

Armstrong State University Engineering Studies MATLAB Marina Algorithm Development I Primer

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

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY

Searching, Sorting. Arizona State University 1

Chapter 8 Search and Sort

CS : Data Structures

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

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

Question 7.11 Show how heapsort processes the input:

Overview of Sorting Algorithms

CS126 Final Exam Review

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

Armstrong Atlantic State University Engineering Studies MATLAB Marina 2D Plotting Primer

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

Analysis of Algorithms. CS 1037a Topic 13

Sorting. Hsuan-Tien Lin. June 9, Dept. of CSIE, NTU. H.-T. Lin (NTU CSIE) Sorting 06/09, / 13

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

SORTING AND SEARCHING

Sorting/Searching and File I/O. Sorting Searching Reading for this lecture: L&L

UNIT 3: ANALYSIS OF SIMPLE ALGORITHMS

Data Structures and Algorithms

S1) It's another form of peak finder problem that we discussed in class, We exploit the idea used in binary search.

Sorting and Searching Algorithms

CS61B, Spring 2003 Discussion #15 Amir Kamil UC Berkeley 4/28/03

O(n): printing a list of n items to the screen, looking at each item once.

Subject: Computer Science

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Algorithms. 1. At a high level of abstraction, the algorithm for doing laundry is:

Lecture 19 Sorting Goodrich, Tamassia

CISC 1100: Structures of Computer Science

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

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

Principles of Programming. Chapter 6: Arrays

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

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

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

Searching Algorithms/Time Analysis

LISTS WITH PYTHON. José M. Garrido Department of Computer Science. May College of Computing and Software Engineering Kennesaw State University

Introduction to Computers and Programming. Today

Programming 2. Topic 8: Linked Lists, Basic Searching and Sorting

Sorting & Searching (and a Tower)

CS 103 Lecture 4 Slides

CSCI 104 Log Structured Merge Trees. Mark Redekopp

Hiroki Yasuga, Elisabeth Kolp, Andreas Lang. 25th September 2014, Scientific Programming

Introduction to Analysis of Algorithms

Quick-Sort fi fi fi 7 9. Quick-Sort Goodrich, Tamassia

CmpSci 187: Programming with Data Structures Spring 2015

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms 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

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

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F


Algorithmic Analysis. Go go Big O(h)!

Algorithm Analysis. Big Oh

KF5008 Algorithm Efficiency; Sorting and Searching Algorithms;

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

UNIT-2. Problem of size n. Sub-problem 1 size n/2. Sub-problem 2 size n/2. Solution to the original problem

Computational Complexity: Measuring the Efficiency of Algorithms

Sorting and Searching -- Introduction

Introduction. Sorting. Definitions and Terminology: Program efficiency. Sorting Algorithm Analysis. 13. Sorting. 13. Sorting.

Sorting. Task Description. Selection Sort. Should we worry about speed?

Introduction to the Analysis of Algorithms. Algorithm

Sorting Algorithms Day 2 4/5/17

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Data structure and algorithm in Python

Programming in OOP/C++

Sorting Algorithms. Slides used during lecture of 8/11/2013 (D. Roose) Adapted from slides by

Transcription:

Armstrong Atlantic State University Engineering Studies MATLAB Marina Sorting Primer Prerequisites The Sorting Primer assumes knowledge of the MATLAB IDE, MATLAB help, arithmetic operations, built in functions, scripts, variables, arrays, logic expressions, conditional structures, iteration, functions, debugging, characters and strings, cell arrays, structures, file input and output, and searching. Material on these topics is covered in the MATLAB Marina Introduction to MATLAB module, MATLAB Marina Variables module, MATLAB Marina Arrays module, MATLAB Marina Logic Expressions module, MATLAB Marina Conditional Structures module, MATLAB Marina Iteration module, MATLAB Marina Functions module, MATLAB Marina debugging module, MATLAB Marina Character and Strings module, MATLAB Marina Cell Arrays module, MATLAB Marina Structures module, MATLAB Marina File Input and Output module, and MATLAB Marina Searching module. Learning Objectives 1. Be able to describe the operation of the different types of sorts: selection, insertion, quick, merge, and bucket. 2. Be able to use MATLAB s sort function to sort collections of primitive data types. 3. Be able to write sort functions (selection) for collections of data of non-primitive data types such as cell arrays and structure arrays. Terms sorting, selection sort, quick sort MATLAB Functions, Keywords, and Operators sort Sorting Sorting involves ordering a collection of data in some way. Arranging integers from smallest to largest or largest to smallest and arranging names in alphabetical order are examples of sorting. There are many different types of sorts, some examples of sorts are: selection sort, insertion sort, quick sort, bucket sort, and merge sort. A selection sort works by searching the collection for the next element in the ordering until the entire collection is ordered. Sorting a collection in increasing order would involve: finding the smallest element and placing that in the first spot, then finding the next smallest and placing it in the second spot, and continuing this procedure until all elements have been placed in order. Figure 1 shows an example of a section sort function that sorts an array of numbers in increasing order. 1

function result = selectionsort(data) % selectionsort.m % syntax: result = selectionsort(data) % data is the collection of data to sort % result is ordered list of numbers (increasing order) % Notes: data must be a 1D array of numbers result = data; % search list number times equal to its size for pass = 1 : 1 : length(result)-1 smallindex = pass; % search remaining unordered sublist for next smallest element for k = pass + 1 : 1 : length(result) if (result(k) < result(smallindex)) % update next smallest element if necessary smallindex = k; % put next smallest element in correct place by swapping if (smallindex ~= pass) temp = result(pass); result(pass) = result(smallindex); result(smallindex) = temp; Figure 1, selectionsort function Notice that the selection sort requires a nested loop. The outer loop keeps track of which pass and the inner loop is to search the sublist for the desired value. A selection sort must search the collection a number of times equal to the number of elements minus one, when it finds the next smallest element it will switch it with the value in the current position. For example, to sort the array [17, 4, 13, 9, 2, 10] requires five passes: After the first pass, the partially sorted list is [2, 4, 13, 9, 17, 10] After the second pass, the partially sorted list is [2, 4, 13, 9, 17, 10] After the third pass, the partially sorted list is [2, 4, 9, 13, 17, 10] After the fourth pass, the partially sorted list is [2, 4, 9, 10, 17, 13] And after the fifth pass, the sorted list is [2, 4, 9, 10, 13, 17] 2

Sorting Examples Consider sorting the array [17, 7, 3, 14, 9, 2, 21, 8]: With a selection sort: [17, 7, 3, 14, 9, 2, 21, 8] Pass 1, [ 2, 7, 3, 14, 9, 17, 21, 8 ] Pass 2, [ 2, 3, 7, 14, 9, 17, 21, 8 ] Pass 3, [ 2, 3, 7, 14, 9, 17, 21, 8 ] Pass 4, [ 2, 3, 7, 8, 9, 17, 21, 14 ] Pass 5, [ 2, 3, 7, 8, 9, 17, 21, 14 ] etc. With a merge sort: [17, 7, 3, 14, 9, 2, 21, 8] [ 17, 7, 3, 14 ] [ 9, 2, 21, 8 ] [ 17, 7 ] [ 3, 14 ] [ 9, 2 ] [ 21, 8 ] [17] [7] [3] [14] [9] [2] [21] [8] [7, 17] [3, 14] [2, 9] [8, 21] [3, 7, 14, 17] [2, 8, 9, 21] [2, 3, 7, 8, 9, 14, 17, 21] With a quick sort using the median of sublists as pivot (best case): [ 17, 7, 3, 14, 9, 2, 21, 8 ] [7, 3, 2, 8] [9] [17, 14, 21] [3, 2] [7] [8] [14] [17] [21] [2] [3] [2, 3] [2, 3, 7, 8] [14, 17, 21] [ 2, 3, 7, 8, 9, 14, 17, 21 ] With a quick sort using first element of sublists as pivot: [ 17, 7, 3, 14, 9, 2, 21, 8 ] [7, 3, 14, 9, 2, 8] [17] [21] [3, 2] [7] [14, 9, 8] [2][3] [9, 8] [14] [8][9] [2, 3] [8,9] [8, 9, 14] [2, 3, 7, 8, 9, 14] [ 2, 3, 7, 8, 9, 14, 17, 21 ] A quick sort works well if it gets good pivots for each sublist. The pivot can be the median of the sublist (best case) or could use the first element of list and hope it is close to the median. 3

Estimating Efficiency of Algorithms Big O notation allows one to characterize functions by their growth rates. Big O notation is commonly used to analyze algorithms in terms of utilization of computer resources and can be used to estimate the worst case performance (computing cost in terms of execution time, memory required) of an algorithm indepent of the computer architecture it is run on. The Big O of an algorithm is expressed as O( function of N), where N is size of the collection to be processed. Typically we are concerned with the computation cost as N gets large (approaches infinity). A simplified set of rules for Big O notation is: For sequential operations, the Big O values of each operation are added For nested operations the Big O values of each operation are multiplied If the Big O function is a sum of terms, keep only the term with the largest growth rate If the Big O function is a product of terms, any constants that do not dep upon the size of the data being processed are omitted. O(1) computation cost of the algorithm is indepent of the size of the data, for example accessing an element in an array. O(N) computation cost of the algorithm is proportional to size of the data, for example copying an array. O(N 2 ) computation cost of the algorithm is proportional to the square of the size of the data, for example gamma correction on an image Other Big O examples: finding an item in a sorted list using a binary search is O(log N), finding an item in an unsorted list is O(N), sorting a list using an insertion sort is O(N 2 ), and sorting a list using a quick sort is O(N log N). The MATLAB code segment of Figure 2a generates a multiplication table for up to N*N. The algorithm for the code of Figure 2a is O(N 2 ), the disp operation is performed N 2 times. N = 3; for k1 = 1 : 1 : N for k2 = 1 : 1 : N disp([int2str(k1), '*', int2str(k2), '=', int2str(k1*k2)]); Figure 2a, MATLAB Code to Generate Multiplication Table 4

The MATLAB code segment of Figure 2b displays the elements of an array of size N. The algorithm for the code of Figure 2b is O(N), the disp operation is performed N times. arr = [1:1:N]; for k = 1 : 1 : length(arr) disp(arr(k)); Figure 2b, MATLAB Code to Display the Elements of an Array A somewhat simplistic view of Big O (at least for basic looping algorithms) is that for p nested loops, the algorithm is O(N p ). This simplistic view can overestimate the worst case efficiency. When there is a choice of algorithms to perform the same operation, the more efficient algorithm is generally preferable especially if there are resource constraints such as limited memory, storage, or processor time. For example, the worst case to search a sorted list of 10000 items using a binary search is 10000 comparisons (N = 10000) whereas the worst case for searching the same list using a binary search is 14 comparisons (log2(n) = 13.29). Last modified Monday, November 18, 2013 This work by Thomas Murphy is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License. 5