Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming for Art: Algorithms

Similar presentations


Presentation on theme: "Programming for Art: Algorithms"— Presentation transcript:

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

2 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.

3 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.

4 Basic Search i Problem: Find the array element
We have an array of integers. Problem: Find the array element containing the phone number i

5 Basic Search i=0 Check element 0: is it equal to 8621627? No
We have an array of integers. Check element 0: is it equal to ? No i=0

6 Basic Search i=1 Check element 1: is it equal to 8621627? No
We have an array of integers. i=1 Check element 1: is it equal to ? No

7 Basic Search i=2 Check element 2: is it equal to 8621627? No
We have an array of integers. i=2 Check element 2: is it equal to ? No

8 Basic Search i=3 Check element 3: is it equal to 8621627? No
We have an array of integers. i=3 Check element 3: is it equal to ? No

9 Basic Search i=4 Check element 4: is it equal to 8621627? No
We have an array of integers. i=4 Check element 4: is it equal to ? No

10 Basic Search i=5 Check element 5: is it equal to 8621627?
We have an array of integers. i=5 Check element 5: is it equal to ? Yes! We need look no further.

11 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. Don Knowles Jim Parker Pete Downing Karl Schmidt Kerry Richardson Bill Murray Harry Jarvis Jennifer Wilson

12 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. Don Knowles Jim Parker Pete Downing Karl Schmidt Kerry Richardson Bill Murray Harry Jarvis Jennifer Wilson

13 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. N Indices 1 2 21 98 132 400 981 ... Values

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

15 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;

16 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

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

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

19 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; Indices 11 2 21 32 4 43 Values

20 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; Indices 11 2 21 32 4 43 Values

21 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; Indices 11 2 21 32 4 43 Values

22 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; Indices 11 2 21 32 4 43 Values

23 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; Indices 11 2 21 32 4 43 Values

24 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; Indices 11 2 21 32 4 43 Values

25 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; Indices 11 2 21 32 4 43 Values

26 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; 11 2 21 32 4 43

27 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; 11 2 21 32 4 43

28 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. 11 2 21 32 4 43

29 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 2 21 32 4 43

30 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?

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

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

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

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

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

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

37 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 11 2 21 32 4 43

38 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 2 21 32 4 43

39 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 2 21 11 32 4 43

40 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 2 21 11 32 4 43

41 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?

42 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 …

43 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; 2 21 11 32 4 43

44 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; 2 21 11 32 4 43

45 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; 2 21 11 32 4 43

46 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; 2 21 11 32 4 43

47 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; 2 21 11 32 4 43

48 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; 2 21 11 32 4 43

49 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 smallest=2 smallest = 2, array[smallest] = 21; 2 21 11 32 4 43

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

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

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

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

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

55 Sort Repeat Smallest is value 11 at 3 2 4 11 32 21 43

56 Sort Repeat Smallest is value 21 at 5 2 4 11 21 32 43

57 Sort Repeat Smallest is value 32 at 5 2 4 11 21 32 43

58 Sort Repeat Smallest is value 43 at 6 2 4 11 21 32 43

59 // Jim Parker // Art 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; }

60 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();


Download ppt "Programming for Art: Algorithms"

Similar presentations


Ads by Google