COMPUTER 2430 Object Oriented Programming and Data Structures I

Slides:



Advertisements
Similar presentations
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Advertisements

Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
the fourth iteration of this loop is shown here
1 Sorting II: Bubble Sort and Selection Sort CSC326 Information Structure Spring 2009.
Simple Sorting Algorithms
Lecture 4 Feb 5 completion of recursion (inserting into a linked list as last item) analysis of algorithms – Chapter 2.
Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )
Sorting Algorithms: Selection, Insertion and Bubble.
Time Complexity s Sorting –Insertion sorting s Time complexity.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Chapter 19: Searching and Sorting Algorithms
CSE 373 Data Structures and Algorithms
1 Data Structures and Algorithms Sorting I Gal A. Kaminka Computer Science Department.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
BUBBLE SORT. Introduction Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to.
Comparison of Optimization Algorithms By Jonathan Lutu.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Bubble Sort.
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
CS 2430 Day 24. Announcements Quiz this Friday Program 5 posted on Monday Program 4 due date: Friday at 10pm Program 4 grace date: Wednesday at 10pm (don’t.
Elementary Sorting 30 January Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j List[j])
1 Algorithms CSCI 235, Fall 2015 Lecture 11 Elementary Sorts.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
CS212: Data Structures and Algorithms
Sorting Mr. Jacobs.
Lecture 14 Searching and Sorting Richard Gesick.
CSC 222: Object-Oriented Programming
Algorithms CSCI 235, Fall 2017 Lecture 13 Elementary Sorts II
Alg2_1c Extra Material for Alg2_1
Sorting Algorithms.
Algorithm Analysis CSE 2011 Winter September 2018.
COMPUTER 2430 Object Oriented Programming and Data Structures I
10.3 Bubble Sort Chapter 10 - Sorting.
Linear and Binary Search
Algorithm design and Analysis
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
Describing algorithms in pseudo code
Introduction to Programming
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
COMPUTER 2430 Object Oriented Programming and Data Structures I
Lecture 11 Searching and Sorting Richard Gesick.
COMPUTER 2430 Object Oriented Programming and Data Structures I
IT 4043 Data Structures and Algorithms
COMPUTER 2430 Object Oriented Programming and Data Structures I
Intro to Sorting Sorting
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Search,Sort,Recursion.
CS 2430 Object Oriented Programming and Data Structures I
Simple Sorting Algorithms
COMPUTER 2430 Object Oriented Programming and Data Structures I
Simple Sorting Algorithms
CS210- Lecture 3 Jun 6, 2005 Announcements
Simple Sorting Algorithms
Sorting Sorting is a fundamental problem in computer science.
Module 8 – Searching & Sorting Algorithms
Stacks, Queues, ListNodes
COMPUTER 2430 Object Oriented Programming and Data Structures I
Presentation transcript:

COMPUTER 2430 Object Oriented Programming and Data Structures I

Selection Sort Bubble Sort Insertion Sort . . . Sorting Algorithms Selection Sort Bubble Sort Insertion Sort . . .

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.

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?

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!

Selection Sort n: 9 Number of comparisons 12 7 34 15 5 22 14 21 17 n: 9 Number of comparisons 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 36

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

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 +

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) + . . . + 3 + 2 + 1 = ((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

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

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

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) + . . . + 3 + 2 + 1 = (n2 – n) / 2 The Big O of Bubble Sort O(n2)

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

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 1 + 2 + 3 + . . . + (n-3) + (n-2) + (n-1) = (n2 – n) / 2 The Big O: O(n2)

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: (1 + 2 + 3 + . . . + (n-3) + (n-2) + (n-1)) / 2 = ((n2 – n) / 2) / 2 The Big O: O(n2)

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)

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;

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;

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

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;

Test 2: Queue and User Queue myQueue = new Queue (size); . . . Object obj = nextTarget(); boolean found = peeker(myQueue, size, obj); if (found) { } else

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;

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;

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.

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

Lab 11 Due today by 11 pm