Presentation is loading. Please wait.

Presentation is loading. Please wait.

SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following.

Similar presentations


Presentation on theme: "SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following."— Presentation transcript:

1 SORTING Chapter 8 CS340 1

2 Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting algorithms:  selection sort  bubble sort  insertion sort  Shell sort  merge sort  heapsort  quicksort  To understand the differences in performance of these algorithms, and which to use for small, medium arrays, and large arrays CS340 2

3 Introduction  Sorting entails arranging data in order  Familiarity with sorting algorithms is an important programming skill  The study of sorting algorithms provides insight  into problem solving techniques such as divide and conquer  into the analysis and comparison of algorithms which perform the same task CS340 3

4 Using Java Sorting Methods  The Java API provides a class Arrays with several overloaded sort methods for different array types  The Collections class provides similar sorting methods for Lists  Sorting methods for arrays of primitive types are based on the quicksort algorithm  Sorting methods for arrays of objects and Lists are based on the merge sort algorithm  Both algorithms are O(n log n) CS340 4

5 Declaring a Generic Method CS340 5

6 Declaring a Generic Method (cont.)  Sample declarations: public static void sort(T[] items, Comparator comp) represents the generic parameter for the sort method CS340 6

7 Declaring a Generic Method (cont.)  Sample declarations: public static void sort(T[] items, Comparator comp) should also appear in the method parameter list CS340 7

8 Declaring a Generic Method (cont.)  Sample declarations: public static void sort(T[] items, Comparator comp) The second method parameter means that comp must be an object that implements the Comparator interface for type T or for a superclass of type T CS340 8

9 Declaring a Generic Method (cont.)  Sample declarations: public static void sort(T[] items, Comparator comp) For example, you can define a class that implements Comparator and use it to sort an array of Integer objects or an array of Double objects CS340 9

10 Declaring a Generic Method (cont.)  Sample declarations: public static > void sort(List list) > means that generic parameter T must implement the interface Comparable CS340 10

11 Declaring a Generic Method (cont.)  Sample declarations: public static > void sort(List list) The method parameter list (the object being sorted) is of type List CS340 11

12 Section 8.2 Selection Sort CS340 12

13 Selection Sort  Selection sort is relatively easy to understand  It sorts an array by making several passes through the array, selecting a next smallest item in the array each time and placing it where it belongs in the array  While the sort algorithms are not limited to arrays, throughout this chapter we will sort arrays for simplicity  All items to be sorted must be Comparable objects, so, for example, any int values must be wrapped in Integer objects CS340 13

14 Trace of Selection Sort n = number of elements in the array 1.for fill = 0 to n – 2 do 2.Set posMin to the subscript of the smallest item in the subarray starting at subscript fill 3.Exchange the item at posMin with the one at fill 35 65 30 60 20 n5 fill posMin 01234 CS340 14

15 Trace of Selection Sort (cont.) n = number of elements in the array 1.for fill = 0 to n – 2 do 2.Set posMin to the subscript of the smallest item in the subarray starting at subscript fill 3.Exchange the item at posMin with the one at fill 35 65 30 60 20 n5 fill0 posMin fill 01234 CS340 15

16 Trace of Selection Sort (cont.) n = number of elements in the array 1.for fill = 0 to n – 2 do 2.Set posMin to the subscript of the smallest item in the subarray starting at subscript fill 3.Exchange the item at posMin with the one at fill 35 65 30 60 20 n5 fill0 posMin4 fillposMin 01234 CS340 16

17 Trace of Selection Sort (cont.) n = number of elements in the array 1.for fill = 0 to n – 2 do 2.Set posMin to the subscript of the smallest item in the subarray starting at subscript fill 3.Exchange the item at posMin with the one at fill 20 65 30 60 35 n5 fill0 posMin4 fillposMin 01234 CS340 17

18 Trace of Selection Sort (cont.) n = number of elements in the array 1.for fill = 0 to n – 2 do 2.Set posMin to the subscript of the smallest item in the subarray starting at subscript fill 3.Exchange the item at posMin with the one at fill 20 65 30 60 35 n5 fill1 posMin4 fillposMin 01234 CS340 18

19 Trace of Selection Sort (cont.) n = number of elements in the array 1.for fill = 0 to n – 2 do 2.Set posMin to the subscript of the smallest item in the subarray starting at subscript fill 3.Exchange the item at posMin with the one at fill 20 65 30 60 35 n5 fill1 posMin2 fill posMin 01234 CS340 19

20 Trace of Selection Sort (cont.) n = number of elements in the array 1.for fill = 0 to n – 2 do 2.Set posMin to the subscript of the smallest item in the subarray starting at subscript fill 3.Exchange the item at posMin with the one at fill 20 30 65 60 35 n5 fill1 posMin2 fill posMin 01234 CS340 20

21 Trace of Selection Sort (cont.) n = number of elements in the array 1.for fill = 0 to n – 2 do 2.Set posMin to the subscript of the smallest item in the subarray starting at subscript fill 3.Exchange the item at posMin with the one at fill 20 30 65 60 35 n5 fill2 posMin2 fill posMin 01234 CS340 21

22 Trace of Selection Sort (cont.) n = number of elements in the array 1.for fill = 0 to n – 2 do 2.Set posMin to the subscript of the smallest item in the subarray starting at subscript fill 3.Exchange the item at posMin with the one at fill 20 30 65 60 35 n5 fill2 posMin4 fillposMin 01234 CS340 22

23 Trace of Selection Sort (cont.) n = number of elements in the array 1.for fill = 0 to n – 2 do 2.Set posMin to the subscript of the smallest item in the subarray starting at subscript fill 3.Exchange the item at posMin with the one at fill 20 30 35 60 65 n5 fill2 posMin4 fillposMin 01234 CS340 23

24 Trace of Selection Sort (cont.) n = number of elements in the array 1.for fill = 0 to n – 2 do 2.Set posMin to the subscript of the smallest item in the subarray starting at subscript fill 3.Exchange the item at posMin with the one at fill 20 30 35 60 65 n5 fill3 posMin4 fill posMin 01234 CS340 24

25 Trace of Selection Sort (cont.) n = number of elements in the array 1.for fill = 0 to n – 2 do 2.Set posMin to the subscript of the smallest item in the subarray starting at subscript fill 3.Exchange the item at posMin with the one at fill 20 30 35 60 65 n5 fill3 posMin3 fill posMin 01234 CS340 25

26 Trace of Selection Sort (cont.) n = number of elements in the array 1.for fill = 0 to n – 2 do 2.Set posMin to the subscript of the smallest item in the subarray starting at subscript fill 3.Exchange the item at posMin with the one at fill 20 30 35 60 65 n5 fill3 posMin3 fill posMin 01234 CS340 26

27 Trace of Selection Sort (cont.) n = number of elements in the array 1.for fill = 0 to n – 2 do 2.Set posMin to the subscript of the smallest item in the subarray starting at subscript fill 3.Exchange the item at posMin with the one at fill 20 30 35 60 65 n5 fill3 posMin3 01234 CS340 27

28 Trace of Selection Sort Refinement 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 35 65 30 60 20 n5 fill posMin next 01234 CS340 28

29 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 35 65 30 60 20 n5 fill0 posMin next 01234 fill CS340 29

30 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 35 65 30 60 20 n5 fill0 posMin0 next 01234 fill posMin CS340 30

31 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 35 65 30 60 20 n5 fill0 posMin0 next1 01234 fill posMin next CS340 31

32 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 35 65 30 60 20 n5 fill0 posMin0 next1 01234 fill posMin next CS340 32

33 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 35 65 30 60 20 n5 fill0 posMin0 next2 01234 fill posMin next CS340 33

34 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 35 65 30 60 20 n5 fill0 posMin0 next2 01234 fill posMin next CS340 34

35 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 35 65 30 60 20 n5 fill0 posMin2 next2 01234 fill posMin next CS340 35

36 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 35 65 30 60 20 n5 fill0 posMin2 next3 01234 fill posMin next CS340 36

37 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 35 65 30 60 20 n5 fill0 posMin2 next3 01234 fill posMin next CS340 37

38 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 35 65 30 60 20 n5 fill0 posMin2 next4 01234 fill posMin next CS340 38

39 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 35 65 30 60 20 n5 fill0 posMin2 next4 01234 fill posMin next CS340 39

40 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 35 65 30 60 20 n5 fill0 posMin4 next4 01234 fill posMin next CS340 40

41 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 65 30 60 35 n5 fill0 posMin4 next4 01234 fill posMin next CS340 41

42 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 65 30 60 35 n5 fill1 posMin4 next4 01234 fill posMin next CS340 42

43 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 65 30 60 35 n5 fill1 posMin1 next4 01234 fill posMin next CS340 43

44 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 65 30 60 35 n5 fill1 posMin1 next2 01234 fill posMin next CS340 44

45 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 65 30 60 35 n5 fill1 posMin1 next2 01234 fill posMin next CS340 45

46 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 65 30 60 35 n5 fill1 posMin2 next2 01234 fill posMin next CS340 46

47 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 65 30 60 35 n5 fill1 posMin2 next3 01234 fill posMin next CS340 47

48 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 65 30 60 35 n5 fill1 posMin2 next3 01234 fill posMin next CS340 48

49 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 65 30 60 35 n5 fill1 posMin2 next4 01234 fill posMin next CS340 49

50 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 65 30 60 35 n5 fill1 posMin2 next4 01234 fill posMin next CS340 50

51 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 30 65 60 35 n5 fill1 posMin2 next4 01234 fill posMin next CS340 51

52 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 30 65 60 35 n5 fill2 posMin2 next4 01234 fill posMin next CS340 52

53 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 30 65 60 35 n5 fill2 posMin2 next4 01234 fill posMin next CS340 53

54 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 30 65 60 35 n5 fill2 posMin2 next3 01234 fill posMin next CS340 54

55 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 30 65 60 35 n5 fill2 posMin2 next3 01234 fill posMin next CS340 55

56 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 30 65 60 35 n5 fill2 posMin3 next3 01234 fill posMin next CS340 56

57 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 30 65 60 35 n5 fill2 posMin3 next4 01234 fill posMin next CS340 57

58 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 30 65 60 35 n5 fill2 posMin3 next4 01234 fill posMin next CS340 58

59 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 30 65 60 35 n5 fill2 posMin4 next4 01234 fill posMin next CS340 59

60 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 30 35 60 65 n5 fill2 posMin4 next4 01234 fill posMin next CS340 60

61 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 30 35 60 65 n5 fill3 posMin4 next4 01234 fill posMin next CS340 61

62 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 30 35 60 65 n5 fill3 posMin3 next4 01234 fill posMin next CS340 62

63 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 30 35 60 65 n5 fill3 posMin3 next4 01234 fill posMin next CS340 63

64 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 30 35 60 65 n5 fill3 posMin3 next4 01234 fill posMin next CS340 64

65 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 30 35 60 65 n5 fill3 posMin3 next4 01234 fill posMin next CS340 65

66 Trace of Selection Sort Refinement (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill 20 30 35 60 65 n5 fill3 posMin3 next4 01234 CS340 66

67 Analysis of Selection Sort 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill This loop is performed n-1 times CS340 67

68 Analysis of Selection Sort (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill There are n-1 exchanges CS340 68

69 Analysis of Selection Sort (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill This comparison is performed (n – 1 - fill) times for each value of fill and can be represented by the following series: (n-1) + (n-2) +... + 3 + 2 + 1 This comparison is performed (n – 1 - fill) times for each value of fill and can be represented by the following series: (n-1) + (n-2) +... + 3 + 2 + 1 CS340 69

70 Analysis of Selection Sort (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill CS340 70

71 Analysis of Selection Sort (cont.) 1.for fill = 0 to n – 2 do 2.Initialize posMin to fill 3.for next = fill + 1 to n – 1 do 4.if the item at next is less than the item at posMin 5.Reset posMin to next 6.Exchange the item at posMin with the one at fill For very large n we can ignore all but the significant term in the expression, so the number of comparisons is O(n 2 ) exchanges is O(n) An O(n 2 ) sort is called a quadratic sort For very large n we can ignore all but the significant term in the expression, so the number of comparisons is O(n 2 ) exchanges is O(n) An O(n 2 ) sort is called a quadratic sort CS340 71

72 Code for Selection Sort (cont.)  Listing 8.1( SelectionSort.java, pages 426 - 427) CS340 72

73 Making Sort Methods Generic  To avoid a warning message about an unchecked call to compareTo, change the method heading to public static > void sort(T[] table { and change the variable temp from Comparable to type T T temp = table[fill]; CS340 73

74 Section 8.3 Bubble Sort CS340 74

75 Bubble Sort  Also a quadratic sort  Compares adjacent array elements and exchanges their values if they are out of order  Smaller values bubble up to the top of the array and larger values sink to the bottom; hence the name CS340 75

76 Trace of Bubble Sort 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 60 42 75 83 27 [0] [1] [2] [3] [4] CS340 76

77 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 60 42 75 83 27 [0] [1] [2] [3] [4] pass1 exchanges made0 CS340 77

78 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 42 60 75 83 27 [0] [1] [2] [3] [4] pass1 exchanges made1 CS340 78

79 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 42 60 75 83 27 [0] [1] [2] [3] [4] pass1 exchanges made1 CS340 79

80 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 42 60 75 83 27 [0] [1] [2] [3] [4] pass1 exchanges made1 CS340 80

81 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 42 60 75 83 27 [0] [1] [2] [3] [4] pass1 exchanges made1 CS340 81

82 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 42 60 75 27 83 [0] [1] [2] [3] [4] pass1 exchanges made1 CS340 82

83 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 42 60 75 27 83 [0] [1] [2] [3] [4] pass1 exchanges made2 At the end of pass 1, the last item (index [4]) is guaranteed to be in its correct position. There is no need to test it again in the next pass CS340 83

84 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 42 60 75 27 83 [0] [1] [2] [3] [4] pass2 exchanges made0 CS340 84

85 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 42 60 75 27 83 [0] [1] [2] [3] [4] pass2 exchanges made0 CS340 85

86 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 42 60 75 27 83 [0] [1] [2] [3] [4] pass2 exchanges made0 CS340 86

87 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 42 60 27 75 83 [0] [1] [2] [3] [4] pass2 exchanges made1 CS340 87

88 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 42 60 27 75 83 [0] [1] [2] [3] [4] pass2 exchanges made1 CS340 88

89 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 42 60 27 75 83 [0] [1] [2] [3] [4] pass3 exchanges made0 CS340 89

90 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 42 60 27 75 83 [0] [1] [2] [3] [4] pass3 exchanges made0 CS340 90

91 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 42 27 60 75 83 [0] [1] [2] [3] [4] pass3 exchanges made1 CS340 91

92 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 42 27 60 75 83 [0] [1] [2] [3] [4] pass3 exchanges made1 CS340 92

93 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 42 27 60 75 83 [0] [1] [2] [3] [4] pass4 exchanges made0 CS340 93

94 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 27 42 60 75 83 [0] [1] [2] [3] [4] pass4 exchanges made1 CS340 94

95 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 27 42 60 75 83 [0] [1] [2] [3] [4] pass4 exchanges made1 Where n is the length of the array, after the completion of n – 1 passes (4, in this example) the array is sorted CS340 95

96 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 27 42 60 75 83 [0] [1] [2] [3] [4] pass4 exchanges made1 CS340 96

97 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 27 42 60 75 83 [0] [1] [2] [3] [4] pass4 exchanges made1 Where n is the length of the array, after the completion of n – 1 passes (4, in this example) the array is sorted CS340 97

98 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 27 42 60 75 83 [0] [1] [2] [3] [4] pass4 exchanges made1 Sometimes an array will be sorted before n – 1 passes. This can be detected if there are no exchanges made during a pass through the array CS340 98

99 Trace of Bubble Sort (cont.) 1.do 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.while the array in not sorted 27 42 60 75 83 [0] [1] [2] [3] [4] pass4 exchanges made1 The algorithm can be modified to detect exchanges (next) CS340 99

100 Trace of Bubble Sort (cont.) 1.Initialize exchanges to false 2.for each pair of adjacent array elements 3.if the values in a pair are out of order 4.Exchange the values 5.Set exchanges to true 27 42 60 75 83 [0] [1] [2] [3] [4] pass4 exchanges made1 The algorithm can be modified to detect exchanges CS340 100

101 Analysis of Bubble Sort  The number of comparisons and exchanges is represented by (n – 1) + (n – 2) +... + 3 + 2 + 1  Worst case:  number of comparisons is O(n 2 )  number of exchanges is O(n 2 )  Compared to selection sort with its O(n 2 ) comparisons and O(n) exchanges, bubble sort usually performs worse  If the array is sorted early, the later comparisons and exchanges are not performed and performance is improved CS340 101

102 Analysis of Bubble Sort (cont.)  The best case occurs when the array is already sorted  one pass is required (O(n) comparisons)  no exchanges are required (O(1) exchanges)  Bubble sort works best on arrays nearly sorted and worst on inverted arrays (elements are in reverse sorted order) CS340 102

103 Code for Bubble Sort  Listing 8.2 ( BubbleSort.java, pages 430 - 431) CS340 103


Download ppt "SORTING Chapter 8 CS340 1. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following."

Similar presentations


Ads by Google