Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 13 - Monday CS 121.

Similar presentations


Presentation on theme: "Week 13 - Monday CS 121."— Presentation transcript:

1 Week 13 - Monday CS 121

2 Last time What did we talk about last time? Big Oh notation Searching
Linear search Binary search Lab 12

3 Questions?

4 Project 5

5 Sorting 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

6 But, is it interesting? 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

7 Computers are stupid 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

8 Bubble Sort

9 Bubble sort is a classic sorting algorithm
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

10 Code for a single pass 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; }

11 Single pass example Run through the whole array, swapping any entries that are out of order No swap 45 Swap No swap 7 45 54 37 108 51 54 37 Swap No swap 108 51 Swap

12 How many passes do we need?
How bad could it be? What if the array was in reverse-sorted order? One pass would only move the largest number to the bottom We would need n – 1 passes to sort the whole array 6 5 4 3 2 7 1 6 5 4 3 2 1 7 6 5 4 3 7 2 1 6 5 7 4 3 2 1 7 6 5 4 3 2 1 6 7 5 4 3 2 1 6 5 4 7 3 2 1

13 Full bubble sort code 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; }

14 Ascending sort 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; }

15 What’s the running time of bubble sort?
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

16 Selection sort

17 Selection 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

18 Selection sort single pass
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 min 7 7 45 54 37 108 51 45 7 54 37 108 51 minIndex 2

19 Project 5

20 Color

21 Light 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

22 RGB 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

23 CMYK 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

24 Color class 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 for Red, Green, and Blue

25 Example colors Color Red Green Blue Black 255 Orange 165 Gray 128 Cyan
255 Orange 165 Gray 128 Cyan Magenta Yellow White

26 To use Color To create a custom color:
Create colors using the constructor to specify RGB values Get individual values using: getRed() getGreen() getBlue() Color c = new Color(255,165,0); //orange int green = c.getGreen();

27 Luminance 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

28 Image Formats

29 Real world image formats
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

30 JPEG or JPG images 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

31 PNG images 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

32 Picture Class

33 Purpose of the Picture class
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

34 Picture methods 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

35 Examples 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%

36 Upcoming

37 Next time… Finish image manipulation Lab 13

38 Reminders Start on Project 5 Read Chapter 11


Download ppt "Week 13 - Monday CS 121."

Similar presentations


Ads by Google