Week 13 - Monday What did we talk about last time? Big Oh notation Searching Linear search Binary search Lab 12 The importance of sorting should be evident to you by now Applications: Sorting a column in Excel Organizing your iTunes playlists by artist name Ranking a high school graduating class Finding a median score to report on an exam Countless others… And of course, we need to be able to sort in order to make binary search work Yes! It’s tricky No, it’s not! Give me 100 names written on 100 index cards and I can sort them, no problem One way to remind yourself that it’s tricky is by increasing the problem size What if I gave you 1,000,000 names written on 1,000,000 index cards You might need some organizational system Oh, yes, and there’s that mantra of this class A computer can’t “jump” to the M section, unless you explicitly create an M section or something For most common sorts, the computer has to compare two numbers (or Strings or whatever) at a time Based on that comparison, it has to take another step in the algorithm Remember, we have to swap things around in an array It is very simple to understand It is very simple to code It is not very fast The idea is simply to go through your array, swapping out of order elements until nothing is out of order One “pass” of the bubble sort algorithm goes through the array once, swapping out of order elements for( int j = 0; j < array.length - 1; j++ ) if( array[j] > array[j + 1] ) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } Run through the whole array, swapping any entries that are out of order 7 No swap 45 0 Swap 45 0 No swap 54 37 Swap 54 37 No swap 108 51 Swap 108 51 How bad could it be? What if the array was in reversesorted order? One pass would only move the largest number to the bottom We would need n – 1 passes to sort the whole array 6 7 6 57 4 75 4 37 273 217 71 The full Java method for bubble sort would require us to have at least n – 1 passes Alternatively, we could keep a flag to indicate that no swaps were needed on a given pass for( int i = 0; i < array.length – 1; i++ ) for( int j = 0; j < array.length - 1; j++ ) if( array[j] > array[j + 1] ) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } The bubble sort we saw sorts integers in ascending order What if you wanted to sort them in descending order? Only a single change is needed to the inner loop: for( int i = 0; i < array.length – 1; i++ ) for( int j = 0; j < array.length - 1; j++ ) if( array[j] < array[j + 1] ) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } The outer loop runs n – 1 times The inner loop runs n – 1 times The inner loop has a constant amount of work inside of it, call it c (n – 1)(n – 1)c = cn2 – 2cn + c, which is… O(n2) Hmm, not great, let’s try another sort Instead of “bubbling” down the largest (or smallest) number, search through all the numbers to find the smallest, and put it first Then, move on to the next position in the array, and find the smallest number out of those that are left Keep going, always finding the smallest number out of those that are left Look through everything from index 0 to the end, trying to find the smallest value Finally, swap the lowest value with the index we're looking at 7 min 0 45 07 45 0 7 54 54 37 37 108 51 minIndex 0 2 108 51 Visible light is a form of electromagnetic radiation We could think of it as a wave with a specific frequency That is a useful way to think of sound It seems sort of cumbersome for light Color theorists have discovered that we can represent most visible colors as a combination of a small number of set colors One system for representing color is RGB With Red, Green, and Blue components, you can combine them to make most visible colors Combining colors is an additive process: With no colors, the background is black Adding colors never makes a darker color Pure Red added to pure Green added to pure Blue makes White RGB is a good model for computer screens CMYK stands for Cyan, Magenta, Yellow, and Key (Black) CMYK is another color system, but it’s a subtractive system With no colors, the background is white Adding colors never makes a lighter color Pure Cyan added to pure Magenta added to pure Yellow makes Black CMYK is useful for printing, not for computer screens The Color class is how Java keeps track of colors, using an RGB model To use it, you need to type import java.awt.Color; at the top of your program (before the class declaration) Each Color object represents one of 16,777,216 different colors with a value between 0-255 for Red, Green, and Blue Color Red Green Blue Black 0 0 0 Red 255 0 0 Green 0 255 0 Blue 0 0 255 Orange 255 165 0 Gray 128 128 128 Cyan 0 255 255 Magenta 255 0 255 Yellow 255 255 0 White 255 255 255 To create a custom color: Color c = new Color(255,165,0); //orange int green = c.getGreen(); Create colors using the constructor to specify RGB values Get individual values using: getRed() getGreen() getBlue() If the R, G, B values happen to be the same, the color is a shade of gray 255, 255, 255 = White 128, 128, 128 = Gray 0, 0, 0 = Black To convert a color to a shade of gray, use the following formula: Value = .3R + .59G + .11B Based on the way the human eye perceives colors as light intensities We will be thinking of images as 2D arrays or rectangular grids of pixels (each of which has a color stored in a Color object) Bitmaps (.bmp files) are almost that simple Most common image formats (.jpg, .png, and .gif files) are more complex They use different forms of compression to keep the image size small Otherwise, an 800 x 600 image is 3 bytes per pixel x 800 x 600 = 1,440,000 bytes > 1 MB Stands for Joint Photographic Experts Group Good for images without too much high contrast (sharp edges) Photographs are often stored as JPEGs Uses crazy math (discrete cosine transform) to reduce the amount of data needed Lossy compression Good for images with low numbers of colors and high contrast differences Has built-in compression sort of like zip files Similar to the older GIF (.gif) images GIFs are unpopular now because they only support 256 colors GIFs also suffered from legal battles over the algorithm used for compression Lossless compression Because JPEGs and PNGs are complex file types using compression, the authors of the old textbook provided us with the Picture class It’s an easy interface for doing routine things with an image Loading/saving an image Getting the height and width of an image Changing the pixels of an image Showing the image Everything you’d want a picture to do: Method Use Picture(String file) Creates a Picture from a file Picture(int w, int h) Create a blank Picture with width w and height h int width() Return the width of the image int height() Return the height of the image Color get(int x, int y) Return the Color of the pixel at (x,y) void set(int x, int y, Color c) Set the Color of the pixel at (x,y) to c void show() Display the image void save(String file) Save the Picture to a file Write code that will read user input for width and height Make an image that is completely filled with the color blue with the specified width and height Read in the name of an image file from the user Darken the colors in it by 50% Straightforward idea Flip an image around the y-axis Maybe you want to decipher some of Leonardo’s writings No, the other one Given an image with width w and height h: Moving from left to right in the original image, copy each column, storing each column from right to left in the new image Original 0 1 Mirrored 2 0 1 2 0 A 0 A 1 B 1 B 2 C 2 C 3 D 3 D What would the code for mirroring look like? Picture picture = new Picture( file ); //the picture to be mirrored Picture mirrored = new Picture( picture.width(), picture.height() ); for( int i = 0; i < picture.width(); i++ ) for( int j = 0; j < picture.height(); j++ ) mirrored.set( picture.width() - i - 1, j, picture.get( i, j ) ); Pretty much the same thing Instead of copying each column in reverse order, copy each row in reverse order Another straightforward idea Necessary if you are writing software for a digital camera (the user might turn the camera for portrait instead of landscape) Given an image with width w and height h: Create a new image with width h and height w Copy each column in the original image into a row in the new image, remembering to move back along the row 0 0 Original 1 2 Rotated B 1 2 C 2 3 1 2 3 D C B A A 0 1 0 D What would the code for a rotation look like? Picture picture = new Picture( file ); //the picture to be rotated Picture rotated = new Picture( picture.height(), picture.width() ); for( int i = 0; i < picture.width(); i++ ) for( int j = 0; j < picture.height(); j++ ) rotated.set( picture.height() - j - 1, i, picture.get( i, j ) ); Rotating 180°, 270°, -90°, -180°, or -270° can be done using similar techniques or simply performing right rotations multiple times Rotations that are not multiples of 90° are much trickier, result in non-rectangular final images, and need some trigonometry Don’t worry about rotations that are not multiples of 90° You’ve seen this many times before Sometimes an image won’t fit nicely in on a PowerPoint slide Let's just focus on growing, because that's what you need to do in your project If we are just doubling the image, we create a new image whose width and height are twice the original Then, we go through the new image, copying the pixels from the original using pixels whose column and row are half as big as the ones we are putting into the new image Doubling an image Each old pixels maps to four new ones 0 1 0 A A B B 0 1 1 A A B B 0 A B 2 C C D D 1 C D 3 C C D D This kind of resize works, but is crude To shrinking an image, a clever resize might average together pixels When growing an image, different techniques can be done to fill in “guesses” for pixels that sit between pixels from the original image, instead of just duplicating them People who program Photoshop have thought long and hard about how to do these tasks better Finish image manipulation Inheritance Start on Project 5 Read Chapter 11