Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPUTER 2430 Object Oriented Programming and Data Structures I

Similar presentations


Presentation on theme: "COMPUTER 2430 Object Oriented Programming and Data Structures I"— Presentation transcript:

1 COMPUTER 2430 Object Oriented Programming and Data Structures I

2 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)

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

4 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

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

6 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 ++)

7 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];

8 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.

9 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

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

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

12 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

13 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

14 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

15 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

16 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,

17 Counting: How Many Items?
(n-3) + (n–2) + (n-1) + n n (n-3) + (n–2) + (n-1) n (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

18 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

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

20 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 +

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

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

23 Quiz 5 Wednesday, Nov 28 Sorting Big Os Counting

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


Download ppt "COMPUTER 2430 Object Oriented Programming and Data Structures I"

Similar presentations


Ads by Google