Programming for Art: Algorithms

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Data Structures Advanced Sorts Part 2: Quicksort Phil Tayco Slide version 1.0 Mar. 22, 2015.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Algorithm Efficiency and Sorting
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Chapter 8 ARRAYS Continued
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 11 Fall 2010.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
Sorting an array bubble and selection sorts. Sorting An arrangement or permutation of data An arrangement or permutation of data May be either: May be.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Data Structures Arrays and Lists Part 2 More List Operations.
1 Applied Arrays Lists and Strings Chapter 12 2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Searching and Sorting Arrays
The Bubble Sort Mr. Dave Clausen La Cañada High School
Chapter 9: Sorting and Searching Arrays
List Algorithms Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Searching and Sorting Arrays
Week 13: Searching and Sorting
Arrays Low level collections.
Sorting Why? Displaying in order Faster Searching Categories Internal
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
Simple Sorting Algorithms
ENEE150 Discussion 13 Section 0101 Adam Wang.
Arrays … The Sequel Applications and Extensions
Arrays & Functions Lesson xx
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Arrays, For loop While loop Do while loop
Peer Instruction 6 Java Arrays.
Describing algorithms in pseudo code
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
List Algorithms Taken from notes by Dr. Neil Moore
Writing Methods AP Computer Science A.
Chapter 8 Search and Sort
Programming for Artists
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Coding Concepts (Basics)
CS Data Structure: Heaps.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Searching and Sorting 1-D Arrays
Search,Sort,Recursion.
Searching.
Search,Sort,Recursion.
Simple Sorting Algorithms
Suggested self-checks: Section 7.11 #1-11
Searching and Sorting Arrays
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Exercise 5 1. We learned bubble sort during class. This problem requires you to modify the code for bubble sorting method to implement the selection sorting.
Linear and Binary Search
Introduction to Sorting Algorithms
Simple Sorting Algorithms
Lec 21 More Fun with Arrays: For Loops
Presentation transcript:

Programming for Art: Algorithms Dr. J. R. Parker Art/Digital Media Lab Lec 15 Fall 2010

Algorithms An algorithm, as we discussed in the first weeks, is a step by step specification of how to solve a problem. We’ve been developing algorithms all along. While creating media artworks, it may be necessary to perform some traditional computing algorithms.

Searching Find something in an array … Whatever it is we’re looking for, we obviously put it there in the first place. An array is a place to put many kinds of things that we might want to find again later.

Basic Search i Problem: Find the array element We have an array of integers. 0 1 2 3 4 5 6 7 2206784 2208750 2201212 2207643 2844707 8621627 2208730 2848556 Problem: Find the array element containing the phone number 8621627 i

Basic Search i=0 Check element 0: is it equal to 8621627? No We have an array of integers. 0 1 2 3 4 5 6 7 2206784 2208750 2201212 2207643 2844707 8621627 2208730 2848556 Check element 0: is it equal to 8621627? No i=0

Basic Search i=1 Check element 1: is it equal to 8621627? No We have an array of integers. 0 1 2 3 4 5 6 7 2206784 2208750 2201212 2207643 2844707 8621627 2208730 2848556 i=1 Check element 1: is it equal to 8621627? No

Basic Search i=2 Check element 2: is it equal to 8621627? No We have an array of integers. 0 1 2 3 4 5 6 7 2206784 2208750 2201212 2207643 2844707 8621627 2208730 2848556 i=2 Check element 2: is it equal to 8621627? No

Basic Search i=3 Check element 3: is it equal to 8621627? No We have an array of integers. 0 1 2 3 4 5 6 7 2206784 2208750 2201212 2207643 2844707 8621627 2208730 2848556 i=3 Check element 3: is it equal to 8621627? No

Basic Search i=4 Check element 4: is it equal to 8621627? No We have an array of integers. 0 1 2 3 4 5 6 7 2206784 2208750 2201212 2207643 2844707 8621627 2208730 2848556 i=4 Check element 4: is it equal to 8621627? No

Basic Search i=5 Check element 5: is it equal to 8621627? We have an array of integers. 0 1 2 3 4 5 6 7 2206784 2208750 2201212 2207643 2844707 8621627 2208730 2848556 i=5 Check element 5: is it equal to 8621627? Yes! We need look no further.

Basic Search This was a search – find the index of the element that has a specified value. Why do it? The index of the phone number in this array could be the index of the person’s name in another. 8621627 Don Knowles Jim Parker Pete Downing Karl Schmidt Kerry Richardson Bill Murray Harry Jarvis Jennifer Wilson

Basic Search In this case, finding the phone number in one array gives us the index that allows us to extract the name of the person having that phone. 8621627 Don Knowles Jim Parker Pete Downing Karl Schmidt Kerry Richardson Bill Murray Harry Jarvis Jennifer Wilson

Search If the array is sorted (in this case ascending order) then the search is more efficient. If the element is the array is bigger then the one we’re searching for then it’s not going to be found. We can stop. 0 1 2 3 4 5 N-1 Indices 1 2 21 98 132 400 981 ... Values

Search for (i=0; i<N; i++) { if (array[i] == target) // The value has been found. result = i; }

Search int search (int target) { result = -1; for (int i=0; i<N; i++) if (array[i] == target) // The value has been found. result = i; } return result;

Search This code is for when the array is in ascending order int search (int target) { result = -1; for (int i=0; i<N; i++) if (array[i] == target) result = I; // The value has been found. else if array[i] > target) return -1; } return result; This code is for when the array is in ascending order

Sort Cause an array of numbers (or strings) to appear in ascending (or descending) order 0 1 2 3 4 5 6 Indices 11 2 21 32 4 43 Values

There are many sorting algorithms. Here’s one: 1. First, find the smallest value in the array. 0 1 2 3 4 5 6 Indices 11 2 21 32 4 43 Values

First, find the smallest value in the array. Sort First, find the smallest value in the array. Starting at 0, search the array for the smallest int. index = 1; smallest = array[0] = 11; 0 1 2 3 4 5 6 Indices 11 2 21 32 4 43 Values

First, find the smallest value in the array. Sort First, find the smallest value in the array. index = 1; Is array[index]<smallest? Yes: smallest=array[index] smallest = array[0] = 11; 0 1 2 3 4 5 6 Indices 11 2 21 32 4 43 Values

First, find the smallest value in the array. Sort First, find the smallest value in the array. index = 2; Is array[index]<smallest? No: add 1 to index smallest = array[0] = 2; 0 1 2 3 4 5 6 Indices 11 2 21 32 4 43 Values

First, find the smallest value in the array. Sort First, find the smallest value in the array. index = 3; Is array[index]<smallest? Yes: smallest=array[index] smallest = array[0] = 2; 0 1 2 3 4 5 6 Indices 11 2 21 32 4 43 Values

First, find the smallest value in the array. Sort First, find the smallest value in the array. index = 4; Is array[index]<smallest? No: increment index smallest = array[3] = 0; 0 1 2 3 4 5 6 Indices 11 2 21 32 4 43 Values

First, find the smallest value in the array. Sort First, find the smallest value in the array. index = 5; Is array[index]<smallest? No: increment index smallest = array[3] = 0; 0 1 2 3 4 5 6 Indices 11 2 21 32 4 43 Values

First, find the smallest value in the array. Sort First, find the smallest value in the array. index = 6; Is array[index]<smallest? No: increment index smallest = array[3] = 0; 0 1 2 3 4 5 6 Indices 11 2 21 32 4 43 Values

Sort At this point, we’re done. index = 7, N = 6 so index>N The variable smallest holds the smallest value in the array, now = 0. (is it smallest?) Where in the array does this belong? smallest = array[3] = 0; 0 1 2 3 4 5 6 11 2 21 32 4 43

Sort The smallest value in the array belongs in array[0]. (If it is in ascending order) So: array[0] = smallest except that we would lose the old value in array[0]. Where do we put that? Can’t just discard it. smallest = array[3] = 0; 0 1 2 3 4 5 6 11 2 21 32 4 43

Sort The smallest value in the array belongs in array[0]. (If it is in ascending order) So: array[0] = smallest except that we would lose the old value in array[0]. Where do we put that? Can’t just discard it. 0 1 2 3 4 5 6 11 2 21 32 4 43

Sort We could put the smallest value in array[0] and the old value that was in array[0] into the place where the smallest came from. … but we goofed. We know what the smallest value is, but not where we got it. We did not save the index of where we found it. Where do we put that? Can’t just discard it. 11 0 1 2 3 4 5 6 2 21 32 4 43

Sort So let’s change the process. Instead of keeping the smallest value, keep track of where it was – what was its index in the array?

Sort smallest initially 0 index = 1; Is array[index]<array[smallest]? Yes: smallest=index smallest = 0, array[smallest] = 11; 0 1 2 3 4 5 6 11 2 21 32 4 43

Sort index = 2; Is array[index]<array[smallest]? No: add 1 to index smallest = 1 array[1] = 2; 0 1 2 3 4 5 6 11 2 21 32 4 43

Sort index = 3; Is array[index]<array[smallest]? Yes: smallest=index smallest = 1 array[1] = 2; 0 1 2 3 4 5 6 11 2 21 32 4 43

Sort index = 4; Is array[index]<array[smallest]? No: increment index smallest =3 array[3]= 0; 0 1 2 3 4 5 6 11 2 21 32 4 43

Sort index = 5; Is array[index]<array[smallest]? No: increment index smallest =3 array[3] = 0; 0 1 2 3 4 5 6 Indices 11 2 21 32 4 43 Values

Sort index = 6; Is array[index]<array[smallest]? No: increment index smallest =3 array[3] = 0; 0 1 2 3 4 5 6 11 2 21 32 4 43

Exchange Now we know where the smallest element is (smallest) and where the initial element is (0) Exchange the values. This can be done like so: 1. Temp = array[index] Index smallest temp 11 0 1 2 3 4 5 6 11 2 21 32 4 43

Exchange Now we know where the smallest element is (smallest) and where the initial element is (0) Exchange the values. This can be done like so: 2. array[index] = array[smallest] Index smallest temp 11 0 1 2 3 4 5 6 2 21 32 4 43

Exchange Now we know where the smallest element is (smallest) and where the initial element is (0) Exchange the values. This can be done like so: 3. array[smallest] = temp Index smallest temp 11 0 1 2 3 4 5 6 2 21 11 32 4 43

Exchange Now the smallest element is in the intial spot, and all array elements are still in the array. We are one step closer to sorting the entire array. Index smallest temp 11 0 1 2 3 4 5 6 2 21 11 32 4 43

Code smallest=0; for (i=1; i<N; i++) // Find smallest if (array[i] < array[smallest]) smallest = i; // exchange first for smallest temp = array[i]; array[i] = array[smallest]; array[smallest] = temp; This only puts one element in order (the first). Now what?

Now we do the rest. What we have is a description of how to replace the first element of an array by the smallest, while keeping all elements. The first element is now correct, The rest of the array can be handled in the same way as we did the first: thinking about elements 1..N as an array, find the smallest and exchange array[1] for that one. Then again and again …

Sort Ignore element 0 and start at 1, repeating the entire process smallest=1 index = 2; Is array[index]<array[smallest] ? No: increment index smallest = 1, array[smallest] = 2; 0 1 2 3 4 5 6 2 21 11 32 4 43

Sort Ignore element 0 and start at 1, repeating the entire process smallest=1 index = 3; Is array[index]<array[smallest] ? No: increment index smallest = 1, array[smallest] = 2; 0 1 2 3 4 5 6 2 21 11 32 4 43

Sort Ignore element 0 and start at 1, repeating the entire process smallest=1 index = 4; Is array[index]<array[smallest] ? No: increment index smallest = 1, array[smallest] = 2; 0 1 2 3 4 5 6 2 21 11 32 4 43

Sort Ignore element 0 and start at 1, repeating the entire process smallest=1 index = 5; Is array[index]<array[smallest] ? No: increment index smallest = 1, array[smallest] = 2; 0 1 2 3 4 5 6 2 21 11 32 4 43

Sort Ignore element 0 and start at 1, repeating the entire process smallest=1 index = 6; Is array[index]<array[smallest] ? No: increment index smallest = 1, array[smallest] = 2; 0 1 2 3 4 5 6 2 21 11 32 4 43

Sort And we’re done Turns out that element in location 1 was the smallest all along. Exchanging it is not necessary, but would do no harm. smallest = 1, array[smallest] = 2; 0 1 2 3 4 5 6 2 21 11 32 4 43

Sort Carry on starting from 2 this time. 0 1 2 3 4 5 6 2 21 11 32 4 43 start=2, smallest=2 index=3 repeat the entire process on the array from 2 .. 6 smallest=2 smallest = 2, array[smallest] = 21; 0 1 2 3 4 5 6 2 21 11 32 4 43

Sort Ignore elements 0 and 1 index = 3; Is array[index]<array[smallest] ? Yes: smallest = index smallest = 2, array[smallest] = 21; 0 1 2 3 4 5 6 21 11 32 4 43

Sort Ignore elements 0 and 1 index = 4; Is array[index]<array[smallest] ? No: increment index smallest =3, array[smallest] = 11; 0 1 2 3 4 5 6 21 11 32 4 43

Sort Ignore elements 0 and 1 index = 5; Is array[index]<array[smallest] ? yes: smallest = index smallest =3, array[smallest] = 11; 0 1 2 3 4 5 6 21 11 32 4 43

Sort Ignore elements 0 and 1 index = 5; Is array[index]<array[smallest] ? no: increment index smallest =5, array[smallest] = 4; 0 1 2 3 4 5 6 21 11 32 4 43

Done again Sort Smallest is 5 Exchange the element in 5 for that in the start (2) 0 1 2 3 4 5 6 2 4 11 32 21 43

Sort Repeat Smallest is value 11 at 3 0 1 2 3 4 5 6 2 4 11 32 21 43

Sort Repeat Smallest is value 21 at 5 0 1 2 3 4 5 6 2 4 11 21 32 43

Sort Repeat Smallest is value 32 at 5 0 1 2 3 4 5 6 2 4 11 21 32 43

Sort Repeat Smallest is value 43 at 6 0 1 2 3 4 5 6 2 4 11 21 32 43

// Jim Parker // Art 315 - Sort // Fall 2010 int N = 6; /* Array of N ints to be sorted */ int [] iarray = new int[25]; void setup() { int i; size(300,300); background(184,184,184); /* Initialize array to value in the lecture */ iarray[0] = 11; iarray[1] = 2; iarray[2] = 21; iarray[3] = 0; iarray[4] = 32; iarray[5] = 4; iarray[6] = 43; }

void draw() { // index of smallest element in this pass int smallest = 0; int temp=0; smallest=0; // Outer loop: start array at 'j' for (int j=0; j<N; j++) smallest = j; for (int i=j; i<N; i++) // Find smallest if (iarray[i] < iarray[smallest]) smallest = i; // exchange first for smallest temp = iarray[j]; iarray[j] = iarray[smallest]; iarray[smallest] = temp; } for (int i=0; i<N; i++) print ("("+i+":"+iarray[i]+")"); println();