Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Sorting

Similar presentations


Presentation on theme: "An Introduction to Sorting"— Presentation transcript:

1 An Introduction to Sorting
Chapter 11

2 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

3 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 …

4 Selection Sort Fig Before and after exchanging shortest book and the first book.

5 Selection Sort Fig A selection sort of an array of integers into ascending order.

6 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]. }

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

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

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

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

11 Fig. 11-4 An insertion sort of books

12 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

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

14 Iterative Insertion Sort
Fig An insertion sort of an array of integers into ascending order

15 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 }

16 Recursive Insertion Sort
Fig 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

17 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

18 Insertion Sort of Chain of Linked Nodes
Fig A chain of integers sorted into ascending order.

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

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

21 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

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

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

24 Shell Sort Fig The subarrays of the array in Fig formed by grouping elements whose indices are 3 apart

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

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

27 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 The time efficiencies of three sorting algorithms, expressed in Big Oh notation.

28 Faster Sorting Methods
Chapter 12

29 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

30 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

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

32 The major steps in a merge sort.

33 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] }

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

35 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);

36 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

37 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 }

38 A partition of an array during a quick sort.

39 A partition strategy for quick sort … continued→

40 A partition strategy for quick sort (continued)

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

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

43 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

44 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

45 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

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

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

48 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 }

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

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

51 Sorted Lists Chapter 13

52 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

53 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

54 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

55 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

56 The Method add Fig Insertion points of names into a sorted chain of linked nodes.

57 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

58 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

59 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()

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

61 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;

62 The Method add Fig Recursively adding a node at the beginning of the chain … continued →

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

64 The Method add Fig Recursively adding a node between existing nodes in a chain … continued →

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

66 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 The worst-case efficiencies of the operations on the ADT sorted list for two implementations

67 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

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

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

70 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

71 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

72 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 The worst-case efficiencies of selected ADT list operations for array-based and linked implementations

73 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 The worst-case efficiencies of the ADT sorted list operations when implemented using an instance of the ADT LIST

74 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

75 Inheritance and Lists Chapter 14

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

77 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

78 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);

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

80 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

81 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

82 Designing a Base Class Fig Access available to a class derived from the class LinkedListBase

83 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

84 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

85 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


Download ppt "An Introduction to Sorting"

Similar presentations


Ads by Google