Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting. RHS – SWC 2 Sorting Searching in sorted data is much faster than searching in unsorted data Being able to sort data efficiently is thus a quite.

Similar presentations


Presentation on theme: "Sorting. RHS – SWC 2 Sorting Searching in sorted data is much faster than searching in unsorted data Being able to sort data efficiently is thus a quite."— Presentation transcript:

1 Sorting

2 RHS – SWC 2 Sorting Searching in sorted data is much faster than searching in unsorted data Being able to sort data efficiently is thus a quite important ability But how fast can be sort data…?

3 RHS – SWC 3 Selection sort A very simple algorithm for sorting an array of n integers works like this: –Search the array from element 0 to element (n-1), to find the smallest element –If the smallest element is element i, then swap element 0 and element i –Now repeat the process from element 1 to element (n-1) –…and so on…

4 RHS – SWC 4 Selection sort 1056264827634186040

5 RHS – SWC 5 Selection sort 1056264827634186040

6 RHS – SWC 6 Selection sort 1056263482764186040

7 RHS – SWC 7 Selection sort 1056263482764186040

8 RHS – SWC 8 Selection sort 1056263482764186040

9 RHS – SWC 9 Selection sort 1056263482764186040

10 RHS – SWC 10 Selection sort 1056263482764186040

11 RHS – SWC 11 Selection sort 1018263440564607682

12 RHS – SWC 12 Selection sort How fast is selection sort? We scan for the smallest element n times –In scan 1, we examine n element –In scan 2, we examine (n-1) element –…and so on A total of n + (n -1) + (n – 2) +…+ 2 + 1 examinations The sum is n(n + 1)/2

13 RHS – SWC 13 Selection sort The total number of examinations is equal to n(n + 1)/2 = (n 2 + n)/2 The run-time complexity of selection sort is therefore O(n 2 ) O(n 2 ) grows fairly fast…

14 RHS – SWC 14 Selection sort nn2n2 24 525 20400 502500 20040000

15 RHS – SWC 15 Merge sort Selection sort is conceptually very simple, but not very efficient… A different algorithm for sorting is merge sort Merge sort is an example of a divide-and- conquer algorithm It is also a recursive algorithm

16 RHS – SWC 16 Merge sort The principle in merge sort is to merge two already sorted arrays: 1026345618404607682

17 RHS – SWC 17 Merge sort 1026345618404607682

18 RHS – SWC 18 Merge sort Merging two sorted arrays is pretty simple, but how did the arrays get sorted…? Recursion to the rescue! Sort the two arrays simply by appying merge sort to them… If the array has length 1 (or 0), it is sorted

19 RHS – SWC 19 Merge sort public void sort() // Sort the array a { if (a.length <= 1) return; // Base case int[] a1 = new int[a.length/2];// Create two new int[] a2 = new int[a.length – a1.length];// arrays to sort System.arraycopy(a,0,a1,0,a1.length);// Copy data to System.arraycopy(a,a1.length,a2,0,a2.length);// the new arrays MergeSorter ms1 = new MergeSorter(a1);// Create two new MergeSorter ms2 = new MergeSorter(a2);// sorter objects ms1.sort();// Sort the two ms2.sort();// new arrays merge(a1,a2);// Merge the arrays }

20 RHS – SWC 20 Merge sort All that is left is the method for merging two arrays A little bit tedious, but as such trivial… Time needed to merge two arrays to the total length of the arrays, i.e to n We can now analyse the run-time com- plexity for merge sort

21 RHS – SWC 21 Merge sort Merge sort of an array of length n requires –Two merge sorts of arrays of length n/2 –Merging two arrays of length n/2 The running time T(n) then becomes: T(n) = 2×T(n/2) + n

22 RHS – SWC 22 Merge sort If we re-insert the expression for T(n) into itself m times, we get T(n) = 2 m ×T(n/2 m ) + mn If we choose m such that n = 2 m, we get T(n) = n×T(1) + mn = n + n×log(n)

23 RHS – SWC 23 Merge sort The run-time complexity of merge sort is therefore O(n log(n)) Many other sorting algorithms have this run-time complexity This is the fastest we can sort, except under very special circumstances Much better than O(n 2 )…

24 RHS – SWC 24 Merge sort nn log(n)n2n2 224 51225 2086400 502822500 200152940000

25 RHS – SWC 25 Sorting in practice It does matter which sorting algorithm you use… …but do I have to code sorting algorithms myself? No! You can – and should – use sorting algorithms found in the Java library

26 RHS – SWC 26 Sorting in practice Sorting an array: Car[] cars = new Car[n]; … Arrays.sort(cars);

27 RHS – SWC 27 Sorting in practice Sorting an arraylist: ArrayList cars = new ArrayList (); … Collections.sort(cars);

28 RHS – SWC 28 Sorting in practice Why not code my own sorting algorithms? Sorting algorithms in Java library are better than anything you can produce… –Carefully debugged –Highly optimised –Used by thousands You cannot beat them

29 RHS – SWC 29 Sorting in practice In order to sort an array of data, we need to be able to compare the elements ”Larger than” should make sense for the elements in the array Easy for numeric types (>) What about types we define ourselves…?

30 RHS – SWC 30 Sorting in practice If a class T implements the Comparable interface, objects of type T can be compared: public interface Comparable { int compareTo(T other); }

31 RHS – SWC 31 Sorting in practice In the interface definition, T is a type parameter It is used the same way as we use an arraylist ArrayList : an arraylist holding elements of type Car

32 RHS – SWC 32 Sorting in practice In order for the sorting algorithms to work properly, an implementation of the interface must obey these rules: The call a.compareTo(b) must return: –A negative number if a < b –Zero if a = b –A positive number if a > b

33 RHS – SWC 33 Sorting in practice The implementation of compareTo must define a so-called total ordering: –Antisymmetric: If a.compareTo(b) ≤ 0, then b.compareTo(a) ≥ 0 –Reflexive: a.compareTo(a) = 0 –Transitive: If a.compareTo(b) ≤ 0 and b.compareTo(c) ≤ 0, then a.compareTo(c) ≤ 0

34 RHS – SWC 34 Sorting in practice public class Car implements Comparable {... // Here using weight as ordering criterion // public int compareTo(Car other) { if (getWeight() < other.getWeight()) return -1; if (getWeight() == other.getWeight()) return 0; return 1; }... }


Download ppt "Sorting. RHS – SWC 2 Sorting Searching in sorted data is much faster than searching in unsorted data Being able to sort data efficiently is thus a quite."

Similar presentations


Ads by Google