MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

28
MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures

Transcript of MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

Page 1: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 1

MIS 215 Module 0Intro to Data Structures

Page 2: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 2

Where are we?

Intro toJava, Course

Java lang.basics

Arrays

Introduction

Newbie Programmers Developers ProfessionalsDesigners

MIS215

Binary Search

SearchTechniques Sorting Techniques

Bubblesort

Basic Algorithms

Fast Sorting algos(quicksort, mergesort)

Hashtables

Graphs,Trees

LinkedLists

Stacks,Queues

List Structures Advanced structures

Page 3: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 3

Today’s Buzzwords Data Structures Algorithms Efficiency of algorithms Analysis of algorithms Programming = Data Structures + Algorithms Programming as a problem-solving method

Elluminate

Page 4: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

Nuts and Bolts of this Class Elluminate

A tool for Synchronous Online Collaboration This course will be all about collaboration Start by clicking the link in the Course Calendar in

WebCT Allow Elluminate to install Do not click on the microphone while in class! (believe

me – bad things will happen if you do)

Dr. Java and JDK 1.5

MIS215 Module 0 - Intro 4

Page 5: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

Elluminate features we will use Definitely:

Private and moderator chatsDrawing boardPolling and quizzing

Possibly:Project presentationsOut-of-class meetingsOnline office hours

MIS215 Module 0 - Intro 5

Page 6: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 6

Introduction to Data Structures What are data structures/ruptures & algorithms

good for? Overview of data structures. Overview of algorithms Some definitions OO programming S/W engineering Java Library Data Structures

Page 7: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 7

What’s a program? So, far, if you’ve only done CS

208/209/equivalent, what has a program been?

Probably, just doing some simple data manipulation..store some grades in an arrayaverage the grades, find min/maxanything else??

Page 8: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 8

Overview of Data Structures

Arrays (unordered and ordered) Stacks, Queues, Lists

why did I lump these all together? Trees (binary, red-black [?], 2-3-4 [?]) Hash table Heap

Page 9: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 9

Our approach to Problem Solving this quarter

Dr. Java Integrated Development Environment Edit – Compile – Test – Debug Unfortunately CaTS won’t install in labs Fortunately it doesn’t take much to install Lets use Dr. Java to write our first program

Page 10: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 10

HelloWorld.java

public class HelloWorld {

public void run() { System.out.println("Hello Java World!"); }

public static void main (String [] args) { HelloWorld tpo = new HelloWorld(); tpo.run(); }}

Page 11: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 11

Dissecting HelloWorld

Every program in Java is (at least) one class Each class has one or more methods. The class that runs the program is the main

class, and has a main(…) method Typically, you should create an instance of a

class in your main method, and call methods of that instance

That’s exactly what HelloWorld Does!

Page 12: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 12

Lets do a little more…public class GradeCalculator {

private int score = 843; // Score of the student

public void run() {

char letgrade = ''; //single quotes for characters

// Do what is needed here to find the letter grade

System.out.println("The student's letter grade is" +

letgrade);

}

public static void main (String [] args) {

GradeCalculator gc = new GradeCalculator();

gc.run();

}

}

Page 13: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 13

Dissecting GradeCalculator

First finish it .. What is the logic for finding the letter

grade from the numeric score? What is that “private int score;” business? What are our alternatives?

Page 14: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 14

The Input Class A stable class for easily receiving input from the

users Documentation of this class is available at

http://www.wright.edu/~arijit.sengupta/mis215/samples/complexity/Input.html

How do you use it? Create an instance of the Input class Call the appropriate method (with or without an

argument) Why should you use the Input class?

Stable – includes some built-in error checks!

Page 15: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 15

Lets try it! Change GradeCalculator to ask the user

for the numeric score Where should you create the Input class? What happens if you call the run() method

multiple times? What is a better way?

Page 16: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 16

Going ahead – practising loops

Write a program that asks the user to guess a number between 1 and 100.

In a loop, let the computer make a guess, and the user says G/L/M (greater/less/match)

Continue until you get a match Report the number of tries

Page 17: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 17

CFC – Comment-First Coding What is it? Why comments?

Descriptive Think in English instead of the cryptic code Will always compile!!! Self-documenting

Why comment-first? How do you fill out the comments? Lets try it!

Page 18: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 18

Only the run() method now..public void run() { // Step 1. Initialization

// Step 2.

// Step 3.

// Step 4.

// Step 5.

}

Page 19: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 19

Lets describe the stepspublic void run() { // Step 1. Initialization // set up the needed variables

// Step 2.

// Step 3.

// Step 4.

// Step 5.

}

Page 20: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 20

Finally, fill in the codepublic void run() { // Step 1. Initialization // set up the needed variables int steps = 0, guess = 0; // any other variables? // Step 2.

// Step 3.

// Step 4.

// Step 5.

}

Page 21: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 21

Now what? Practice CFC – we will try to use this

diligently this quarter. Every assignment you submit must have

your names as comments at the top of the file, and all code must be extensively commented, preferably CFC-ed

Did I just make up a verb there?

Page 22: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

MIS215 Module 0 - Intro 22

Finally…

Lets try to have fun as you write code Remember, programs do what we tell them to do

– so we are the boss! There is nothing like “IT is not working” – IT

always works – just like you tell it to – maybe you didn’t tell it to do the right thing!

Practice, practice, practice… makes it perfect

Page 23: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

Part 2 – Analysis of Algorithms There are many different ways to solve the

same problem How do you tell if one way would be better

than another? Analysis of algorithms is a method for

identifying the efficiency of an algorithm

MIS215 Module 0 - Intro 23

Page 24: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

Order Notation (big-Oh/little-oh) For this course, we are only going to use

the big Oh notation An algorithm is described to have the

complexity of O(some function over n) For example, Mergesort has the

complexity O(n lg n) No – that wasn’t a typo – lg(n) is log2(n)

MIS215 Module 0 - Intro 24

Page 25: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

So what does it mean? Lets first see how these functions behave:

MIS215 Module 0 - Intro 25

n n2 lg(n) n lg(n) 2n n!

10 100 4 34 1024 3628800

100 10000 7 665 1.26765E+30 9.3326E+157

1000 1000000 10 9966 1.0715E+301 #NUM!

5000 25000000 13 61439 #NUM! #NUM!

10000 100000000 14 132878 #NUM! #NUM!

50000 2500000000 16 780483 #NUM! #NUM!

100000 10000000000 17 1660965 #NUM! #NUM!

Page 26: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

So.. Can you compare now? Complexity of Bubblesort is O(n2), and

complexity of Mergesort is O(n lg n). Which one is more efficient?A. Bubblesort

B. Mergesort

C. Both are the same

MIS215 Module 0 - Intro 26

Page 27: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

How do you determine complexity of an algorithm? Typically involves mathematical induction For the purpose of this class, try to find the

main loop of the algorithm, and determine the number of comparisons (or whichever is the most important operation is)

Try to find an association between the input and the number of operations

MIS215 Module 0 - Intro 27

Page 28: MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.

The ComplexityDemo.java program Compile and run it – don’t worry too much

about the source code at the moment See how the time behaves as you run the

different algorithms If you feel adventurous, change the code a

bit and try different input numbers (careful – some will not run for large

inputs)MIS215 Module 0 - Intro 28