1 sorting sorting is the process of arranging a list of items in a particular order the sorting...

36
1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test scores in ascending numeric order Sorting a list of people alphabetically by last name There are many algorithms, which vary in efficiency, for sorting a list of items 1

Upload: gyles-reed

Post on 16-Dec-2015

229 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

1

Sorting• Sorting is the process of arranging a list of items in

a particular order

• The sorting process is based on specific value(s)– Sorting a list of test scores in ascending numeric order

– Sorting a list of people alphabetically by last name

• There are many algorithms, which vary in efficiency, for sorting a list of items

1

Page 2: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

2

Polymorphism in Sorting (Another Way to Do It)

• A class that implements the Comparable interface defines a compareTo method that returns the relative order of its objects

• We can use polymorphism to develop a generic sort for any set of Comparable objects

• The sorting method accepts as a parameter an array of Comparable objects

• That way, one method can be used to sort a group of People, or Books, or whatever as long as the class implements Comparable

2

Page 3: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Comparable<T> Interface

3

• Comparable<T> Interface requires one method:int compareTo (T that) // compares self (“this”) to “that”

• Returns an int value that is:Negative when “this” object is less than “that” object

Zero when “this” object is equal to “that” object

Positive when “this” object is greater than “that” object

• Most useful when there is only one logical or “inherent” ordering for the generic <T> objects

3

Page 4: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Sorting with Comparable

• Unlike sorting with Comparator, there is no need for a separate class containing the logic for comparison of two objects

• The compareTo method in the class of the objects being sorted does the comparison

• Example of usage: • “this” String = “Hello” and “that” String = “World”

int order = “Hello”.compareTo(“World”); // value = -15

4

Page 5: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Making County Objects Comparable

• We could have used County objects that implemented Comparable in Project 2

• We would need to modify the County class – Add the implements Comparable clause– Add the compareTo() method code

• This would limit us to one order of sorting

• Arbitrarily, we could choose population as the inherent order for sorting

5

Page 6: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Making County objects Comparablepublic class County implements Comparable<T>

{

private String name;

private String state;

private int population;

. . .

public int compareTo(County that)

{

return that.population -

this.population;

}

} 6

Page 7: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

7

Sorting with Comparable

• The sorting method doesn't "care" what type of object it is sorting, it just needs to be able to call the compareTo method of that object

• That is guaranteed by using Comparable as the parameter type passed to the sorting method

• Each Comparable class has a compareTo method that determines what it means for one object of that class to be “less than another”

7

Page 8: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Sorting with Comparable

• Class header sort package with Comparable public class SortingAndSearching<T extends Comparable>

Note: use of extends instead of implements• The class header for the objects to be sorted will be: public class MyClass implements Comparable

• Comparable is an Interface so MyClass must implement Comparable - not extend it

• An bit of an inconsistency in Java generics, but it reinforces the idea that implementing an interface has the same semantics as extending a superclass

8

Page 9: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Sorting• Sorting is the process of arranging a list of items in

a particular order

• The sorting process is based on specific value(s)– Sorting a list of test scores in ascending numeric order

– Sorting a list of people alphabetically by last name

• Sequential sorts O(n2): Selection, Insertion, Bubble

• Logarithmic sorts O(nlogn): Quick, Merge

• Now we'll implement the sorts with polymorphic Comparable objects

9

Page 10: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Selection Sort• The approach of Selection Sort:

– Select a value and put it in its final place in the list

– Repeat for all other values

• In more detail:– Find the smallest value in the list

– Switch it with the value in the first position

– Find the next smallest value in the list

– Switch it with the value in the second position

– Repeat until all values are in their proper places10

Page 11: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Selection Sort

• An example:original: 3 9 6 1 2smallest is 1: 1 9 6 3 2smallest is 2: 1 2 6 3 9smallest is 3: 1 2 3 6 9smallest is 6: 1 2 3 6 9

• Each time, the smallest remaining value is found and exchanged with the element in the "next" position to be filled

11

Page 12: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Selection Sort (Comparable)public void selectionSort (T [] data) { int min; T temp; for (int index = 0; index < data.length-1; index++) { min = index; for (int scan = index+1; scan < data.length; scan++) { if (data[scan].compareTo(data[min])<0) min = scan; } swap( data, index, min ); } }

12

Page 13: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Insertion Sort• The approach of Insertion Sort:

– Pick any item and insert it into its proper place in a sorted sublist

– Repeat until all items have been inserted

• In more detail:– Consider the first item to be a sorted sublist (of one item)

– Insert the second item into the sorted sublist, shifting the first item as needed to make room to insert the new addition

– Insert the third item into the sorted sublist (of two items), shifting items as necessary

– Repeat until all values are inserted into their proper positions 13

Page 14: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Insertion Sort

• An example:insert 9: 3 9 6 1 2

insert 6: 3 9 6 1 2

insert 3: 3 6 9 1 2

insert 2: 1 3 6 9 2

finished: 1 2 3 6 9

14

Page 15: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Insertion Sort (Comparable)public void insertionSort (T[] data) { for (int index = 1; index < data.length; index++) { T key = data[index]; int position = index;

/** Shift larger values to the right */ while (position > 0 && data[position-1].compareTo(key) > 0) { data[position] = data[position-1]; position--; } data[position] = key; } }

15

Page 16: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Bubble Sort

• Bubble sort algorithm sorts the values by repeatedly comparing neighboring elements

• It swaps their position if they are not in order

• Each pass through the algorithm moves the largest value to its final position

• A pass may also reposition other elements

• May be more efficient if looping is stopped when no changes were made on last pass

16

Page 17: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Bubble Sort

• An example of one pass:Original: 9 6 8 12 3 1 7Swap 9 & 6: 6 9 8 12 3 1 7Swap 9 & 8: 6 8 9 12 3 1 7No swap: 6 8 9 12 3 1 7Swap 12 & 3: 6 8 9 3 12 1 7Swap 12 & 1: 6 8 9 3 1 12 7Swap 12 & 7: 6 8 9 3 1 7 12

• Each pass moves largest to last position• Each pass can iterate one less time than last

17

Page 18: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Bubble Sort (Comparable) public void bubbleSort (T[] data){ int position, scan; T temp; for (position = data.length - 1; position >= 0; position--) for (scan = 0; scan <= position - 1; scan++) if (data[scan].compareTo(data[scan+1]) > 0) swap( data, scan, scan + 1 ); }

18

Page 19: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

19

Comparing Sorts

• The Selection Sort, Insertion Sort and Bubble Sort algorithms are similar in efficiency

• They both have outer loops that scan all elements, and inner loops that compare the value of the outer loop with almost all values in the list

• Approximately n2 number of comparisons are made to sort a list of size n

• We therefore say that these sorts are of O(n2)

• Other sorts are more efficient: O(n log2 n)19

Page 20: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Quick Sort

• The quick sort algorithm is a “divide and conquer” algorithm

• It compares the data values to a partition element while partitioning into two sub-lists

• Then, it recursively sorts the sub-lists on each side of the partition element

• The recursion base case is a list containing only 1 element (which is inherently sorted)

• A simplistic choice of the partition element is the first element, but that may not be best

20

Page 21: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Quick Sort

90 65 7 305 120 110 8

Initial Data (Select first element as the partition element)

90 65 7 305120 1108

Move all data below partition element value to the leftMove all data above partition element value to the right

Split Point (Only a coincidence that it is in middle)

9065 7 305120 1108

Swap first element with element at the split pointPartition element is now in its correct position

Next Pass Next Pass 21

Page 22: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Quick Sort

• If the list is already (or nearly) sorted, the first element is a poor choice as partition element

• The two partitioned sub-lists are lopsided– One may contain only one element– The other may contain all the other elements

• In this case, the quick sort is not so quick• A better choice might be the middle element

of the list, but note that there is still an initial order for the elements that is “pathological”

22

Page 23: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Merge Sort

• Merge sort is another “divide and conquer” algorithm with a different division strategy– Cut the array of data in half– Sort the left half (recursively calling itself)– Sort the right half (recursively calling itself)– Merge the two sorted halves of the array

• The merge process for two arrays that are already sorted is only O(n) and we perform O(log n) merges

23

Page 24: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Merge Sort

• All comparisons are in the merge process• Copy all elements into a workspace array • Copy them back into the original array

– If there are elements remaining in both halves, compare first elements of each and copy one

– If there are no elements remaining in left half, copy remainder of right half

– If there are no elements remaining in right half, copy remainder of left half

24

Page 25: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

25

Seven Growth Functions

• Seven functions g(n) that occur frequently in the analysis of algorithms (in order of increasing rate of growth relative to n):– Constant 1– Logarithmic log n– Linear n– Log Linear n log n– Quadratic n2

– Cubic n3

– Exponential 2n

Page 26: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

26

Growth Rates Comparedn=1 n=2 n=4 n=8 n=16 n=32

1 1 1 1 1 1 1

logn 0 1 2 3 4 5

n 1 2 4 8 16 32

nlogn 0 2 8 24 64 160

n2 1 4 16 64 256 1024

n3 1 8 64 512 4096 32768

2n 2 4 16 256 65536 4294967296

Page 27: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Introduction to Project 3

• In Project 3, you will experiment with and observe the performance of various sorting algorithms applied to data in different states of organization

• Some of the data will be organized randomly, some will be already sorted, and some will be sorted exactly in the opposite of the desired order

• You will learn that different sort algorithms have different performance in these different situations

27

Page 28: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Introduction to Project 3

• You will be provided the code for the class “SortingAndSearching” which contains multiple sorting methods each of which does a different sort algorithm on Comparable class objects

• You will also be provided a main class that reads a file, sets up data from the file in an array for sorting, invokes the various sort methods, and prints a count of the number of compareTo calls

• You will be provided with some test data files that present different situations for sorting

28

Page 29: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Introduction to Project 3

• You will add code that counts the number of times compareTo is invoked

• We will call this the “instrumentation” code

• There are 3 possible places to put this code– In compareTo method of the Comparable class– In code of SortingAndSearching method(s)– In a “wrapper” class with a compareTo method

that counts calls and calls compareTo of the Comparable class object that it “wraps”

29

Page 30: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Introduction to Project 3

• If you have access to the source code for the Comparable class, you can count each time compareTo gets invoked by the sort

• You need to add methods to reset and read the counter between different sorts

• You can’t do this if you don’t have access to the source code OR

• It’s difficult if you need to do this for many different classes that implement Comparable

30

Page 31: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Introduction to Project 3

• To put instrumentation in the class that implements Comparable

31

ClassName Comparable

SortingClassMainClass

Access resetCount()and getCount() methodsbefore and after each sort

SortingClass is unaware ofthe methods that control thecounting of CompareTo calls

Add instrumentation code here

Sortdata

Page 32: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Introduction to Project 3

• If you have access to the source code for the Sorting class, you can count each time the sort code invokes a compareTo method

• You need to add methods to reset and read the counter between different sorts

• You can’t do this if you don’t have access to the source code, e.g. a sorting class in the standard class library

32

Page 33: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Introduction to Project 3

• To put instrumentation in the Sorting class

3333

ClassName Comparable

SortingClassMainClass

Access resetCount()and getCount() methodsbefore and after each sort

Add instrumentation code here

Sortdata

ClassName is unaware ofthe methods that control thecounting of CompareTo calls

Page 34: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Introduction to Project 3

• If you don’t have access to the source code for the class that implements Comparable or the library sorting class, you “wrap” the Comparable class in another class that controls access to the compareTo method

• The “wrapper” class has methods to reset or read the counter between sorts

• The main method wraps each Comparable class object in the arrays passed to sorting

34

Page 35: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Introduction to Project 3

• To put instrumentation in a wrapper class

ClassName Comparable

SortingClassMainClass

Access resetCount()and getCount() methodsbefore and after each sort

SortingClass is unaware ofthe methods that control thecounting of CompareTo calls

Add instrumentation code here

Sortdata

ClassName is unaware ofthe methods that control thecounting of CompareTo calls

“Wrapper” Class Comparable

Page 36: 1 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of

Introduction to Project 3

• Because I want you to learn something about each sorting algorithm, I am asking you to do this project in the second way

• You will “instrument” the SortingAndSearching code so that you can learn how and where the comparisons are made in sorting algorithm

• Compare your results to the Big-O notation behavior that you expected for each algorithm on each data file and explain your observations

36