CS 1371 Computing for Engineers

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Searching and Sorting Algorithms Based on D. S
Simple Sorting Algorithms
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Algorithm Efficiency and Sorting
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Analysis of Algorithms
Chapter 19: Searching and Sorting Algorithms
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
3 – SIMPLE SORTING ALGORITHMS
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Elementary Sorting 30 January Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j List[j])
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
WHICH SEARCH OR SORT IS BETTER?. COMPARING ALGORITHMS Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount.
LECTURE 9 CS203. Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search) and sorting (selection sort.
Advanced Sorting.
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Chapter 9: Sorting and Searching Arrays
16 Searching and Sorting.
19 Searching and Sorting.
Searching and Sorting Algorithms
Analysis of Algorithms
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
Recitation 13 Searching and Sorting.
Introduction to complexity
COMP 53 – Week Seven Big O Sorting.
Simple Sorting Algorithms
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
Algorithm Analysis CSE 2011 Winter September 2018.
Teach A level Computing: Algorithms and Data Structures
Topic 14 Searching and Simple Sorts
Efficiency (Chapter 2).
Sorting Algorithms Written by J.J. Shepherd.
CS 3343: Analysis of Algorithms
Linear and Binary Search
Algorithm design and Analysis
Selection sort Given an array of length n,
MSIS 655 Advanced Business Applications Programming
Algorithm Efficiency and Sorting
24 Searching and Sorting.
Algorithmic Complexity
Sub-Quadratic Sorting Algorithms
Topic 14 Searching and Simple Sorts
Quicksort.
Algorithm Efficiency and Sorting
Simple Sorting Algorithms
CPS120: Introduction to Computer Science
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
IST311 - CIS265/506 Cleveland State University – Prof. Victor Matos
Data Structures & Algorithms
CPS120: Introduction to Computer Science
Quicksort.
Simple Sorting Algorithms
Simple Sorting Algorithms
Quicksort.
Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases.
Presentation transcript:

CS 1371 Computing for Engineers Programming in Matlab Environment Dr. Mary Hudachek-Buswell

The Most Important Topic Ever Computation Complexity or Big O

Computation Complexity Measuring the efficiency of an algorithm in terms of space(memory) and time(speed) Efficiency is expressed a function of the input size “Big-O” notation -- O(f(n)) Generally looks at worst case, ie longest time and most space used

Complexity Classes: O(1), O(log n), O(n), O(n log n), O(n^2), O(2^n), O(n!)

Time complexity Tends to be looked at as more important Assume that each operation takes one unit of time to process. Measuring: Additive Property: O(m) + O(n) = O(max(n,m)) Multiplicative Property: Loops the sum of the statements inside the loop

Complexity of Loops Example for i = 1:n for j = 1:n x = x + 1 O(n*n) = O(n^2)

Sorting Algorithms Famous Big O Examples Bubble sort Insertion Sort Selection Sort Merge sort Quick Sort

Bubble Sort Iterate through a list sequentially Compare two adjacent elements, if the first greater than the second, then swap them and continue. Eventually the largest value is moved to the back

Example of Bubble Sort 7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8 2 7 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8 2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 (done) 2 7 5 8 4 2 5 4 7 8 2 7 5 4 8 O(n^2)

Selection Sort Given an array of length n, Search elements 1 through n and select the smallest Swap it with the element in location 1 Search elements 2 through n and select the smallest Swap it with the element in location 2 Search elements 3 through n and select the smallest Swap it with the element in location 3 Continue in this fashion until there’s nothing left to search

Example and analysis of Selection Sort The Selection Sort might swap an array element with itself--this is harmless, and not worth checking for Analysis: The outer loop executes n-1 times The inner loop executes about n/2 times on average (from n to 2 times) Work done in the inner loop is constant (swap two array elements) Time required is roughly (n-1)*(n/2) You should recognize this as O(n2) 7 2 8 5 4 2 7 8 5 4 2 4 8 5 7 2 4 5 8 7 2 4 5 7 8

Insertion sort Sequentially moves through the list. Examines an element and inserts it into the previously sorted list at the front of the list

One step of insertion sort 3 4 7 10 55 9 23 28 16 12 14 20 21 33 38 sorted next to be inserted 10 temp less than 10 12 14 14 20 21 33 38 10 12 14 20 21 33 38 sorted O(n^2)

Analysis of insertion sort We run once through the outer loop, inserting each of n elements; this is a factor of n On average, there are n/2 elements already sorted The inner loop looks at (and moves) half of these This gives a second factor of n/4 Hence, the time required for an insertion sort of an array of n elements is proportional to n2/4 Discarding constants, we find that insertion sort is O(n2)

Merge Sort Merge sort is based on the divide-and- conquer paradigm. Its worst-case running time has a lower order of growth than insertion sort.

Merge Sort O(n log(n))

Typical Exam Question: Match Big O's and Algorithms O(log n) O(n log n) O(n2) 2 Nested for loops Merge Sort Binary search Min value in sorted array

Summary Bubble Sort Insertion Sort Selection Sort Stable Not Stable O(1) extra space O(n2) comparisons O(n2) swaps O(n) swaps Adaptive: O(n) when nearly sorted Not Adaptive Merge Sort Quick Sort Stable O(1) extra space O(n2) comparisons O(n2) swaps Adaptive: O(n) when nearly sorted

Bubble Sort function dataOut = bubbleSort(data) lenD = size(data,2); for i = 1:lenD for j = (lenD):-1:(i+1) if(data(j)<data(j-1)) tmp = data(j); data(j)=data(j-1); data(j-1)=tmp; end end end dataOut = data;

Selection Sort function list = selectionSort(list) listSize = numel(list); for i = (1:listSize-1) minElem = list(i); minIndex = i; for j = (i:listSize) if list(j) <= minElem minElem = list(j); minIndex = j; end if i ~= minIndex list([minIndex i]) = list([i minIndex]); %Swap end %for end %selectionSort

Insertion Sort function dataOut = insertionSort(data) len = size(data,2); for j = 2:len; key = data(j); i = j-1; while(i>0 && data(i)>key) data(i+1) = data(i); i = i-1; data(i+1)=key; end end dataOut = data;

Merge Sort function dataOut = mergeSort(data) lenD = size(data,2); if(lenD<2) dataOut = data; else middle = cast(floor(lenD/2),'uint16'); L = data(1:middle); R = data(middle+1:end); L = mergeSort(L); R = mergeSort(R); dataOut = merge(L, R); end function dataOut = merge(L,R) lenL = size(L,2); lenR = size(R,2); i = 0; j = 0; merged = []; while(i<lenL||j<lenR) if (i<lenL && j<lenR) if(L(i+1)<=R(j+1)) merged(i+j+1) = L(i+1); i = i+1; else merged(i+j+1) = R(j+1); j = j+1; end elseif(i<lenL) merged(i+j+1) = L(i+1); i = i+1; elseif(j<lenR) merged(i+j+1) = R(j+1); j = j+1; end dataOut = merged;

Quick Sort function arraysort = quickSort(array) if length(array)<=1 arraysort = array return end pivot = array(end) array(end) = [] smaller = array(array<=pivot) larger = array(array>pivot) arraysort = [quickSort(smaller) pivot quickSort(larger)]

http://math.hws.edu/eck/jsdemo/sortlab.html