An Introduction to Sorting

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture22.
An Introduction to Sorting Chapter 8 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Faster Sorting Methods Chapter 9 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Faster Sorting Methods Chapter 12 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
CMPS1371 Introduction to Computing for Engineers SORTING.
Faster Sorting Methods Chapter Chapter Contents Merge Sort Merging Arrays Recursive Merge Sort The Efficiency of Merge Sort Iterative Merge Sort.
An Introduction to Sorting Chapter Chapter Contents Selection Sort Iterative Selection Sort Recursive Selection Sort The Efficiency of Selection.
Faster Sorting Methods Chapter 9. 2 Chapter Contents Merge Sort Merging Arrays Recursive Merge Sort The Efficiency of Merge Sort Merge Sort in the Java.
An Introduction to Sorting Chapter 9. 2 Chapter Contents Selection Sort Iterative Selection Sort Recursive Selection Sort The Efficiency of Selection.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Announcements.
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
An Introduction to Sorting Chapter 8. 2 Chapter Contents Selection Sort Iterative Selection Sort Recursive Selection Sort The Efficiency of Selection.
Iterators Chapter 7. Chapter Contents What is an Iterator? A Basic Iterator Visits every item in a collection Knows if it has visited all items Doesn’t.
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
Inheritance and Lists Chapter 14 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
An Introduction to Sorting Chapter 11 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
An Introduction to Sorting Chapter 8 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
Comparison-Based Sorting & Analysis Smt Genap
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorted Lists Chapter Chapter Contents Specifications for the ADT Sorted List Using the ADT Sorted List A Linked Implementation The Method add The.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Lists and Sorted Lists: various implementations Slides by Nadia Al-Ghreimil adopted from Steve Armstrong LeTourneau University Longview, TX  2007, 
1 CS162: Introduction to Computer Science II Abstract Data Types.
Searching and Sorting Searching algorithms with simple arrays
Prof. U V THETE Dept. of Computer Science YMA
16 Searching and Sorting.
Sorting Mr. Jacobs.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
A Binary Search Tree Implementation
Algorithm Efficiency and Sorting
CS Data Structures Chapter 8 Lists Mehmet H Gunes
An Introduction to Sorting
Searching and Sorting Linear Search Binary Search ; Reading p
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Adapted from Pearson Education, Inc.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
List Implementations Chapter 9.
Slides by Steve Armstrong LeTourneau University Longview, TX
Sorting … and Insertion Sort.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
24 Searching and Sorting.
Sub-Quadratic Sorting Algorithms
Chapter 4.
Sorting Chapter 8.
Algorithm Efficiency and Sorting
Analysis of Algorithms
CSE 373 Data Structures and Algorithms
Sorting Chapter 10.
Algorithm Efficiency and Sorting
Lecture 6: Linked Data Structures
Recitation 2 CS0445 Data Structures
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Copyright ©2012 by Pearson Education, Inc. All rights reserved
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

An Introduction to Sorting Chapter 11

Chapter Contents Selection Sort Insertion Sort Shell Sort Iterative Selection Sort Recursive Selection Sort The Efficiency of Selection Sort Insertion Sort Iterative Insertion Sort Recursive Insertion Sort The Efficiency of Insertion Sort Insertion Sort of a Chain of Linked Nodes Shell Sort The Java Code The Efficiency of Shell Sort Comparing the Algorithms

Selection Sort Task: rearrange books on shelf by height Approach: Shortest book on the left Approach: Look at books, select shortest book Swap with first book Look at remaining books, select shortest Swap with second book Repeat …

Selection Sort Fig. 11-1 Before and after exchanging shortest book and the first book.

Selection Sort Fig. 11-2 A selection sort of an array of integers into ascending order.

Iterative Selection Sort Iterative algorithm for selection sort Algorithm selectionSort(a, n) // Sorts the first n elements of an array a. for (index = 0; index < n - 1; index++) { indexOfNextSmallest = the index of the smallest value among a[index], a[index+1], . . . , a[n-1] Interchange the values of a[index] and a[indexOfNextSmallest] // Assertion: a[0] £ a[1] £ . . . £ a[index], and these are the smallest // of the original array elements. // The remaining array elements begin at a[index+1]. }

Recursive Selection Sort Recursive algorithm for selection sort Algorithm selectionSort(a, first, last) // Sorts the array elements a[first] through a[last] recursively. if (first < last) { indexOfNextSmallest = the index of the smallest value among a[first], a[first+1], . . . , a[last] Interchange the values of a[first] and a[indexOfNextSmallest] // Assertion: a[0] £ a[1] £ . . . £ a[first] and these are the smallest // of the original array elements. // The remaining array elements begin at a[first+1]. selectionSort(a, first+1, last) }

The Efficiency of Selection Sort Iterative method for loop executes n – 1 times For each of n – 1 calls, inner loop executes n – 2 times (n – 1) + (n – 2) + …+ 1 = n(n – 1)/2 = O(n2) Recursive selection sort performs same operations Also O(n2)

Insertion Sort If first two books are out of order Remove second book Slide first book to right Insert removed book into first slot Then look at third book, if it is out of order Remove that book Slide 2nd book to right Insert removed book into 2nd slot Recheck first two books again Etc.

Fig. 11-3 The placement of the third book during an insertion sort.

Fig. 11-4 An insertion sort of books

Iterative Insertion Sort Iterative algorithm for insertion sort Algorithm insertionSort(a, first, last) // Sorts the array elements a[first] through a[last] iteratively. for (unsorted = first+1 through last) { firstUnsorted = a[unsorted] insertInOrder(firstUnsorted, a, first, unsorted-1) } Algorithm insertInOrder(element, a, begin, end) // Inserts element into the sorted array elements a[begin] through a[end]. index = end while ( (index >= begin) and (element < a[index]) ) { a[index+1] = a[index] // make room index - - } // Assertion: a[index+1] is available. a[index+1] = element // insert

Iterative Insertion Sort Fig. 11-5 An insertion sort inserts the next unsorted element into its proper location within the sorted portion of an array

Iterative Insertion Sort Fig. 11-6 An insertion sort of an array of integers into ascending order

Recursive Insertion Sort Algorithm for recursive insertion sort Algorithm insertionSort(a, first, last) // Sorts the array elements a[first] through a[last] recursively. if (the array contains more than one element) { Sort the array elements a[first] through a[last-1] Insert the last element a[last] into its correct sorted position within the rest of the array }

Recursive Insertion Sort Fig. 11-7 Inserting the first unsorted element into the sorted portion of the array. (a) The element is ≥ last sorted element; (b) the element is < than last sorted element

Efficiency of Insertion Sort Best time efficiency is O(n) Worst time efficiency is O(n2) If array is closer to sorted order Less work the insertion sort does More efficient the sort is Insertion sort is acceptable for small array sizes

Insertion Sort of Chain of Linked Nodes Fig. 11-8 A chain of integers sorted into ascending order.

Insertion Sort of Chain of Linked Nodes Fig. 11-9 During the traversal of a chain to locate the insertion point, save a reference to the node before the current one.

Insertion Sort of Chain of Linked Nodes Efficiency of insertion sort of a chain is O(n2) Fig. 11-10 Breaking a chain of nodes into two pieces as the first step in an insertion sort: (a) the original chain; (b) the two pieces

Shell Sort A variation of the insertion sort But faster than O(n2) Done by sorting subarrays of equally spaced indices Instead of moving to an adjacent location an element moves several locations away Results in an almost sorted array This array sorted efficiently with ordinary insertion sort

Shell Sort Fig. 11-11 An array and the subarrays formed by grouping elements whose indices are 6 apart.

Shell Sort Fig. 11-12 The subarrays of Fig. 11-11 after they are sorted, and the array that contains them.

Shell Sort Fig. 11-13 The subarrays of the array in Fig. 11-12 formed by grouping elements whose indices are 3 apart

Shell Sort Fig. 11-14 The subarrays of Fig. 11-13 after they are sorted, and the array that contains them.

Efficiency of Shell Sort Efficiency is O(n2) for worst case If n is a power of 2 Average-case behavior is O(n1.5) Any time the variable space is even, add 1 This also results in O(n1.5)

Comparing the Algorithms Best Average Worst Case Case Case Selection sort O(n2) O(n2) O(n2) Insertion sort O(n) O(n2) O(n2) Shell sort O(n) O(n1.5) O(n1.5) Fig. 11-15 The time efficiencies of three sorting algorithms, expressed in Big Oh notation.

Faster Sorting Methods Chapter 12

Chapter Contents Merge Sort Quick Sort Radix Sort Merging Arrays Recursive Merge Sort The Efficiency of Merge Sort Iterative Merge Sort Merge Sort in the Java Class Library Quick Sort The Efficiency of Quick Sort Creating the Partition Java Code for Quick Sort Quick Sort in the Java Class Library Radix Sort Pseudocode for Radix Sort Comparing the Algorithms

Merge Sort Divide an array into halves Sort the two halves Merge them into one sorted array Referred to as a divide and conquer algorithm This is often part of a recursive algorithm However recursion is not a requirement

Merging two sorted arrays into one sorted array. Merge Sort Merging two sorted arrays into one sorted array.

The major steps in a merge sort.

Merge Sort Algorithm mergeSort(a, first, last) // Sorts the array elements a[first] through a[last] recursively. if (first < last) { mid = (first + last)/2 mergeSort(a, first, mid) mergeSort(a, mid+1, last) Merge the sorted halves a[first..mid] and a[mid+1..last] }

The effect of the recursive calls and the merges during a merge sort.

Merge Sort Efficiency of the merge sort Merge sort is O(n log n) in all cases It's need for a temporary array is a disadvantage Merge sort in the Java Class Library The class Arrays has sort routines that uses the merge sort for arrays of objects public static void sort(Object[] a); public static void sort(Object[] a, int first, int last);

Quick Sort Divides the array into two pieces Not necessarily halves of the array An element of the array is selected as the pivot Elements are rearranged so that: The pivot is in its final position in sorted array Elements in positions before pivot are less than the pivot Elements after the pivot are greater than the pivot

Quick Sort Algorithm quickSort(a, first, last) // Sorts the array elements a[first] through a[last] recursively. if (first < last) { Choose a pivot Partition the array about the pivot pivotIndex = index of pivot quickSort(a, first, pivotIndex-1) // sort Smaller quickSort(a, pivotIndex+1, last) // sort Larger }

A partition of an array during a quick sort.

A partition strategy for quick sort … continued→

A partition strategy for quick sort (continued)

Quick Sort Median-of-three pivot selection: (a) the original array; (b) the array with its first, middle, and last elements sorted

Quick Sort (a) The array with its first, middle, and last elements sorted; (b) the array after positioning the pivot and just before partitioning.

Quick Sort Quick sort rearranges the elements in an array during partitioning process After each step in the process One element (the pivot) is placed in its correct sorted position The elements in each of the two sub arrays Remain in their respective subarrays The class Arrays in the Java Class Library uses quick sort for arrays of primitive types

Quick Sort Quick sort is O(n log n) in the average case O(n2) in the worst case Worst case can be avoided by careful choice of the pivot

Radix Sort Does not compare objects Treats array elements as if they were strings of the same length Groups elements by a specified digit or character of the string Elements placed into "buckets" which match the digit (character) Originated with card sorters when computers used 80 column punched cards

Radix Sort Original array and buckets after first distribution;(b) reordered array and buckets after second distribution … (continued → )

Radix Sort (c) reordered array and buckets after third distribution; (d) sorted array

Radix sort is O(n) but can only be used for certain kinds of data Pseudo code Algorithm radixSort(a, first, last, maxDigits) // Sorts the array of positive decimal integers a[first..last] into ascending order; // maxDigits is the number of digits in the longest integer. for (i = 1 to maxDigits) { Clear bucket[0], bucket[1], . . . , bucket[9] for (index = first to last) { //consider each item in the array digit = ith digit from the right of a[index] Place a[index] at end of bucket[digit] } Place contents of bucket[0], bucket[1], . . . , bucket[9] into array a }

Comparing the Algorithms The time efficiency of various algorithms in Big Oh notation

Comparing the Algorithms A comparison of growth-rate functions as n increases.

Sorted Lists Chapter 13

Chapter Contents Specifications for the ADT Sorted List Using the ADT Sorted List A Linked Implementation The Method add The Efficiency of the Linked Implementation An Implementation that Uses the ADT List Efficiency Issues

Specifications for the ADT Sorted List Data A collection of objects in sorted order, same data type The number of objects in the collection Operations Add a new entry Remove an entry Get the position of the entry Check if a certain value is contained in the list Clear the list Return the length of the list Check if list is empty or full Display the list Note: a sorted list will not let you add or replace an entry by position

A Linked Implementation (i.e., create a new ADT modeled after LList) Outline of the class public class SortedLinkedList implements SortedListInterface { private Node firstNode; // reference to first node of chain private int length; // number of entries in sorted list public SortedLinkedList() { firstNode = null; length = 0; } // end default constructor < Implementations of the sorted list operations go here. > . . . private class Node { private Object data; private Node next; < Constructors > . . . < Accessor and mutator methods: getData, setData, getNextNode, setNextNode . . . } // end Node } // end SortedLinkedList

The Method getPosition public int getPosition(Comparable anEntry) { int position = 1; Node currentNode = firstNode; while ( (currentNode != null) && (anEntry.compareTo(currentNode.getData()) > 0) ) { currentNode = currentNode.getNextNode(); position++; } // end while if ( (currentNode == null) || anEntry.compareTo(currentNode.getData()) != 0) position = -position; return position; } // end getPosition

The Method add Fig. 13-1 Insertion points of names into a sorted chain of linked nodes.

The Method add public boolean add(Comparable newEntry) { Node newNode = new Node(newEntry); Node nodeBefore = getNodeBefore(newEntry); if (isEmpty() || (nodeBefore == null)) { // add before first node newNode.setNextNode(firstNode); firstNode = newNode; } else { // add between nodeBefore and currentNode Node nodeAfter = nodeBefore.getNextNode(); newNode.setNextNode(nodeAfter); nodeBefore.setNextNode(newNode); } // end if length++; return true; } // end add

The Method getNodeBefore private Node getNodeBefore(Comparable anEntry) { Node currentNode = firstNode; Node nodeBefore = null; while ( (currentNode != null) && ( anEntry.compareTo(currentNode.getData()) > 0) ) { nodeBefore = currentNode; currentNode = currentNode.getNextNode(); } // end while return nodeBefore; } // end getNodeBefore

The Method add Recursive algorithm if ( (currentNode = = null) or newEntry.compareTo(currentNode.getData()) <= 0) { currentNode = new Node(newEntry, currentNode) } else Recursively add newEntry to the chain beginning at currentNode.getNextNode()

Fig. 13-2 Recursively adding Luke to a sorted chain of names The Method add Fig. 13-2 Recursively adding Luke to a sorted chain of names

The Recursive Method add public boolean add(Comparable newEntry) { firstNode = add(newEntry, firstNode); length++; return true; } // end add private Node add(Comparable newEntry, Node currentNode) { if ( (currentNode = = null) || newEntry.compareTo(currentNode.getData()) <= 0) { currentNode = new Node(newEntry, currentNode); } else { Node nodeAfter = add(newEntry, currentNode.getNextNode()); currentNode.setNextNode(nodeAfter); } // end if return currentNode;

The Method add Fig. 13-3 Recursively adding a node at the beginning of the chain … continued →

The Method add Fig. 13-3 (ctd) Recursively adding a node at the beginning of the chain.

The Method add Fig. 13-4 Recursively adding a node between existing nodes in a chain … continued →

The Method add Fig. 13-4 (ctd) Recursively adding a node between existing nodes in a chain.

Efficiency of the Linked Implementation ADT Sorted List Operation Array Linked add(newEntry) remove(anEntry) getPosition(anEntry) getEntry(givenPosition) contains(anEntry) remove(givenPosition) display() clear(), getLength(), isEmpty(), isFull() O(n) O(1) Fig. 13-5 The worst-case efficiencies of the operations on the ADT sorted list for two implementations

An Implementation That Uses the ADT List Use the list as a data field within the class that implements the sorted list public class SortedList implements SortedListInterface { private ListInterface list; public SortedList() { list = new LList(); } // end default constructor . . .// implement add, remove, and getPosition } // end SortedList

An Implementation That Uses the ADT List Fig. 13-6 An instance of a sorted list that contains a list of its entries.

An Implementation That Uses the ADT List Fig. 13-7 A sorted list in which Jamie belongs after Carlos but before Sarah.

public boolean add(Comparable newEntry) { int newPosition = Math.abs(getPosition(newEntry)); return list.add(newPosition, newEntry); } // end add public boolean remove(Comparable anEntry) { boolean result = false; int position = getPosition(anEntry); if (position > 0) { list.remove(position); result = true; } // end if return result; } // end remove

public int getPosition(Comparable anEntry) { int position = 1; int length = list.getLength(); // determine position of anEntry while ( (position <= length) && (anEntry.compareTo(list.getEntry(position)) > 0) ) { position++; } // end while // determine whether anEntry is in list if ((position>length) || (anEntry.compareTo(list.getEntry(position)) != 0)) { position = -position; // anEntry is not in list } // end if return position; } // end getPosition

Efficiency Issues ADT List Operation Array Linked getEntry(givenPosition) add(newPosition, newEntry) remove(givenPosition) contains(anEntry) display() clear(),getLength(),isEmpty(), isFull() O(1) O(n) Fig. 13-8 The worst-case efficiencies of selected ADT list operations for array-based and linked implementations

Efficiency Issues ADT List Operation Array Linked add(newEntry) remove(anEntry) getPosition(anEntry) getEntry(givenPosition) contains(anEntry) remove(givenPosition) display() clear(), getLength(), isEmpty(),isFull() O(n) O(1) O(n2) Fig. 13-9 The worst-case efficiencies of the ADT sorted list operations when implemented using an instance of the ADT LIST

Efficiency Issues When you use an instance of an ADT list to represent entries in ADT sorted list Must use the list's operations to access sorted lists entries Do not access them directly Direct access leads to inefficient implementation of sorted list Underlying list uses a chain of linked nodes to store entries

Inheritance and Lists Chapter 14

Chapter Contents Using Inheritance to Implement a Sorted List Designing a Base Class An Efficient Implementation of a Sorted List The Method add

Using Inheritance to Implement a Sorted List Have a revised SortedList class which inherits from LList public class SortedList extends LList implements SortedListInterface { public boolean add(Comparable newEntry) { int newPosition = Math.abs(getPosition(newEntry)); return super.add(newPosition, newEntry); } // end add < Implementations of remove(anEntry) and getPosition(anEntry) go here. > . . . } // end SortedList

Using Inheritance to Implement a Sorted List Problem It inherits two methods which, if used could destroy the order of the entries in a sorted list Possible solutions Declare sorted list as an instance of SortedListInterface Implement the add and replace within SortedList, but have them return false Implement them but have them throw an exception public boolean add(int newPosition, Object newEntry); public boolean replace(int givenPosition, Object newEntry);

Designing a Base Class Fig. 14-1 A derived class of the class LList cannot access or change anything that is private within LList

Protected Access You can access a protected method or data field of a given class by name only … Within its own class definition Within a class derived from that class Within any class in the same package as that class

Changes to LList Declare firstNode and length to be private Provide protected methods setLength (?) getFirstNode incrementLength setFirstNode decrementLength Make getNodeAt protected Make class Node and its constructors protected Add protected methods setData getData setNextNode getNextNode

Designing a Base Class Fig. 14-2 Access available to a class derived from the class LinkedListBase

Efficient Implementation of a Sorted List The class LinkedListBase enables faster manipulation of the list's underlying data structure We want the class to extend LinkedListBase public class SortedList extends LinkedListBase implements SortedListInterface

The Method add public boolean add(Comparable newEntry) { Node newNode = new Node(newEntry); Node nodeBefore = getNodeBefore(newEntry); if (isEmpty() || nodeBefore == null) { newNode.setNextNode(getFirstNode()); setFirstNode(newNode); } else { Node nodeAfter = nodeBefore.getNextNode(); newNode.setNextNode(nodeAfter); nodeBefore.setNextNode(newNode); } // end if incrementLength(); return true; } // end add

Efficiency Improved add method is O(n) getPosition is O(n2) Improved add method is O(n) The class designer should use inheritance and maintain efficiency Requires that base class provided protected access to underlying data structure