COMPUTER 2430 Object Oriented Programming and Data Structures I

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks, Queues, and Linked Lists
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Tirgul 3 Topics of this Tirgul: Lists Vectors Stack Queue.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
1 MT258 Computer Programming and Problem Solving Unit 9.
Searching – Linear and Binary Searches. Comparing Algorithms Should we use Program 1 or Program 2? Is Program 1 “fast”? “Fast enough”? P1P2.
Week 2 CS 361: Advanced Data Structures and Algorithms
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
Information and Computer Sciences University of Hawaii, Manoa
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Stacks And Queues Chapter 18.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
CS 2430 Day 30. Announcements Quiz #5: 4/19 Agenda Big O Searching –Linear search.
CSC 201 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
Searching Topics Sequential Search Binary Search.
Computer Engineering Rabie A. Ramadan Lecture 6.
Section 1.7 Comparing Algorithms: Big-O Analysis.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
QueueStack CS1020.
Week 4 - Friday CS221.
Searching – Linear and Binary Searches
Chapter 15 Lists Objectives
Stacks and Queues.
Stack and Queue APURBO DATTA.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CMSC 341 Lecture 5 Stacks, Queues
Lecture 2: Implementing ADTs
Chapter 19: Stacks and Queues.
Building Java Programs
Instructor: Mr.Vahidipour
Priority Queue & Heap CSCI 3110 Nan Chen.
Linked List (Part I) Data structure.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 2430 Object Oriented Programming and Data Structures I
ITEC 2620M Introduction to Data Structures
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Introduction to Programming
COMPUTER 2430 Object Oriented Programming and Data Structures I
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Building Java Programs
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 2430 Object Oriented Programming and Data Structures I
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
Lecture 2: Stacks and Queues
COMPUTER 2430 Object Oriented Programming and Data Structures I
Jeff West - Quiz Section 16
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Priority Queues.
CSCS-200 Data Structure and Algorithms
Presentation transcript:

COMPUTER 2430 Object Oriented Programming and Data Structures I

Sorting Algorithms The orders of algorithms, or the Big Os Selection Sort O(n2) Bubble Sort Insertion Sort Worst Case: O(n2) Average case: O(n2)

Orders of Algorithms (Big Os) The order of an algorithm is the order of the fastest growing term (as n  ). For example, O( 6n3 + 1200 n2 + 100n + 20 ) = O(n3) O( 16n3 - n2 + 2000 ) = O(n3) Different coefficients (6 and 16) are ignored. Less significant terms are ignored Also known as the Time Complexity

A un-sorted array of N elements Examples A un-sorted array of N elements Finding if a target is in the array Worst case is N steps Average is N/2 Both O(N) Finding the max/min value in an array O(N) Calculating the average value Linear Search

A regular array of N elements Examples A regular array of N elements Adding an element at the end A few steps, no need to go over the array Constant time O(1) items[count] = obj; count ++; items[count ++] = obj;

A regular array of N elements Examples A regular array of N elements Removing an element (at position index) and maintaining the order of remaining element by moving up elements O(N) for (int i = index; i < count – 1; i ++) items[i] = items[i + 1]; count --; for (int i = index; i < count; i ++)

A regular array of N elements Examples A regular array of N elements Removing an element (at position index) without maintaining the order of remaining elements O(1) items[index] = items[count - 1]; count --; items[index] = items[-- count];

Big O: O(1) for isEmpty and isFull. public class Stack { private Object[] items; private int top; // top is count public Stack ( int size ) items = new Object[size]; top = 0; } public boolean isFull() return top == items.length; public boolean isEmpty() return top == 0; Big O: O(1) for isEmpty and isFull.

public class Stack { private Object[] items; private int top; // top is count . . . public void push( Object obj ) items[top ++] = obj; } Big O: O(1) for push

public class Stack { private Object[] items; private int top; // top is count . . . public Object pop() return items[-- top]; } Big O: O(1) for pop

Circular Queue int front, rear, count; front points to where the first element is rear points to where next element goes

public class Queue { private Object[] items; private int front, rear, count; ... // The constructor takes the saze (items.length) public Queue ( int size ) items = new Object[size]; front = rear = count = 0; } } // class Queue

public class Queue { private Object[] items; private int front, rear, count; ... public void add ( Object x ) items[rear] = x; rear = (rear + 1) % items.length; count ++; } } // class Queue Big O: O(1) for add

public class Queue { private Object[] items; private int front, rear, count; ... public Object remove () Object obj = items[front]; front = (front + 1) % items.length; return obj; } } // class Queue Big O: O(1) for remove

Big Os O(1): not dependent on the number of items O(log N): much slower than O(N) O(N): linear time O(N * log N) O(N2) . . . O(2N): exponential

Big Os N O(log N) O(N) O(N*log N) O(N2) O(2N) 10 4 (3.3) 40 100 1,024 7 (6.6) 700 10,000 1,02410 1,000 10 (9.9) 1,000,000 1,024100 14 (13.2) 140,000 100,000,000 1,0241000

Counting: How Many Items? 1 + 2 + 3 + ... + (n-3) + (n–2) + (n-1) + n n 1 + 2 + 3 + ... + (n-3) + (n–2) + (n-1) n - 1 5 + 6 + 7 + ... + (n-3) + (n-2) + (n-1) n – 5 = ((n-1)-5) + 1 t + (t+1) + (t+2) + ... + (n-2) + (n-1) + n n – t + 1 = last – first + 1

Step is a non-zero integer Formula How many items? Step is a non-zero integer s s+step s+2*step . . . t-2*step t-step t (t – s) / step + 1 (last – first) / step + 1

Counting: What is the sum? 1 + 2 + 3 + ... + (n-3) + (n–2) + (n-1) S = 1 + 2 + 3 + ... +(n-3)+(n–2)+(n-1) S = (n-1)+(n-2)+(n-3)+ ... + 3 + 2 + 1 2*S = n + n + n + ... + n + n + n = n * (n-1) S = n * (n – 1) / 2 = (first + last) * (number of items) / 2 +

Counting: What is the sum? How many items? n – t + 1 t + (t+1) + (t+2) + ... + (n-2) + (n-1) + n S = t + (t+1) + (t+2) + ... + (n-2) + (n-1) + n S = n + (n-1) + (n-2) + ... + (t+2) + (t+1) + t 2*S = (t+n) + (t+n) + (t+n) + ... + (t+n) + (t+n) + (t+n) = (t+n) * (n-t+1) S = (t + n) * (n – t + 1) / 2 = (first + last) * (number of items) / 2 +

Math Formulas 1 + 2 + 3 + .... + r = (1 + r) * r / 2 t + (t+1) + (t+2) + .... + r = (t+r) * (r-t+1) / 2 = (first + last) * (number of items) / 2

Turn in Monday in class Five Bonus Points Exercise Counting Turn in Monday in class Five Bonus Points

Quiz 5 Wednesday, Nov 28 Sorting Big Os Counting

Due Monday, Nov 26 Grace Wednesday, Nov 28 (-5) Prog 5 Due Monday, Nov 26 Grace Wednesday, Nov 28 (-5)