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

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture22.
Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Arrays part 2 Applications & such. Returning an array from a method A method can return an array, just like it can return any other kind of variable;
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Sorting1 Sorting Order in the court!. sorting2 Importance of sorting Sorting a list of values is a fundamental task of computers - this task is one of.
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Simple Sorting Algorithms
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )
Computer Programming Sorting and Sorting Algorithms 1.
Algorithm Efficiency and Sorting
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Chapter 8 ARRAYS Continued
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 14 Searching and Sorting Section 1 - Sequential or Linear Search Section 2 - Binary Search Section 3 - Selection Sort Section 4 - Insertion Sort.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
Comparison-Based Sorting & Analysis Smt Genap
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
CSCI 51 Introduction to Programming March 12, 2009.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
3 – SIMPLE SORTING ALGORITHMS
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
ALGORITHMS.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
SORTING ALGORITHMS King Saud University College of Applied studies and Community Service CSC 1101 By: Nada Alhirabi 1.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
The Sorting Methods Lecture Notes 10. Sorts Many programs will execute more efficiently if the data they process is sorted before processing begins. –
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Today’s Material Sorting: Definitions Basic Sorting Algorithms
Shell Sort. Invented by Donald Shell in 1959, the shell sort is the most efficient of the O(n²) class of sorting algorithms. Of course, the shell sort.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
CSCI 51 Introduction to Programming March 10, 2009.
1 Applied Arrays Lists and Strings Chapter 12 2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching and Sorting Searching algorithms with simple arrays
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Efficiency & SORTING CITS1001.
Lecture 14 Searching and Sorting Richard Gesick.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Simple Sorting Algorithms
Describing algorithms in pseudo code
Outline Late Binding Polymorphism via Inheritance
Lecture 11 Searching and Sorting Richard Gesick.
CIS265/506 Simple Sorting CIS265/506: Chapter 03 - Sorting.
CSE 373 Sorting 2: Selection, Insertion, Shell Sort
CS 165: Project in Algorithms and Data Structures Michael T. Goodrich
Module 8 – Searching & Sorting Algorithms
Arrays.
Presentation transcript:

EFFICIENCY & SORTING CITS1001

Listen to the sound of sorting Various algorithms Quicksort Or Google for “sound of sorting” 2

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 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 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 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 Linear search public Student findStudent(ArrayList classlist, String id) { for (Student s : classlist ) if (s.getStudentID().equals(id)) return s; return null; }

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 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 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 , 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 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 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.) before after

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 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 The first pass After the first pass, the largest element will be at the end

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

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

18 The fourth pass The fourth pass is even shorter

19 The fifth and sixth passes

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 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 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 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 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 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 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 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 Inserting a card The new card is then inserted into the correct position

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

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

32 Stages 3 & Stage 3 Stage 4

33 Stage

34 Stage

35 Final stage

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 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 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 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 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 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!