Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 2430 Object Oriented Programming and Data Structures I

Similar presentations


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

1 CS 2430 Object Oriented Programming and Data Structures I
Day1 on Thursday, before lab1

2 Quiz 4

3 Where is the LAST element?
front rear rear - 1 front rear rear - 1 rear front rear – 1?

4 Quiz 4 public Object getLastElement() { if (count == 0) return null; int last; if (rear == 0) last = items.length - 1; else last = rear – 1; rear = last; count --; return items[last]; }

5 Quiz 4 public Object getLastElement() { if (count == 0) return null; rear –-; if (rear == -1) rear = items.length - 1; count --; return items[rear]; }

6 Without IF public Object getLastElement() { if (count == 0) return null; rear = (rear – 1) % items.length; // What do we get from (0 – 1) % items.length? // -1! count --; return items[rear]; }

7 Without IF public Object getLastElement() { if (count == 0) return null; rear = (rear – 1 + items.length) % items.length; count --; return items[rear]; }

8 public Object removeAndReturnLastElement(Queue theQ, int queueSize) { Queue tq = new Queue(queueSize); if (theQ.isEmpty()) return null; Object obj = theQ.remove(); // is obj the last? while (! theQ.isEmpty()) // No tq.add (obj); obj = theQ.remove(); } while (! tq.isEmpty()) theQ.add(tq.remove); return obj;

9 Exception throw new Exception(“F1: N”); // Not Output System.out.println(“In f1”); // Output

10 Output Caught in f2 Main bottom X: 3 Y: 2 In f2 In f1 (false) Caught in Main X: 4 Y: 3 (true) In f1 (false) Caught in Main X: 2 Y: 2 In f2

11 Generic Interface Comparable
public interface Comparable <E> { public int compareTo ( E x ); } Return value: negative if (this) less than x 0 if (this) equal to x positive if (this) greater than x

12 Class Date public class Date implements Comparable<Date> { x an object of class Date public int compareTo ( Date x ) } A total ordering of all Date objects We can compare any two objects of Date and receive one of three possible results Should be consistent with equals, although not required

13 Sorting Algorithms Selection Sort Bubble sort Insertion Sort …
Assume ascending (non-descending) order Date[] dates = new Date[MAX_SIZE]; int size; int[] a = new int[MAX_SIZE];

14 Selection Sort for i = 0 to (size - 2) Make sure a[i] is in sorted position Don’t go to (size – 1)! find index of the smallest element between a[i] and a[size - 1] swap a[i] and a[index] Must go to a[size – 1] to find the index! 12 7 34 15 5 22 14 21

15 Selection Sort for i = 0 to (size - 2) minIndex = i for j = i + 1 to (size – 1) if a[j] < a[minIndex] minIndex = j swap a[i], a[minIndex] Loop Invariant: After the ith iteration of the outer for loop, the first i elements (a[0] to a[i-1]) are in the correct locations.

16 Selection Sort 12 7 34 15 5 22 14 21 17 minIndex: 4 5 7 34 15 12 22 14 21 17 minIndex: 1 5 7 34 15 12 22 14 21 17 minIndex: 4 5 7 12 15 34 22 14 21 17 minIndex: 6 5 7 12 14 34 22 15 21 17 minIndex: 6 5 7 12 14 15 22 34 21 17 minIndex: 8 5 7 12 14 15 17 34 21 22 minIndex: 7 5 7 12 14 15 17 21 34 22 minIndex: 8 5 7 12 14 15 17 21 22 34 Don’t do it!

17 Bubble Sort for i = 0 to (size - 2) // Make sure a[i] is in sorted position for j = (size - 1) downto (i + 1) if a[j] < a[j - 1] then Swap a[j], a[j - 1]

18 First Pass: i = 0 12 7 34 15 5 22 14 21 17 12 7 34 15 5 22 14 17 21 12 7 34 15 5 22 14 17 21 12 7 34 15 5 14 22 17 21 12 7 34 15 5 14 22 17 21 12 7 34 5 15 14 22 17 21 12 7 5 34 15 14 22 17 21 12 5 7 34 15 14 22 17 21 5 12 7 34 15 14 22 17 21

19 First Pass of Selection Sort
12 7 34 15 5 22 14 21 17 minIndex: 4 Swap items[0] and items[4] 5 7 34 15 12 22 14 21 17

20 Bubble Sorting 12 7 34 15 5 22 14 21 17 i = 0 5 12 7 34 15 14 22 17 21 i = 1 5 7 12 14 34 15 17 22 21 i = 2 5 7 12 14 15 34 17 21 22 i = 3 5 7 12 14 15 17 34 21 22 i = 4 5 7 12 14 15 17 21 34 22 i = 5 5 7 12 14 15 17 21 22 34 i = 6 5 7 12 14 15 17 21 22 34 i = 7 5 7 12 14 15 17 21 22 34

21 Bubble Sort for i = 0 to (size - 2) for j = (size - 1) downto (i + 1) if a[j] < a[j - 1] then Swap a[j], a[j - 1] Loop Invariant: After ith iteration of the outer for loop, the first i elements (a[0] to a[i-1]) are in the correct locations.

22 Insertion Sort Inserting elements into array one at a time Keep the array sorted after each insertion First element: sorted Other elements: insert at the right position and move some elements Insert 12 12 Insert 7 7 12 Insert 34 7 12 34 Insert 15 7 12 15 34 Insert 5 5 7 12 15 34

23 Insertion Sort 12 7 34 15 5 22 14 21 17 Insert 7 and [0] to a[1] are sorted 7 12 34 15 5 22 14 21 17 Insert 34 and a[0] to a[2] are sorted 7 12 34 15 5 22 14 21 17 Insert 15 and a[0] to a[3] are sorted 7 12 15 34 5 22 14 21 17 Insert 5 and a[0] to a[4] are sorted 5 7 12 15 34 22 14 21 17 Insert 22 and a[0] to a[5] are sorted 5 7 12 15 22 34 14 21 17 Insert 14 and a[0] to a[6] are sorted 5 7 12 14 15 22 34 21 17 Insert 21 and a[0] to a[7] are sorted 5 7 12 14 15 21 22 34 17 a[0] to a[8] are sorted 5 7 12 14 15 17 21 22 34

24 Insertion Sort for i = 1 to (size - 1) // the inserted element j = i // is at index i while j > 0 and a[j] < a[j-1] Swap a[j], a[j - 1] j-- 7 12 15 34 5 7 12 15 5 34 7 12 5 15 34 7 5 12 15 34 5 7 12 15 34

25 Insertion Sort for i = 1 to (size - 1) j = i while j > 0 and a[j] < a[j-1] Swap a[j], a[j - 1] j-- Loop Invariant: After ith iteration of the outer for loop, top (i+1) elements (a[0] to a[i]) are sorted. (may not be in the final sorted locations)

26 Prog 4 Due Friday, November 9

27 Turn in on Friday in Class Bonus Points?
Exercise Turn in on Friday in Class Bonus Points?


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

Similar presentations


Ads by Google