Chapter 3: Searching and Sorting, Algorithmic Efficiency

Similar documents
Sorting. Introduction. Classification

Programming in OOP/C++

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

Question 7.11 Show how heapsort processes the input:

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Algorithmic problem-solving: Lecture 2. Algorithmic problem-solving: Tractable vs Intractable problems. Based on Part V of the course textbook.

Operations. Priority Queues & Heaps. Prio-Q as Array. Niave Solution: Prio-Q As Array. Prio-Q as Sorted Array. Prio-Q as Sorted Array

Sorting: Overview/Questions

Heaps and Priority Queues

8.2 Quicksort. 332 Chapter 8. Sorting

Properties of a heap (represented by an array A)

CSC 8301 Design and Analysis of Algorithms: Exhaustive Search

Algorithm classification

Data Structures Lesson 9

Computer Algorithms. Introduction to Algorithm

Lecture 3. Brute Force

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

12/30/2013 S. NALINI,AP/CSE

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

Recursion & Performance. Recursion. Recursion. Recursion. Where Recursion Shines. Breaking a Problem Down

Priority Queues (Heaps)

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

Measuring algorithm efficiency

Topics for CSCI 151 Final Exam Wednesday, May 10

Partha Sarathi Manal

Lecture 15: Algorithms. AP Computer Science Principles

For 100% Result Oriented IGNOU Coaching and Project Training Call CPD: ,

Time Complexity of an Algorithm

You may refer to the lesson on data structures (Introduction to Data Structures) as necessary.

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

Heapsort. Why study Heapsort?

6. Asymptotics: The Big-O and Other Notations

Lecture 7. Transform-and-Conquer

IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech

A data structure and associated algorithms, NOT GARBAGE COLLECTION

Arrays. Week 4. Assylbek Jumagaliyev

Algorithms Interview Questions

Programming II (CS300)

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Practice Problems for the Final

CS 216 Fall 2007 Final Exam Page 1 of 10 Name: ID:

Introduction to Algorithms: Brute-Force Algorithms

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Algorithm Design and Analysis. What is an Algorithm? Do Algorithms Matter?

Data Structures - Test 2

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

8 Algorithms 8.1. Foundations of Computer Science Cengage Learning

Programming II (CS300)

Fast Bit Sort. A New In Place Sorting Technique. Nando Favaro February 2009

1 Interlude: Is keeping the data sorted worth it? 2 Tree Heap and Priority queue

1. Answer the following questions about each code fragment below. [8 points]

CS 360 Exam 1 Fall 2014 Solution. 1. Answer the following questions about each code fragment below. [8 points]

Algorithms and Data Structures

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1


Programming II (CS300)

1. Answer the following questions about each code fragment below. [8 points]

Searching, Sorting. Arizona State University 1

What is an algorithm?

CH 8. HEAPS AND PRIORITY QUEUES

Heaps, Heapsort, Priority Queues

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

ECE 122. Engineering Problem Solving Using Java

Chapter4: Data Structures. Data: It is a collection of raw facts that has implicit meaning.

Array. Arijit Mondal. Dept. of Computer Science & Engineering Indian Institute of Technology Patna IIT Patna 1

CH. 8 PRIORITY QUEUES AND HEAPS

Algorithm Analysis. This is based on Chapter 4 of the text.

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

CS 310 B-trees, Page 1. Motives. Large-scale databases are stored in disks/hard drives.

Introduction to Programming in C Department of Computer Science and Engineering

COMP 103 RECAP-TODAY. Priority Queues and Heaps. Queues and Priority Queues 3 Queues: Oldest out first

What is sorting? Lecture 36: How can computation sort data in order for you? Why is sorting important? What is sorting? 11/30/10

OF VICTORIA EXAMINATIONS- DECEMBER 2010 CSC

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Heaps and Priority Queues

O(n): printing a list of n items to the screen, looking at each item once.

Sorting algorithms Properties of sorting algorithm 1) Adaptive: speeds up to O(n) when data is nearly sorted 2) Stable: does not change the relative

CmpSci 187: Programming with Data Structures Spring 2015

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

Sorting. Bubble sort method. Bubble sort properties. Quick sort method. Notes. Eugeniy E. Mikhailov. Lecture 27. Notes. Notes

Basic Data Structures (Version 7) Name:

Week 12: Running Time and Performance

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

Chapter 3. The Efficiency of Algorithms INVITATION TO. Computer Science

(Refer Slide Time: 00:50)

CS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee

lecture notes September 2, How to sort?

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

Data Structures. Chapter 04. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Discussion. What problems stretch the limits of computation? Compare 4 Algorithms. What is Brilliance? 11/11/11

Transcription:

1 Chapter 3: Searching and Sorting, Algorithmic Efficiency Search Algorithms Problem: Would like an efficient algorithm to find names, numbers etc. in lists/records (could involve DATA TYPES). Basic Search: Unsorted Lists Purpose: To find a name in an unsorted list by comparing one at a time until the name is found or the end of the array is reached. See Example 4 in the example programs for a basic program to do this. Searching through data types requires some different syntax... Example: Search through data types for a record corresponding to name Zelda Suppose each name and number is stored in a data type type person character(len=30)::name integer::number end type person We declare an array of person(s) type(person)::directory(1000) Then search for Zelda. Do i=1,1000 if (directory(i)%name== Zelda ) then print *,directory(i)%number end if End do This algorithm is fine for an unsorted list of names. However, if we know that the list has been stored in some order (i.e. it is sorted) then there are faster methods.

2 Binary Search: Sorted Lists Purpose: To search through an ordered array more quickly. (Note that <, >=, == etc. can be used with character strings to compare alphabetical order). Algorithm: 1. Data: The name to be searched for is placed in a alphabetically ordered array Namearray that is filled from data files. 2. Set Low=1 Low, High integers Set High=N where N is Size(Namearray) 3. Compare Name with Namearray(Low) and Namearray(High). If there is a match, then exit. Else Namearray(Low) < Name < Namearray(High). Set Mid=(Low+High)/2!Note integer division, so Mid is an integer N/2. 4. Compare Name with Namearray(Mid) if (Name < Namearray(Mid) ) High=Mid Mid=(High+Low)/2!Sets Mid N/4 if (Name == Namearray(Mid) ), We are finished. if (Name > Namearray(Mid) ) Low=Mid Mid=(High+Low)/2!Sets Mid 3N/4 5. Repeat Step 4 above until name is found or array is exhausted. 6. Print out results.

3 Algorithmic Efficiency Suppose an algorithm handles N pieces of data. Let the time needed to complete the calculation be Z(N). Z(N) will be a function of both the speed of the computer and the dataset used. Z(N) may refer either to the worst case scenario or the average over a large number of cases. Define g(n) to be the order of Z(N)...i.e. Z(N) = O( g(n) ) or Example: Z(N) lim = const 0. N g(n) Z(N) = 3N 3 + 20N 2 + 7N + 3 then g(n) = N 3 Often, algorithms are classified using simple functions, e.g., polynomial or power g(n) = N a exponential g(n) = exp Na logarthmic g(n) = log 2 N factorial g(n) = N! Example: Search Algorithms Unsorted: Average N/2, Worst Case N. Sorted (binary search): Average (1/2) log 2 N, Worst Case: log 2 N. The Travelling Salesman Problem Problem: A salesman must visit N cities (in any order). Which order minimises travel distances? The brute force approach to this problem is an example of a factorial algorithm (as there are N! possible combinations to check). The brute force method is therefore crippling slow even for fairly modest N. It is a classic problem in computational science /mathematics to come up with faster algorithms.

4 1 3 7 5 6 11 9 6 18 10 24 16 12 13 20 Figure: An example of a partially ordered heap. Sorting Algorithms Problem: Algorithm must sort names or numbers in an array OLDARRAY into a particular order. Algorithm: Sorting Algorithms: Insertion Sort 1. Create NEWARRAY 2. One at a time, place elements of OLDARRAY at a correct position in NEWARRAY. The simple way of doing this is to start at the top of the new (sorted) array and go downwards until place is found (see e.g. Examples 10, 10a). This algorithm has efficiency g(n)=o(n 2 ). Idea: 2 steps Sorting Algorithms: Heapsort 1. Partially sort data into a heap...a partially sorted binary tree. 2. Complete the sort. In a heap, the top level has one element, second level 2 elements, nth level 2 n 1 elements. The last level need not be complete. Each number in the heap is greater than or equal to its immediate boss, i.e. the number directly above it in the tree.

5 3 5 7 6 6 11 9-18 10 24 16 12 13 20 Figure: The partially ordered heap above after one retirement and all the subsequent promotions. Algorithm: Store the heap in an array A, A(1)- top element, A(2) A(3) - second row etc. Step 2 first: To completely order a partially ordered heap 1. Create a new array B. Let B(1)=A(1). 2. Next step is to retire A(1) and promote the lesser of A(2) and A(3) to the top job. 3. Promote one of the underlings of A(2) or A(3) to their vacant job. 4. Keep going until vacancy reaching bottom. 5. Return to 2 above and retire the top number into the next available space in B, until the whole array is sorted and B is filled.. Now Step 1: To create a partially ordered heap from randomly arranged data. Suppose A is initially filled in a completely random way. The idea here is to create the heap from the bottom row upwards. Note that the bottom row, because they don t have any workers below, already satisfy the heap condition that each boss has a lower value than its two workers. 1. Go through the next-to-bottom row one at a time. Where one or both workers have a lower value than their boss, promote the lower-valued worker and demote the boss. The next-to-bottom and bottom rows now satisfy the heap condition. 2. Look on the two-from-bottom row. Again, when a boss with a higher value than one of its workers is found the two are swapped. 3. This time the demoted boss must be checked in the next-to-bottom row to see if it needs further demotion. This must be done before moving on to check the next boss in the two-from-bottom row. 4. Continue until the bottom three rows all satisfy the heap condition. 5. Move on to the three-from bottom row, and continue, making sure that each boss is demoted all the way down to the bottom row if necessary.

6 6. Continue until the whole array satisfies the heap condition. The key point about the HEAPSORT algorithm is that has efficiency Z(N)=O(Nlog 2 N), as opposed to O(N 2 ) for the insertion sort algorithm. This means that for large N it is much, much faster, and so it becomes more than worthwhile to deal with the extra complexity.