Topic 7 Standard Algorithms Learning Objectives Describe and exemplify the following standard algorithms in pseudocode and an appropriate high level.

Slides:



Advertisements
Similar presentations
Part IV: Memory Management
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
CSE Lecture 3 – Algorithms I
Algorithms (continued)
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
CompSci Searching & Sorting. CompSci Searching & Sorting The Plan  Searching  Sorting  Java Context.
Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Merge sort, Insertion sort
Computer Programming Sorting and Sorting Algorithms 1.
Algorithm Efficiency and Sorting
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort or bubble sort 1. Find the minimum value in the list 2. Swap it with the value.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Analysis of Algorithms CS 477/677
Searching and Sorting Arrays
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
By D.Kumaragurubaran Adishesh Pant
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.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Chapter 19: Searching and Sorting Algorithms
Searching and Sorting Chapter Sorting Arrays.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
Standard Algorithms –search for an item in an array –count items in an array –find the largest (or smallest) item in an array.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort (iterative, recursive?) * Bubble sort.
CSC 211 Data Structures Lecture 13
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Data Structure Introduction.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
3 – SIMPLE SORTING ALGORITHMS
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
SORTING & SEARCHING - Bubble SortBubble Sort - Insertion SortInsertion Sort - Quick SortQuick Sort - Binary SearchBinary Search 2 nd June 2005 Thursday.
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.
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.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Data Structures and Algorithms Lecture 17, 18 and 19 (Sorting) Instructor: Quratulain Date: 10, 13 and 17 November, 2009 Faculty of Computer Science, IBA.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
1 Algorithms Searching and Sorting Algorithm Efficiency.
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
Searching and Sorting Algorithms
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
IGCSE 6 Cambridge Effectiveness of algorithms Computer Science
Linear and Binary Search
Algorithms Chapter 3 With Question/Answer Animations
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Principles of Computing – UFCFA3-30-1
Principles of Computing – UFCFA3-30-1
Presentation transcript:

Topic 7 Standard Algorithms

Learning Objectives Describe and exemplify the following standard algorithms in pseudocode and an appropriate high level language Binary search Describe and compare simple linear and binary search algorithms Describe and compare sort algorithms for simple sort, bubble sort and selection sort in terms of number of comparisons and use of memory Describe and exemplify user-defined module libraries

Linear Search

Simplest search method to implement Scanning takes place from left to right until the search key is found Search key is 76

Linear Search Algorithm 1. Set found to false 2. Input search key 3. Point to first element in list 4. Do while (not end of list) and (not found) 5. if array(value) = key then 6. found=true 7. output suitable message 8. else 9. look at next element in list 10. end if 11. loop 12. If (not found) then 13. key not in list 14. End if

Linear Search Not a bad algorithm for short lists Easier to implement than other methods List does not need to be sorted Might be only method for large unordered tables of data and files Inefficient since each array element has to be compared with search key until a match is found

Analysis One comparison required to find target at start of list Two comparisons for target in second position etc Maximum comparisons is N for a list of N items Therefore average number of comparisons is N/2

Exercise Implement the Linear search algorithm given on page 145 in VB 2005

Binary Search

Faster method BUT list must be ordered Sometimes called a binary chop as it splits the data list into two sublists and repeats the process until a search key is found

Binary Search Example

Binary Search Example Search Key is 90

Binary Search Example Left ListRight List Mid Value

Binary Search Example Mid Value Left ListRight List

Binary Search Example Mid Value Left List Right List Target Found

Binary Search Algorithm - ascending 1. Set found=false 2. Set first_location to start of list 3. Set last_location to end of list 4. Input search target 5. Repeat 6. Set pointer to middle of list…. integer(first+last)/2 7. If array(middle)=target then 8. found=true 9. Output suitable message 10. Else 11. if array(middle)>target then 12. last_location=middle else 14. first_location = middle end if 16. End if 17. Until found = true or first>last

Exercise 1 With a partner, use the cards given to exemplify the binary search algorithm Use cards for different search keys Make sure that you know how this algorithm works

Exercise 2 Implement the algorithm given on page 150 You cannot use code given on next pages as version of VB is different!

Summary of Searches Linear SearchBinary Search Is simple to code and implementIs more complex to code Quite efficient for short length data lists Efficient for any length of data list Very slow on large lists since each data element has to be compared Fast on any length of data list since it only deals with half sub-lists. Hence the name is binary chop Does not require data to be orderedData has to be ordered Average search length is N/2 where N is the number of data elements Search length is log 2 N Plays a part in other algorithms such as finding maximum, minimum and also in selection sort Binary chop is used in fast searching routines

Sorting

Important process in computing, especially in data processing Telephone directories Sports league tables Lottery numbers Etc.

Sorting Efficient sorting is important to optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly; it is also often useful for canonicalizing data and for producing human- readable output.sortingsearch merge canonicalizing

Sorting Since the dawn of computing, the sorting problem has attracted a great deal of research, perhaps due to the complexity of solving it efficiently despite its simple, familiar statement.

Sorting External Sorts External storage devices used Large amounts of data Internal Sorts Fairly small lists Uses internal memory (RAM)

Sorting Three algorithms described and compared 1. Simple sort 2. Bubble sort 3. Selection sort using two lists

Simple Sort In the first pass, each item in the list is compared with the first item in the list If the first item in the list is bigger then the item being compared then they are swapped.

Simple Sort st Comparison Swap

Simple Sort

nd Comparison

Simple Sort rd Comparison

Simple Sort th Comparison Swap

Simple Sort th Comparison

Simple Sort th Comparison

Simple Sort th Comparison Swap

Simple Sort

th Comparison

Simple Sort th Comparison

Simple Sort st Comparison

Simple Sort nd Comparison Swap

Simple Sort

rd Comparison Swap

Simple Sort

th Comparison

Simple Sort th Comparison Swap

Simple Sort

And so on…

Simple Sort until…

Simple Sort 1. Performs fewer exchanges on a randomly ordered list 2. Must make N-1 passes through list even when fully sorted or partially sorted

Simple Sort Algorithm 1. for outer = 1 to n 2. for inner = outer + 1 to n 3. if List (outer) > List(inner) then 4. swap values 5. end if 6. next inner 7. next outer

Simple Sort Task Using the cards provided and With a partner Sort the cards into ascending order using the simple sort methd

Simple Sort Task Using the cards provided and With a partner Sort the cards into ascending order using the simple sort method

Bubble sort First Comparison Swap

Bubble sort

Second Comparison

Bubble sort Third Comparison Swap

Bubble sort

Fourth Comparison Swap

Bubble sort

Fifth Comparison Swap

Bubble sort

Sixth Comparison Swap

Bubble sort

Seventh Comparison Swap

Bubble sort

th Comparison Swap

Bubble sort

th Comparison Swap

Bubble sort Notice… we are sorting list into an ascending list. The largest number is now at the end of the list…where it should be! This completes the first pass through the list.

The process begins again. 1st Comparison Second Pass

Bubble sort nd Comparison Swap Second Pass

Bubble sort Second Pass

Bubble sort rd Comparison Swap Second Pass

Bubble sort Second Pass

Bubble sort th Comparison Second Pass

Bubble sort th Comparison Swap Second Pass

Bubble Sort 1. for outer = 1 to n-1 2. for inner = 0 to N if list(inner) > list(inner + 1) then 4. swap values 5. end if 6. next inner 7. next outer

Bubble Sort 1. Makes excessive exchanges (but less so in a partially ordered list). 2. Works best on a partially ordered list 3. Can detect when sorted as no swaps take place. 4. Most inefficient when list is randomly ordered

Bubble Sort task Using the cards provided and With a partner Sort the cards into ascending order using the bubble sort method

Selection Sort This version uses two lists…

Selection Sort

X34 0 After 1 st pass

Selection Sort 7596X82X34 01 After 2 nd pass

Selection Sort 7596X8XX After 3 rd pass

Selection Sort 7596X8XXX After 4 th pass

Selection Sort 7596X8XXXX After 5 th pass

Selection Sort 7X96X8XXXX After 6 th pass

Selection Sort 7X9XX8XXXX After 7 th pass

Selection Sort XX9XX8XXXX After 8 th pass

Selection Sort XX9XXXXXXX After 9 th pass

Selection Sort XXXXXXXXXX After 10 th pass

Selection Sort 1. for outer = 1 to n-1 2. minimum = outer 3. for inner = 0 to N {line modified for two lists} 4. if list_A(inner) < list_A(minimum) then 5. minimum = inner 6. end if 7. next inner 8. list_B(outer) = list_A(minimum) 9. list_A(minimum) = dummy value 10. next outer

Selection Sort 1. Makes excessive use of memory as two lists required.

Selection Sort Task Using the cards provided and With a partner Sort the cards into ascending order using the selection sort method

Summary of three sorting algorithms The criteria for measuring algorithm performance are – 1. Behaviour with different size lists 2. Memory requirements 3. Stability

Summary of three sorting algorithms Simple sortBubble sortSelection sort using two lists ComparisonsN(N-1)/2N x N PassesNNNegligible MemoryNegligible Small UsesSmall ListsNoneLists stabilityStable

Summary of three sorting algorithms Partially ordered list – use Bubble Sort Randomly ordered list – use Simple Sort Simplicity of implementation – use Selection Sort

User-defined Module Libraries

Module Library Depositaries of useful software procedures, functions, subroutines, programs, applications, OS routines Objects Classes Type declarations Etc.

Module Library If they are all packaged as a DLL file (dynamic link library) then they can be used within most programming environments simply by calling them up Windows itself is composed of many DLL files A DLL contains executable code and will link to a programming application at run time rather than at compile time.

Exercise Create a new folder and call it Module Library Work through the worked examples on page 169 onwards