Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS100J 27 March 2007 Algorithms on arrays Reading: 8.3–8.5 Please punctuate this: Dear John, I want a man who knows what love is all about you are generous.

Similar presentations


Presentation on theme: "1 CS100J 27 March 2007 Algorithms on arrays Reading: 8.3–8.5 Please punctuate this: Dear John, I want a man who knows what love is all about you are generous."— Presentation transcript:

1 1 CS100J 27 March 2007 Algorithms on arrays Reading: 8.3–8.5 Please punctuate this: Dear John, I want a man who knows what love is all about you are generous kind thoughtful people who are not like you admit to being useless and inferior you have ruined me for other men I yearn for you I have no feelings whatsoever when we're apart I can be forever happy will you let me be yours Gloria The searching, sorting, and other algorithms will be on the course website, along with a JUnit testing class for them.

2 2 This is a neat example of the ambiguity that English can cause, if not used properly! We try to use English properly and precisely, but ambiguity tends to creep in because of difference in cultures in which people grow up and simply because of differences of opinion. Read on! Dear John: I want a man who knows what love is all about. You are generous, kind, thoughtful. People who are not like you admit to being useless and inferior. You have ruined me for other men. I yearn for you. I have no feelings whatsoever when we're apart. I can be forever happy -- will you let me be yours? Gloria Dear John: I want a man who knows what love is. All about you are generous, kind, thoughtful people, who are not like you. Admit to being useless and inferior. You have ruined me. For other men, I yearn. For you, I have no feelings whatsoever. When we're apart, I can be forever happy. Will you let me be? Yours, Gloria.

3 3 Today and Thursday Look at horizontal notation for writing assertions about arrays. Develop several methods that process arrays. The idea is to help you learn how to develop algorithms. Write a function to tell whether two arrays are equal. Write a function to copy an array. Write a function to tell whether two DNA sequences are complements of each other. Look at other algorithms that manipulate arrays Look at storing a table of values in a Java array. including adding a value to the table, deleting the last value of the table, deleting some other value from the table. The material on tables is in Sec. 8.4 of course text.

4 4 Horizontal notation for arrays, strings, Vectors Example of an assertion about an array b. It asserts that: 1.b[0..k–1] is sorted (i.e. its values are in ascending order) 2.Everything in b[0..k–1] is ≤ everything in b[k..b.length–1] b = 0 k b.length b = 0 k b.length 1.b[0..k] is sorted (i.e. its values are in ascending order) 2.Everything in b[0..k] is ≤ everything in b[k+1..b.length–1]

5 5 Maintain a table of values in an array int[] b= new int[5000]; // The n values in the table are in b[0..n–1] int n= 0; // 0 ≤ n <= 5000 b table of values this part is unused 0 n b.length As a program is executed, it may have to maintain a table of values, say temperatures, within an array. The table will start out empty; then values will be added to it. We must say where in the array the values are stored. // Add t to the table: b[n]= t; n= n+1; // Delete last element of table // (assuming it exists). n= n –1;

6 6 Maintain a table of values in an array b table of values this part is unused 0 j n b.length // Delete value b[j] from the table. If the order of values in the table doesn’t matter: n= n–1; b[j]= b[n]; If the order of values in table does matter: n= n–1; // Move b[j+1..n] to b[j..n–1] // inv: b[j+1..k–1] have been moved for (int k= j+1; k–1 != n; k= k+1 ) { // Process b[k] (move it down 1 pos.) b[k–1]= b[k]; }

7 7 Find first position of x in array b. x is guaranteed to be in the array. We use the horizontal notation to write assertions about arrays. ? 0 n precondition: b x not here x ? 0 i n postcondition: b x not here ? 0 i n invariant: b The invariant is found easily from the postcondition i= 0; while (i != n && b[i] != x) { i= i + 1; }

8 8 Deoxyribonucleic acid (DNA) is the building block of all life. Each DNA strand consists of two strings of bases twisted together to form a double helix. The 4 possible bases are represented by G, A, T and C. In a helix, A and T bond together, as do C and G. The two sequences in a helix are complements. For example, these two sequences are complements of each other: sequence 1:ACGTTAC sequence 2:TGCAATG Paired bases meet at an angle. DNA is a very large molecule; the image shows only a tiny fraction of the typical molecule. For the bacterium Escherichia coli, the image would be 80 kilometers long. For a typical piece of DNA from an eukaryote cell, the image would stretch from Dallas to Washington, D. C.! DNA is not fully stretched out inside a cell but is wound around proteins, which protect the DNA. Taken from www.ucmp.berkeley.edu/glossary/gloss3/dna.html

9 9 Linear search.Vague spec.: find first occurrence of v in b[h..k-1]. Better spec.: Store an integer in i to truthify: postcondition: (0) v is not in b[h..i-1] (1) Either i= k or v = b[k] invariant: v is not in b[h..i-1] Finding the min. Vague spec.: Find the min of b[h..k] Better spec.: Precondition: h <= k (because an empty set of values has no min) Store in i to truthify: postcondition: b[m] is the min of b[h..k] (and it is first occurrence of the min) invariant: b[m] is the min of b[h..t-1] (and it is first occur. of the min) Binary search: Vague spec: Look for v in sorted array segment b[h..k]. Better spec: Precondition: b[h..k] is sorted (in ascending order). Store in i to truthify: postcondition: b[h..i] <= v and v < b[i+1..k] invariant: b[h..i] <= v and v < b[j..k]

10 10 Dutch national flag.Vague spec.: b[0..n-1] contains only red, white, blue balls. Sort it using only swaps. Better spec.: Precondition: n >= 0 Permute b[0..n–1] to truthify: postcondition: b[0..h–1] are red balls b[h..k–1] are white balls b[k..n–1] are blue balls precondition: postcondition: invariant : reds whites ? blues 0 h k j n ? 0 n reds whites blues 0 h k n

11 11 Partition algorithm: Permute the values of the array and store a value in j to truthify: x ? h k pre: ≤ x x ≥ x h j k post: x ≤ x ? ≥x h i j k inv:

12 12 Sorting: ? h k pre: sorted h k post: ≤ b[i..k], sorted ≥b[h..i-1], ? h i k selectionsort inv: sorted ? h i k insertionsort inv: Quicksort will be on the course website


Download ppt "1 CS100J 27 March 2007 Algorithms on arrays Reading: 8.3–8.5 Please punctuate this: Dear John, I want a man who knows what love is all about you are generous."

Similar presentations


Ads by Google