Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 3540 Data Structures with OOP

Similar presentations


Presentation on theme: "COP 3540 Data Structures with OOP"— Presentation transcript:

1 COP 3540 Data Structures with OOP
'XYZ' Instructor NotesOOADv4.2 Instructor Notes COP 3540 Data Structures with OOP Chapter 3: Simple Sorting Module 'X' - <name of module>Module 5 - Analysis and Design Overview

2 'XYZ' Instructor NotesOOADv4.2 Instructor Notes
Contents Introduction Simple sorting algorithms Bubble Sort Selection Sort Insertion Sort Sorting Objects Sorting algorithm comparison 1/20/2014 Jim Littleton - COP3538 Module 'X' - <name of module>Module 5 - Analysis and Design Overview

3 'XYZ' Instructor NotesOOADv4.2 Instructor Notes
Introduction It’s more efficient to work with ordered values “Simple” sorting algorithms Easy to understand Slow (relative to advanced sorting algorithms) Poor performance (> 100 values) O(n2) sorting algorithms Bubble Sort Selection Sort Insertion Sort 1/20/2014 Jim Littleton - COP3538 Module 'X' - <name of module>Module 5 - Analysis and Design Overview

4 Basic Process of Simple Sorting Algorithms
'XYZ' Instructor NotesOOADv4.2 Instructor Notes Basic Process of Simple Sorting Algorithms Traverse the array multiple times On each traversal Compare two elements in the array Swap/Copy elements in the array Depends on the algorithm 1/20/2014 Jim Littleton - COP3538 Module 'X' - <name of module>Module 5 - Analysis and Design Overview

5 'XYZ' Instructor NotesOOADv4.2 Instructor Notes
Bubble Sort Very slow Very simple Process Traverse the array multiple times (until sorted) During each traversal 1. Compare the elements at positions n and n+1 2. If the n element is larger than the n+1 element Swap the two elements 3. Move to the n+1 element 4. Repeat until the last element is reached Start next traversal 1/20/2014 Jim Littleton - COP3538 Module 'X' - <name of module>Module 5 - Analysis and Design Overview

6 Bubble Sort Bubble Sort Animation
1/20/2014 Jim Littleton - COP3538

7 Bubble Sort Code Listing public class BubbleSort {
private int[] array = new int[]{4, 1, 0, 5, 7, 6, 8, 2, 3, 9}; private int numElems = 10; public void bubbleSort() { for(int y = 0; y < numElems; y++) { // Traverse array multiple times for(int x = 0; x < numElems – 1; x++) { // Compare elements in the array if(array[x] > array[x + 1]) { // Compare two elements swap(x, x + 1); } } } public void swap(int pos1, int pos2) { int temp = array[pos1]; array[pos1] = array[pos2]; array[pos2] = temp; 1/20/2014 Jim Littleton - COP3538

8 Bubble Sort Performance
'XYZ' Instructor NotesOOADv4.2 Instructor Notes Bubble Sort Performance Number of comparisons n2 – n (90 comparisons for 10 elements) Number of swaps 0 to (n2 – n) (0 to 90 swaps for 10 elements) 0, if the array is already sorted (n2 – n) / 2, if the array is randomly sorted (n2 – n), if the array is sorted in reverse order Algorithm can be written more efficiently Don’t compare the sorted section of the array With each traversal, another element is sorted No point in comparing sorted elements Stop sorting early if the array is already sorted If no swaps occur during a traversal of the array The array is already sorted 1/20/2014 Jim Littleton - COP3538 Module 'X' - <name of module>Module 5 - Analysis and Design Overview

9 Bubble Sort More Efficient Code Listing public class BubbleSort {
private int[] array = new int[]{4, 1, 0, 5, 7, 6, 8, 2, 3, 9}; private int numElems = 10; public void bubbleSort() { boolean swapped = true; // Track whether a swap occurred for(int y = 0; y < numElems && swapped; y++) { // Loop if swapped occurred swapped = false; // Reset swapped status with each traversal for(int x = 0; x < numElems – (y + 1); x++) { // Reduce elements compared if(array[x] > array[x + 1]) { swap(x, x + 1); swapped = true; // Update swapped status is swap occurs } } } public void swap(int pos1, int pos2) { int temp = array[pos1]; array[pos1] = array[pos2]; array[pos2] = temp; 1/20/2014 Jim Littleton - COP3538

10 Efficient Bubble Sort Performance
'XYZ' Instructor NotesOOADv4.2 Instructor Notes Efficient Bubble Sort Performance Number of comparisons (n2 – n) / 2 (45 comparisons for 10 elements) Number of swaps 0 to (n2 – n) / 2 (0 to 45 swaps for 10 elements) 0, if the array is already sorted (n2 – n) / 4, random order (22.5 swaps/10 elements) (n2 – n) / 2, if the array is sorted in reverse order Bubble Sort has an efficiency of O(n2) Constants like “-1”, “/2” and “/4” are ignored 1/20/2014 Jim Littleton - COP3538 Module 'X' - <name of module>Module 5 - Analysis and Design Overview

11 'XYZ' Instructor NotesOOADv4.2 Instructor Notes
Bubble Sort – Notes Both compares and swaps are proportional to n2 Sorting is completed in O(n2) time This is slow When an algorithm contains ‟NESTED LOOPS” O(n2) performance should be expected! 1/20/2014 Jim Littleton - COP3538 Module 'X' - <name of module>Module 5 - Analysis and Design Overview

12 'XYZ' Instructor NotesOOADv4.2 Instructor Notes
Selection Sort Faster than Bubble Sort Simple (nearly as simple as Bubble Sort) Process Traverse the array multiple times During each traversal 1. Initialize pointers Set target (t) and min to first element in the array Set n to the t+1 element in the array 2. Compare the elements at positions n and min 3. If the n element is smaller than the min element Set min to n 4. Move to the n+1 element 5. Repeat until the last element is reached Swap the elements in the min and t positions Start next traversal 1/20/2014 Jim Littleton - COP3538 Module 'X' - <name of module>Module 5 - Analysis and Design Overview

13 Selection Sort Selection Sort Animation
1/20/2014 Jim Littleton - COP3538

14 Selection Sort Code Listing public class SelectionSort {
private int[] array = new int[]{4, 1, 0, 5, 7, 6, 8, 2, 3, 9}; private int numElems = 10; public void selectionSort() { int min; for(int y = 0; y < numElems - 1; y++) { // Traverse array multiple times min = y; // Set minimum value pointer for(int x = y + 1; x < numElems; x++) { // Traverses the array if(array[x] < array[min]) { // Compare two elements min = x; // Update minimum value pointer } swap(y, min); // Perform the swap once per traversal } } public void swap(int pos1, int pos2) { int temp = array[pos1]; array[pos1] = array[pos2]; array[pos2] = temp; 1/20/2014 Jim Littleton - COP3538

15 Selection Sort Performance
'XYZ' Instructor NotesOOADv4.2 Instructor Notes Selection Sort Performance Number of comparisons (n2 – n) / 2 (45 comparisons for 10 elements) Number of swaps n – 1 (9 swaps for 10 elements) Algorithm can be written more efficiently Don’t swap if min value is in the correct position If the min value is already in the correct position No point in swapping the element with itself IMPORTANT!! Unlike the Bubble Sort CANNOT stop sorting if no swap occurs 1/20/2014 Jim Littleton - COP3538 Module 'X' - <name of module>Module 5 - Analysis and Design Overview

16 Selection Sort More Efficient Code Listing
public class SelectionSort { private int[] array = new int[]{4, 1, 0, 5, 7, 6, 8, 2, 3, 9}; private int numElems = 10; public void selectionSort() { int min; for(int y = 0; y < numElems - 1; y++) { // Traverse array multiple times min = y; // Set minimum value pointer for(int x = y + 1; x < numElems; x++) { // Traverses the array if(array[x] < array[min]) { // Compare two elements min = x; // Update minimum value pointer } if(min > y) { // Only swap the elements if they are not in the same position swap(y, min); // Perform the swap once per traversal } } public void swap(int pos1, int pos2) { int temp = array[pos1]; array[pos1] = array[pos2]; array[pos2] = temp; 1/20/2014 Jim Littleton - COP3538

17 Efficient Selection Sort Performance
'XYZ' Instructor NotesOOADv4.2 Instructor Notes Efficient Selection Sort Performance Number of comparisons (n2 – n) / 2 (45 comparisons for 10 elements) Number of swaps 0 to (n – 1) (0 to 9 swaps for 10 elements) 0, if the array is already sorted (n – 1) / 2, random order (4.5 swaps/10 elements) (n – 1), if the array is sorted in reverse order Selection Sort has an efficiency of O(n2) Constants like “-1” and “/2” are ignored 1/20/2014 Jim Littleton - COP3538 Module 'X' - <name of module>Module 5 - Analysis and Design Overview

18 'XYZ' Instructor NotesOOADv4.2 Instructor Notes
Selection Sort – Notes Only compares are proportional to n2 Swaps are proportional to n Sorting is still completed in O(n2) time Comparisons are as slow as the Bubble Sort Swaps are faster than the Bubble Sort Fewer swaps are performed compared to Bubble Sort The Selection Sort is faster than Bubble Sort Especially if swapping two values takes a long time Comparing two values is quick Swapping two values takes time Requires 3 copy (assignment) operations 1/20/2014 Jim Littleton - COP3538 Module 'X' - <name of module>Module 5 - Analysis and Design Overview

19 'XYZ' Instructor NotesOOADv4.2 Instructor Notes
Insertion Sort Faster than Bubble Sort, as fast as Selection Sort More complicated than the Bubble Sort and Selection Sort Process Traverse the array multiple times During each traversal 1. Initialize pointers Set n to the second element in the array Set temp to the value of the n element 2. Compare the temp value and n-1 element 3. If the temp value is smaller than the n-1 element Move n-1 element to n Set n to n-1 Repeat steps 2 and 3 until temp value is no longer smaller than n-1 element 4. Copy temp value to n-1 element Start next traversal 1/20/2014 Jim Littleton - COP3538 Module 'X' - <name of module>Module 5 - Analysis and Design Overview

20 Insertion Sort Insertion Sort Animation
1/20/2014 Jim Littleton - COP3538

21 Insertion Sort Code Listing public class InsertionSort {
private int[] array = new int[]{4, 1, 0, 5, 7, 6, 8, 2, 3, 9}; private int numElems = 10; public void insertionSort() { int x, temp; for(int y = 1; y < numElems; y++) { // Traverse array multiple times temp = array[y]; // Set minimum value pointer x = y; while(x > 0 && array[x – 1] >= temp) { // Traverses the array array[x] = array[x - 1]; // Move elements x--; } array[x] = temp; // Perform the swap once per traversal } } 1/20/2014 Jim Littleton - COP3538

22 Insertion Sort Performance
'XYZ' Instructor NotesOOADv4.2 Instructor Notes Insertion Sort Performance Number of comparisons (n2 – n) / 2 (45 comparisons for 10 elements) (n2 – n) / 4 occur for randomly ordered array Number of copies 0 to (n2 – n) / 2 (0 to 45 copies for 10 elements) 0, if all the elements are already in the correct positions (n – 1) / 4, random order (22.5 copies for 10 elements) (n2 – n) / 2, if the array is sorted in reverse order Insertion Sort has an efficiency of O(n2) Constants like “-1”, “/2” and “/4” are ignored 1/20/2014 Jim Littleton - COP3538 Module 'X' - <name of module>Module 5 - Analysis and Design Overview

23 'XYZ' Instructor NotesOOADv4.2 Instructor Notes
Insertion Sort – Notes Both compares and copies are proportional to n2 Sorting is completed in O(n2) time Copies require less time than swaps (1 swap is equal to 3 copies) Insertion Sort can be twice as fast as Bubble Sort Insertion Sort can be as fast as Selection Sort If the array is sorted in the reverse order (5, 4, 3, 2, 1) Then Insertion Sort is just as slow as Bubble Sort Insertion Sort performs best when the array is nearly sorted Runs at O(n) time rather than O(n2) The Insertion Sort is used by complex algorithms for this very reason See Priority Queue’s Insert algorithm!! (covered in chapter 4) 1/20/2014 Jim Littleton - COP3538 Module 'X' - <name of module>Module 5 - Analysis and Design Overview

24 Sorting an Array of Objects
An array is an array It doesn’t matter what the array contains Primitives values (int, double, etc.) Objects It does matter when sorting an array Primitive values are accessed directly Objects, however, contain a number of properties Every object in an array must be sorted on same property The “key field” is the property the sort is performed on Trying to sort objects directly won’t work The array stores the memory address of each object Will only sort the stored memory addresses 1/20/2014 Jim Littleton - COP3538

25 Sorting an Array of Objects
Sorting an Object Array Example public class Student { private String firstName; // Comparison performed using String.compareTo method private String lastName; // Comparison performed using String.compareTo method private int numCredits; // Regular comparison (numCredits > value) private double gpa; // Regular comparison (gpa > value) private boolean declaredMajor; // Regular comparison (declaredMajor > value) public Student() { } } public class MainClass { private static Student[] student = new Student[10]; public static void main(String[] args) { // Only the comparison statements are listed below if(student[x].firstName.compareTo(student[x + 1].firstName) < 0) if(student[x].firstName.compareToIgnoreCase(student[x + 1].firstName) == 0) if(student[x].numCredits > student[x + 1].numCredits) } 1/20/2014 Jim Littleton - COP3538

26 'XYZ' Instructor NotesOOADv4.2 Instructor Notes
Questions? 1/6/2014 Jim Littleton - COP3538 Module 'X' - <name of module>Module 5 - Analysis and Design Overview


Download ppt "COP 3540 Data Structures with OOP"

Similar presentations


Ads by Google