Download presentation
Presentation is loading. Please wait.
Published byΘέτις Αναγνωστάκης Modified over 6 years ago
1
COMPUTER 2430 Object Oriented Programming and Data Structures I
2
Selection Sort Bubble Sort Insertion Sort . . .
Sorting Algorithms Selection Sort Bubble Sort Insertion Sort . . .
3
Algorithm Evaluation How to compare the sorting algorithms? Can we design a better algorithm? Is there a better/best algorithm? When the problem size (number of elements) becomes larger and larger. Implement algorithms and compare running times. Compare the orders of the algorithms, the big Os.
4
Sorting Algorithms How to compare the orders of the sorting algorithms? For an array of n elements (n is very large!) How many times is the comparison done as a function of n?
5
Selection Sort n: 9 12 7 34 15 5 22 14 21 17 # of comparisons: 8 5 7 34 15 12 22 14 21 17 # of comparisons: 7 5 7 34 15 12 22 14 21 17 # of comparisons: 6 5 7 12 15 34 22 14 21 17 # of comparisons: 5 5 7 12 14 34 22 15 21 17 # of comparisons: 4 5 7 12 14 15 22 34 21 17 # of comparisons: 3 5 7 12 14 15 17 34 21 22 # of comparisons: 2 5 7 12 14 15 17 21 34 22 # of comparisons: 1 5 7 12 14 15 17 21 22 34 Don’t do it!
6
Selection Sort n: 9 Number of comparisons
12 7 34 15 5 22 14 21 17 n: 9 Number of comparisons = 36
7
Selection Sort Pass i First of j Last of j # of Comparisons 1 2 . n-4
for i = 0 to (n - 2) minIndex = i for j = i + 1 to (n – 1) if a[j] < a[minIndex] minIndex = j swap a[i], a[minIndex] How many comparisons for each i? Pass i First of j Last of j # of Comparisons 1 2 . n-4 n-3 n-2 1 2 3 . n-3 n-2 n-1 n-1 . n-1 n-2 n-3 . 3 2 1
8
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 +
9
Selection Sort for i = 0 to (n - 2) minIndex = i for j = i + 1 to (n – 1) if a[j] < a[minIndex] minIndex = j swap a[i], a[minIndex] Total # of comparisons: (n-1) + (n–2) + (n-3) = ((n-1) + 1) * (n-1) / 2 = n * (n-1) / 2 = (n2 – n) / 2 = O(n2) i 1 2 … n-4 n-3 n-2 # Comparisons n-1 3
10
Selection Sort Total number of comparisons (n2 – n) / 2 The Big O of Selection Sort O(n2) Ignore all less significant terms Ignore the coefficient of the most significant term
11
Bubble Sort Pass i First of j Last of j # of Comparisons 1 2 . n-4 n-3
for i = 0 to (n - 2) for j = (n - 1) down to (i + 1) if a[j] < a[j - 1] then Swap a[j], a[j - 1] How many comparisons for each i? Pass i First of j Last of j # of Comparisons 1 2 . n-4 n-3 n-2 n-1 . 1 2 3 . n-3 n-2 n-1 n-1 n-2 n-3 . 3 2 1
12
Bubble Sort for i = 0 to (n - 2) for j = (n - 1) down to (i + 1) if a[j] < a[j - 1] then Swap a[j], a[j - 1] Total number of comparisons (n-1) + (n–2) + (n-3) = (n2 – n) / 2 The Big O of Bubble Sort O(n2)
13
Insertion Sort for i = 1 to (n - 1) j = i while j > 0 and a[j] < a[j-1] Swap a[j], a[j - 1] j—- Total number of comparisons The best case: elements are sorted already n – 1 Big O: O(n)?
14
Insertion Sort for i = 1 to (n - 1) j = i while j > 0 and a[j] < a[j-1] Swap a[j], a[j - 1] j—- Total number of comparisons The worse case: elements in reverse order (n-3) + (n-2) + (n-1) = (n2 – n) / 2 The Big O: O(n2)
15
Insertion Sort for i = 1 to (n - 1) j = i while j > 0 and a[j] < a[j-1] Swap a[j], a[j - 1] j—- Total number of comparisons The average case: ( (n-3) + (n-2) + (n-1)) / 2 = ((n2 – n) / 2) / 2 The Big O: O(n2)
16
Sorting Algorithms For an array of n elements How many times is the comparison done as a function of n? O(n2) for all three algorithms How many times is swap done as a function of n? Selection sort: O(n) Bubble sort: O(n2) Insertion sort: O(n2)
17
Test 2: Stack and User public Additive sumThemAll(AdditiveStack s, int stackSize) { Additive sum = new Additive(); AdditiveStack ts = new AdditiveStack(stackSize); while (!s.isEmpty()) Additive temp = s.pop(); sum = sum.plus(temp); ts.push(temp); } while (!ts.isEmpty()) s.push( ts.pop() ); return sum;
18
Test 2: Stack and User public Additive sumThemAll(AdditiveStack s, int stackSize) { Additive sum = new Additive(); // Make sure ts is large enough AdditiveStack ts = new AdditiveStack(stackSize); while (!s.isEmpty()) Additive temp = s.pop(); sum = sum.plus(temp); ts.push(temp); } while (!ts.isEmpty()) // We need the loop s.push( ts.pop() ); // This does not work: s = ts; return sum;
19
Test 2: Queue and Implementer
public class Queue { private Object items[]; int front, rear, count; // size is items.length, not count! public Queue (int size) items = new Object[size]; front = rear = count = 0; } public boolean find(Object target) . . .
20
Test 2: Queue and Implementer
public class Queue { private Object items[]; int front, rear, count; public boolean find(Object target) for (int i = 0; i < count; i++) int index = (front + i) % items.length; if (items[index].equals(target)) return true; } return false;
21
Test 2: Queue and User Queue myQueue = new Queue (size); Object obj = nextTarget(); boolean found = peeker(myQueue, size, obj); if (found) { } else
22
Test 2: Queue and User public boolean peeker(Queue q, int queueSize, object target) { Queue tq = new Queue(queueSize); boolean isThere = false; while (!q.isEmpty()) Object temp = q.remove(); if (temp.equals(target)) isThere = true; tq.add(temp); } while (!tq.isEmpty()) q.add(tq.remove); return isThere;
23
Test 2: Queue and User public boolean peeker(Queue q, int queueSize, object target) { Queue tq = new Queue(queueSize); // Make sure tq is large enough boolean isThere = false; while (!q.isEmpty()) Object temp = q.remove(); if (temp.equals(target)) isThere = true; tq.add(temp); } while (!tq.isEmpty()) // We need the loop! q.add(tq.remove); return isThere;
24
Expected Learning Outcomes
Develop software using elementary data structures. Design, implement, and test programs using classes, inheritance, and polymorphism. Compare and contrast algorithm efficiency at a very basic level. Write module tests, system tests, and develop test specifications. Perform simple object-oriented analysis and design. Work in a small team (two or three people) on the analysis, design, implementation, and testing of a project.
25
Prog 5 Punch in and out in SE Tool Let me know of any issues
With the tool With your partners Prog5 score could be different for students in the same group
26
Lab 11 Due today by 11 pm
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.