IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture22.

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

Garfield AP Computer Science
1 Various Methods of Populating Arrays Randomly generated integers.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture3.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture24.
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.
Sorting I Chapter 8 Kruse and Ryba. Introduction Common problem: sort a list of values, starting from lowest to highest. –List of exam scores –Words of.
CSC 2300 Data Structures & Algorithms March 16, 2007 Chapter 7. Sorting.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 24 Sorting.
Sorting Algorithms and Average Case Time Complexity
Sorting Part 2 CS221 – 3/4/09. Announcements Midterm: 3/11 – 15% of your total grade – We will review in class on 3/9 – You can bring one sheet of paper.
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Sorting Chapter 10.
Algorithm Efficiency and Sorting
Analysis of Algorithms CS 477/677
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting CS 202 – Fundamental Structures of Computer Science II Bilkent.
Data Structures/ Algorithms and Generic Programming Sorting Algorithms.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
By: Vishal Kumar Arora AP,CSE Department, Shaheed Bhagat Singh State Technical Campus, Ferozepur. Different types of Sorting Techniques used in Data Structures.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Computer Science Searching & Sorting.
Sorting Chapter 10. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
Chapter 7: Sorting Algorithms Insertion Sort. Sorting Algorithms  Insertion Sort  Shell Sort  Heap Sort  Merge Sort  Quick Sort 2.
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Comparison-Based Sorting & Analysis Smt Genap
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture17.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Searching and Sorting.
CSCI 51 Introduction to Programming March 12, 2009.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
SORTING Chapter 8 CS Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
SORTING ALGORITHMS King Saud University College of Applied studies and Community Service CSC 1101 By: Nada Alhirabi 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
CSE 143 Lecture 16 Sorting reading: 13.1, slides created by Marty Stepp
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Data Structures Arrays and Lists Part 2 More List Operations.
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.
Data Structures and Algorithms Instructor: Tesfaye Guta [M.Sc.] Haramaya University.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting.
Chapter 23 Sorting Jung Soo (Sue) Lim Cal State LA.
Prof. U V THETE Dept. of Computer Science YMA
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Lecture 14 Searching and Sorting Richard Gesick.
CSC 222: Object-Oriented Programming
Lecture 11 Searching and Sorting Richard Gesick.
Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed
Sub-Quadratic Sorting Algorithms
Sorting Chapter 8.
slides created by Marty Stepp
CSE 373 Data Structures and Algorithms
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Comparison-Based Sorting & Analysis

2 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Several sorting algorithms: Bubble Sort Selection Sort Insertion Sort Shell Sort For each algorithm: Basic Idea Example Implementation Algorithm Analysis Outline

3 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Sorting Sorting = ordering. Sorted = ordered based on a particular way. Generally, collections of data are presented in a sorted manner. Examples of Sorting: Words in a dictionary are sorted (and case distinctions are ignored). Files in a directory are often listed in sorted order. The index of a book is sorted (and case distinctions are ignored). Many banks provide statements that list checks in increasing order (by check number). In a newspaper, the calendar of events in a schedule is generally sorted by date. Musical compact disks in a record store are generally sorted by recording artist. Why? Imagine finding the phone number of your friend in your mobile phone, but the phone book is not sorted.

4 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Bubble Sort: Idea Idea: bubble in water. Bubble in water moves upward. Why? How? When a bubble moves upward, the water from above will move downward to fill in the space left by the bubble.

5 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Bubble Sort: Example Notice that at least one element will be in the correct position each iteration.

6 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb Bubble Sort: Example Stop here… why?

7 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Bubble Sort: Implementation void sort(int a[]){ for (int i = a.length; i>=0; i--) { boolean swapped = false; for (int j = 0; j<i; j++) {... if (a[j] > a[j+1]) { int T = a[j]; a[j] = a[j+1]; a[j+1] = T; swapped = true; }... } if (!swapped) return; }

8 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Bubble Sort: Analysis Running time: Worst case: O(N 2 ) Best case: O(N) -- when? why? Variant: bi-directional bubble sort original bubble sort: only works to one direction bi-directional bubble sort: works back and forth.

9 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Selection Sort: Idea 1. We have two group of items: sorted group, and unsorted group 2. Initially, all items are in the unsorted group. The sorted group is empty. We assume that items in the unsorted group unsorted. We have to keep items in the sorted group sorted. 3. Select the “best” (eg. smallest) item from the unsorted group, then put the “best” item at the end of the sorted group. 4. Repeat the process until the unsorted group becomes empty.

10 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb Selection Sort: Example

11 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb Selection Sort: Example

12 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb Selection Sort: Example

13 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Selection Sort: Implementation void sort(int a[]) throws Exception { for (int i = 0; i < a.length; i++) { int min = i; int j; /* Find the smallest element in the unsorted list */ for (j = i + 1; j < a.length; j++)... } if (a[j] < a[min]) { min = j; }... }

14 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Selection Sort: Implementation /* Swap the smallest unsorted element into the end of the sorted list. */ int T = a[min]; a[min] = a[i]; a[i] = T;... }

15 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Selection Sort: Analysis Running time: Worst case: O(N 2 ) Best case: O(N 2 ) Based on big-oh analysis, is selection sort better than bubble sort? Does the actual running time reflect the analysis?

16 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Insertion Sort: Idea Idea: sorting cards. 8 | | | | | |

17 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Insertion Sort: Idea 1. We have two group of items: sorted group, and unsorted group 2. Initially, all items in the unsorted group and the sorted group is empty. We assume that items in the unsorted group unsorted. We have to keep items in the sorted group sorted. 3. Pick any item from, then insert the item at the right position in the sorted group to maintain sorted property. 4. Repeat the process until the unsorted group becomes empty.

18 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb Insertion Sort: Example

19 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb Insertion Sort: Example

20 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb Insertion Sort: Example

21 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb Insertion Sort: Example

22 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Insertion Sort: Implementation Insertion sort to sort an array of integers public static void insertionSort (int[] a) { for (int ii = 1; ii < a.length; ii++) { int jj = ii; while (( jj > 0) && (a[jj] < a[jj - 1])) { int temp = a[jj]; a[jj] = a[jj - 1]; a[jj - 1] = temp; jj--; } Note: value of a[jj] always the same  possibility for improvement of efficiency.

23 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Insertion Sort: Efficient Implementation A slightly more efficient Insertion sort public static void insertionSort2 (int[] a) { for (int ii = 1; ii < a.length; ii++) { int temp = a[ii]; int jj = ii; while (( jj > 0) && (temp < a[jj - 1])) { a[jj] = a[jj - 1]; jj--; } a[jj] = temp; }

24 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Insertion Sort: Analysis Running time analysis: Worst case: O(N 2 ) Best case: O(N) Is insertion sort faster than selection sort? Notice the similarity and the difference between insertion sort and selection sort.

25 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 A Lower Bound Bubble Sort, Selection Sort, Insertion Sort all have worst case of O(N 2 ). Turns out, for any algorithm that exchanges adjacent items, this is the best worst case: Ω(N 2 ) In other words, this is a lower bound! See proof in Section 8.3 of Weiss

26 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb Original: 5-sort: Sort items with distance 5 element: Shell Sort: Idea Donald Shell (1959): Exchange items that are far apart!

27 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb Original: After 5-sort: After 3-sort: Shell Sort: Example After 1-sort:

28 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Shell Sort: Gap Values Gap: the distance between items being sorted. As we progress, the gap decreases. Shell Sort is also called Diminishing Gap Sort. Shell proposed starting gap of N/2, halving at each step. There are many ways of choosing the next gap.

29 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 O(N 3/2 )?O(N 5/4 )? O(N 7/6 )? Shell Sort: Analysis So we have 3 nested loops, but Shell Sort is still better than Insertion Sort! Why?

30 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Generic Sort So far we have methods to sort integers. What about Strings? Employees? Cookies? A new method for each class? No! In order to be sorted, objects should be comparable (less than, equal, greater than). Solution: use an interface that has a method to compare two objects. Remember: A class that implements an interface inherits the interface (method definitions) = interface inheritance, not implementation inheritance.

31 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 The Comparable Interface In Java, generic aspect of “comparable” is defined in an interface in package java.lang : public interface Comparable { public int compareTo (Object ob); } method compareTo returns: negative integer: the object (this) is smaller than the parameter ‘ob’ 0: the object is equal to the parameter ‘ob’ positive integer: the object (this) is greater than the parameter ‘ob’

32 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Interface: Example public class CircleComparable extends Circle implements Comparable { public CircleComparable (double r) {super (r);} public int compareTo (Object other) { CircleComparable otherCircle = (CircleComparable) other; if (radius < otherCircle.getRadius ()) return -1; else if (radius > otherCircle.getRadius ()) return 1; else return 0; }

33 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Insertion Sort: Generic Sort Generic Insertion sort public static void insertionSort3 (Comparable[] a) { for (int ii = 1; ii < a.length; ii++) { Comparable temp = a[ii]; int jj = ii; while (( jj > 0) && (temp.compareTo (a[jj - 1]) < 0)) { a[jj] = a[jj - 1]; jj--; } a[jj] = temp; }

34 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 import java.util.*; public class SortCircle { public static void main (String args[]) { CircleComparable[] ling = new CircleComparable[20]; Random generator = new Random (); for (int ii = 0; ii < ling.length; ii++) { ling[ii] = new CircleComparable ( 1 + generator.nextInt (100)); System.out.print (ling[ii].getRadius () + " "); } System.out.println (); Sort.insertionSort3 (ling); for (int ii = 0; ii < ling.length; ii++) { System.out.print (ling[ii].getRadius () + " "); } System.out.println (); }

35 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb 2007 Other kinds of sort Merge Sort Quick Sort Heap sort. We will discuss this after tree. Postman sort / Radix Sort. etc.

36 Ruli Manurung (Fasilkom UI)IKI10100: Lecture22 nd Feb /resources/animation/ Weiss book, chapter 8: Sorting Algorithm Further Reading