Presentation is loading. Please wait.

Presentation is loading. Please wait.

EFFICIENCY & SORTING CITS1001. Listen to the sound of sorting Various algorithms Quicksort

Similar presentations


Presentation on theme: "EFFICIENCY & SORTING CITS1001. Listen to the sound of sorting Various algorithms Quicksort"— Presentation transcript:

1 EFFICIENCY & SORTING CITS1001

2 Listen to the sound of sorting Various algorithms http://www.youtube.com/watch?v=t8g-iYGHpEA Quicksort http://www.youtube.com/watch?v=m1PS8IR6Td0 Or Google for “sound of sorting” 2

3 3 Scope of this lecture Linear Search Sorting algorithms and efficiency References: Wirth, Algorithms + Data Structures = Programs, Chapter 2 Knuth, The Art of Computer Programming, Volume 3, Sorting and Searching This lecture is based on powerpoint slides originally by Gordon Royle, UWA

4 4 Searching Searching refers to the process of finding data items that match certain criteria We may just want a yes/no answer to a question, or we may want additional details as well Find out whether any students got a mark of 49 Find out which students got a mark of 49 The simplest searching technique is called linear search, which involves looking through each element in turn until we find one that matches the criteria

5 5 Our favourite student class public class Student { private String studentID; private int mark; public Student(String studentID, int mark) { this.studentID = studentID; this.mark = mark; } public String getStudentID() { return studentID; } public int getMark() { return mark; } A skeleton version of a possible Student class in a student records system

6 6 A collection of students We consider a class list being stored as an ArrayList The question we consider is how to retrieve the data for a student with a given student number So we will write a method with the following signature public Student findStudent(ArrayList classlist, String id) The method returns a (reference to a) Student object The arraylist of students is a parameter The student ID we want is the other parameter

7 7 Linear search public Student findStudent(ArrayList classlist, String id) { for (Student s : classlist ) if (s.getStudentID().equals(id)) return s; return null; }

8 8 Comments If the arraylist does contain the desired value, the method returns true as soon as it is found If the arraylist does not contain the desired value, the method returns false after checking every element of the collection without success We have shown the general situation of finding an object in a collection of objects

9 9 Performance of linear search How fast does linear search work on an collection of n items? We can identify three situations Best case, when the input is the most convenient possible Worst case, when the input is the least convenient possible Average case, averaged over all the inputs In the best case, linear search finds the item at the first position of the array, so it needs 1 comparison In the worst case, linear search does not find the item, so it performs n comparisons unsuccessfully To calculate the average case performance, we would need some problem-specific assumptions about the input data

10 10 Linear search is too slow If we have very large amounts of data, then linear search is not feasible For example, we can view a telephone directory as a very large array of objects, with each object consisting of a name and a number If you are asked to find out which person has phone number 9388 6105, how long would it take you to do this by linear search? However, if I ask you to find out the phone number of a specific person, then you can do it much, much faster How do you do it? How can we program a computer to do this?

11 11 Sorted collections The reason that is quick, while is slow is because the collection (i.e. the phone book) is sorted into alphabetical order, and somehow this allows us to find an entry much more quickly (we will see why later) Most useful databases are sorted – dictionaries, indexes, etc. Name Phone number Name Phone number

12 12 Sorting Before we examine how to efficiently search in a sorted collection, we consider how to sort the collection We again start with the “plain vanilla” example – sorting an array of integers into increasing order Later we will extend this to sorting arrays of objects according to various other criteria (alphabetical, etc.) 6811512274 124678 15 before after

13 13 The basic set up We will implement a number of sorting methods, all of which operate on an array of integers We will develop these as a utility class called Sorter – a class with no instance variables, but just static methods (cf. Math ) Each method will have a similar signature, where the only thing that will vary is the name of the sorting technique public static void nameSort(int[] a) Each method receives an array as a parameter, and will sort that array “in place” i.e. the method returns nothing, but a gets updated

14 14 bubbleSort The idea behind bubbleSort is to systematically compare pairs of elements, exchanging them if they are out of order If the array contains n elements, then we view the algorithm as consisting of n–1 “passes” In the first pass we compare Element 0 with element 1, exchange if necessary Element 1 with element 2, exchange if necessary … Element n  2 with element n  1, exchange if necessary

15 15 The first pass After the first pass, the largest element will be at the end 68115122746811512274618 15274618 122746181512274618 2157461812271546181227415

16 16 The second pass The second pass doesn’t need to make the last comparison 6181227415168122741516821274151681227415168122741516827124151682741215

17 17 The third pass The third pass can omit the last two comparisons 168274121516827412151627841215168274121516287412151627481215

18 18 The fourth pass The fourth pass is even shorter 16274812151627481215126478121512674812151267481215

19 19 The fifth and sixth passes 1264781215126478121512647812151246781215124678121512467812151246781215

20 20 Why does it work? We need to have some argument or “proof” that this works We claim that This is true after the first pass, because the largest element in the array is encountered at some stage and then “swapped all the way to the end” of the array The same argument – applied to the remainder of the array – shows that the second pass puts the second largest element into place; repeating this argument n times gives the result After i passes, the largest i elements in the array are in their correct positions

21 21 Coding bubblesort public static void bubbleSort(int[] a) { for (int pass = 1; pass < a.length; pass++) for (int j = 0; j < a.length-pass; j++) if (a[j] > a[j+1]) swap(a, j, j+1); }

22 22 Sorting students public static void bubbleSort(Student[] a) { for (int pass = 1; pass < a.length; pass++) for (int j = 0; j < a.length-pass; j++) if (/* a[j] and a[j+1] out of order */) swap(a, j, j+1); } Almost identical code, except that we need to get the right boolean condition to check when two students are in the “wrong order”

23 23 What order do we want? The precise form of the statement depends on whether we want to sort students: Alphabetically according to their studentId Numerically according to their mark In addition, the desired sort could be ascending (smaller values first) or descending (smaller values last) Suppose that we want to sort the students into normal (ascending) alphabetical order by studentId

24 24 For alphabetic order The comparison between the two Student objects a[j] and a[j+1] first needs to obtain the two ids to compare, so it will involve the two Strings String s1 = a[j].getStudentID(); String s2 = a[j+1].getStudentID(); To compare two String s we use the compareTo method if (s1.compareTo(s2) > 0) { // Swap the two Students }

25 25 Selection sort When sorting n items, Selection Sort works as follows The procedure has n–1 stages Select the smallest element in the array, and swap it with the element in position 0 Then select the smallest element in the array starting from position 1, and swap it with the element in position 1 Then select the smallest element in the array starting from position 2, and swap it with the element in position 2 Etc. This algorithm has the following properties After i stages, the first i items in the array are the i smallest items, in order At the (i+1) th stage, the (i+1) th smallest item is placed in the (i+1) th slot in the array

26 26 Coding selectionSort public static void selectionSort(int[] a) { for (int pass = 0; pass < a.length – 1; pass++) { int smallest = pass; for (int j = pass + 1; j < a.length; j++) if (a[j] < a[smallest]) smallest = j; swap(a, smallest, pass); }

27 27 Insertion Sort (like sorting cards) In card games, it is common to pick up your cards as they are dealt, and to sort them into order as they arrive For example, suppose your first three cards are Next you pick up a 9 of clubs

28 28 Inserting a card The new card is then inserted into the correct position

29 29 Insertion sort We can develop this idea into an algorithm called Insertion Sort When sorting n items, Insertion Sort works as follows The procedure has n–1 stages Compare the second item in the array with the first item; make them ordered Compare the third item in the array with the first two items; make them ordered Etc. This algorithm has the following properties After i stages, the first i+1 items are sorted although they aren’t the smallest At the (i+1) th stage, the item originally in position i+2 is placed in its correct position relative to the first i+1 items

30 30 Example Initial array Stage 0: Move the first element into position (do nothing) Stage 1: Examine the second element and insert it into position (again do nothing) 6811512274 6811512274 6811512274

31 31 Stage 2 This element is out of position, so it will have to be inserted 6811512274 6811512274 6811512274 6811512274 6811512274

32 32 Stages 3 & 4 6811512274 6811512274 6811512274 6811512274 6811512274 Stage 3 Stage 4

33 33 Stage 5 6811512274 6811512274 6811512274 6811512274 6811512274 6811512274 6811512274

34 34 Stage 6 6811512274 6811512274 6811512274 6811512274 6811512274 6811512274

35 35 Final stage 6811512274 6811512274 6811512274 6811512274 6811512274 6811512274 6811512274 6811512274

36 36 Code for insertionSort public static void insertionSort(int[] a) { for (int pass = 1; pass < a.length; pass++) { int tmp = a[pass]; // new element to insert int pos = pass - 1; // move out-of-order elements up to make space while (pos >= 0 && a[pos] > tmp) { a[pos+1] = a[pos]; pos--; } // insert the new element in the right place a[pos+1] = tmp; }

37 37 public static void insertionSort(int[] a) { for (int pass = 1; pass < a.length; pass++) { int tmp = a[pass]; int pos = pass-1; while (pos >= 0 && a[pos] > tmp) { a[pos+1] = a[pos]; pos--; } a[pos+1] = tmp; } Code dissection The body of the for-loop contains the code for one stage or “pass” of the algorithm

38 38 public static void insertionSort(int[] a) { for (int pass=1;pass<a.length; pass++) { int tmp = a[pass]; int pos = pass-1; while (pos >= 0 && a[pos] > tmp) { a[pos+1] = a[pos]; pos--; } a[pos+1] = tmp; } Code dissection The variable tmp stores the value that is to be inserted; the variable pos will eventually contain the position where it should be inserted

39 39 public static void insertionSort(int[] a) { for (int pass=1;pass<a.length; pass++) { int tmp = a[pass]; int pos = pass-1; while (pos >= 0 && a[pos] > tmp) { a[pos+1] = a[pos]; pos--; } a[pos+1] = tmp; } Code dissection This code does the work of shifting each element in turn one space along if it is bigger than the value to be inserted. We also need to ensure that we don’t fall off the left-hand end of the array!

40 40 public static void insertionSort(int[] a) { for (int pass=1;pass<a.length; pass++) { int tmp = a[pass]; int pos = pass-1; while (pos >= 0 && a[pos] > tmp) { a[pos+1] = a[pos]; pos--; } a[pos+1] = tmp; } Code dissection The while loop finishes when we have found the correct position for a[pass], so it is now inserted into this position

41 41 public static void insertionSort(int[] a) { for (int pass=1;pass<a.length; pass++) { int tmp = a[pass]; int pos = pass-1; while (pos >= 0 && a[pos] > tmp) { a[pos+1] = a[pos]; pos--; } a[pos+1] = tmp; } Code dissection Note that if a[pass] is already in the correct spot, the while loop does nothing and a[pass] goes back into the same place!


Download ppt "EFFICIENCY & SORTING CITS1001. Listen to the sound of sorting Various algorithms Quicksort"

Similar presentations


Ads by Google