C/C++ Programming Lecture 18 Name:

Similar documents
8.1. Chapter 8: Introduction to Search Algorithms. Linear Search. Linear Search. Linear Search - Example 8/23/2014. Introduction to Search Algorithms

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

LECTURE 08 SEARCHING AND SORTING ARRAYS

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

LECTURE 17. Array Searching and Sorting

Lecture 6 Sorting and Searching

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

The University Of Michigan. EECS402 Lecture 07. Andrew M. Morgan. Sorting Arrays. Element Order Of Arrays

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Chapter 10 - Notes Applications of Arrays

CS 106 Introduction to Computer Science I

Ch 8. Searching and Sorting Arrays Part 1. Definitions of Search and Sort

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

! Search: find an item in an array, return the. ! Sort: rearrange the items in an array into some. ! There are various methods (algorithms) for

CSE101-lec#19. Array searching and sorting techniques. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms

Sorting/Searching and File I/O. Sorting Searching Reading for this lecture: L&L

Arrays. Week 4. Assylbek Jumagaliyev

SCJ2013 Data Structure & Algorithms. Bubble Sort. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

Searching & Sorting in Java Bubble Sort

Programming in OOP/C++

Search,Sort,Recursion

Chapter Objectives. List Processing. Chapter 10: Applications of Arrays and Strings. Java Programming: Program Design Including Data Structures

Arrays and Applications

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

Outline. What s wrong with Enumerations? Indexing Searching Self-Organizing Vectors Binary Searching Sorting Command line Arguments.

QuickSort. CIS 15 : Spring 2007

Sorting Algorithms. Array Data is being arranged in ascending order using the bubble sort algorithm. #1 #2 #3 #4 #5 #6 #7

Sorting. Quicksort analysis Bubble sort. November 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Data Structures and Algorithms for Engineers

Sorting and Searching

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Sorting Algorithms part 1

Outline. 14. Organization. Programming in Java. Computer Science Dept Va Tech August D Barnette, B Keller & P Schoenhoff

SORTING AND SEARCHING

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

Chapter 8 Search and Sort

Sorting and Searching Algorithms

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

To become familiar with array manipulation, searching, and sorting.

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

CSc 110, Spring 2017 Lecture 39: searching

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

Solutions to Chapter 8

Sorting. Bringing Order to the World

Tail Recursion: working from the beginning towards the end.

Module 08: Searching and Sorting Algorithms

Data Structures - Test 2

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Sorting/Searching and File I/O

1 a = [ 5, 1, 6, 2, 4, 3 ] 4 f o r j i n r a n g e ( i + 1, l e n ( a ) 1) : 3 min = i

Plan of the lecture. Quick-Sort. Partition of lists (or using extra workspace) Quick-Sort ( 10.2) Quick-Sort Tree. Partitioning arrays

ECE 242 Fall 13 Exam I Profs. Wolf and Tessier

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

Fun facts about recursion

Recursive Algorithms. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Chapter 10 Sorting and Searching Algorithms

Sorting and Searching -- Introduction

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

(6-2) Efficiency of Algorithms D & D Chapter 20. Instructor - Andrew S. O Fallon CptS 122 (July 11, 2018) Washington State University

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

UNIT 7. SEARCH, SORT AND MERGE

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself? contains a reference to itself.

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself?

Lecture Notes 14 More sorting CSS Data Structures and Object-Oriented Programming Professor Clark F. Olson

Binary Search APRIL 25 TH, 2014

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection

Quadratic: the time that it takes to sort an array is proportional to the. square of the number of elements.

Overview of Sorting Algorithms

Sorting Pearson Education, Inc. All rights reserved.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Searching and Sorting

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Topic 14 Searching and Simple Sorts

LINEAR INSERTION SORT

Unit 10: Sorting/Searching/Recursion

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

Data Structures And Algorithms

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

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

QuickSort

Introduction. Sorting. Table of Contents

Recursion: The Beginning

CSE 143 Lecture 4. Implementing ArrayIntList; Binary Search. reading:

Introduction. Sorting. Definitions and Terminology: Program efficiency. Sorting Algorithm Analysis. 13. Sorting. 13. Sorting.

Sorting and searching arrays

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Computer Science Foundation Exam

Algorithm Analysis. Performance Factors

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

CSE 143 Lecture 2. reading:

Priority queues. Priority queues. Priority queue operations

What is Recursion? ! Each problem is a smaller instance of itself. ! Implemented via functions. ! Very powerful solving technique.

QUIZ. 0] Define arrays 1] Define records 2] How are arrays and records: (a) similar? (b) different?

Lecture 15: Algorithms. AP Computer Science Principles

Searching and Sorting (Savitch, Chapter 7.4)

CmpSci 187: Programming with Data Structures Spring 2015

Computer Science & Engineering 150A Problem Solving Using Computers

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Transcription:

. The following is the textbook's code for a linear search on an unsorted array. //***************************************************************** // The searchlist function performs a linear search on an * // integer array. The array list, which has a maximum of numelems * // elements, is searched for the number stored in value. If the * // number is found, its array subscript is returned. Otherwise, * // - is returned indicating the value was not in the array. * //***************************************************************** int searchlist(const int list[], int numelems, int value) { int index = ; // Used as a subscript to search array int position = -; // To record position of search value bool found = false; // Flag to indicate if the value was found while (index < numelems &&!found) { if (list[index] == value) { // If the value is found found = true; // Set the flag position = index; // Record the value's subscript } // end if index++; } // end while // Go to the next element return position; // Return the position, or - } // end searchlist a) Trace the searchlist code using the following data. 4 5 6 (MAX-) list: 9 5 7 8 4 numelems: 7 value: b) If numelems is,,, then how many comparisons of array items would you expect on a successful search? c) If numelems is,,, then what is the maximun number of comparisons of array items on a successful search? d) If numelems is,,, then how many comparisons of array items would you expect on an unsuccessful search? e) How could we speedup the above code by eliminating the found flag? f) If there are,, names in a phone book, what algorithm do you use to search for a name, say "Smith, John". Lecture 8 Page

. The following is the textbook's code (p. 457) for a binary search on an array sorted in ascending order (items get bigger as we go from left to right). //*************************************************************** // The binarysearch function performs a binary search on an * // integer array. array, which has a maximum of size elements, * // is searched for the number stored in value. If the number is * // found, its array subscript is returned. Otherwise, - is * // returned indicating the value was not in the array. * //*************************************************************** int binarysearch(const int array[], int size, int value) { int first =, // First array element last = size -, // Last array element middle, // Mid point of search position = -; // Position of search value bool found = false; // Flag while (!found && first <= last) { middle = (first + last) / ; if (array[middle] == value) { found = true; position = middle; // Calculate mid point // If value is found at mid } else if (array[middle] > value) { // If value is in lower half last = middle - ; } else { first = middle + ; } // end if } // end while return position; } // end binarysearch a) Trace the binarysearch code using the following data. 4 5 6 (MAX-) array: 4 5 7 8 9 // If value is in upper half size: 7 value: b) If size is,,, then what is the maximun number of comparisons of array items on a successful search? c) If size is,,, then how many comparisons of array items would you expect on an unsuccessful search? d) How could we speedup the above code by eliminating the found flag? Lecture 8 Page

All simple sorts consist of two nested loops where: the outer loop keeps track of the dividing line between the sorted and unsorted part with the sorted part growing by one in size each iteration of the outer loop. the inner loop's job is to do the work to extend the sorted part's size by one. Initially, the sorted part is typically empty. The simple sorts differ in how their inner loops perform their job.. Selection sort is an example of a simple sort. Selection sort s inner loop scans the unsorted part of the array to find the minimum item. The minimum item in the unsorted part is then exchanged with the first unsorted item to extend the sorted part by one item. At the start of the first iteration of the outer loop, the initial array is completely unsorted: Empty 4 5 6 7 8 4 9 6 5 45 The inner loop scans the unsorted part and determines that the index of the minimum item, minindex = 6. 4 5 6 7 8 4 9 6 5 45 firstunsortedindex = minindex = 6 After the inner loop (but still inside the outer loop), the item at minindex is exchanged with the item at firstunsortedindex. Thus, extending the of the array by one item. 4 5 6 7 8 4 9 6 5 45 firstunsortedindex = minindex = 6 a) Write the code for the inner loop b) Write the code to exchange the items Lecture 8 Page

4. Bubble sort is another example of a simple sort. Bubble sort s inner loop scans the unsorted part of the array comparing adjacent items. If it finds adjacent items out of order, then it exchanges them. This causes the largest item to bubble up to the top of the unsorted part of the array. At the start of the first iteration of the outer loop, the initial array is completely unsorted: 4 5 6 7 8 4 9 6 5 45 Empty The inner loop scans the unsorted part by comparing adjacent items and exchanging them if out of order. in order, so don't exchange 4 4 9 5 6 6 7 5 8 45 lastunsortedindex = 8 4 4 9 5 6 6 7 5 8 45 in order, so don't exchange in order, so don't exchange 4 5 6 7 4 6 9 5 8 45 4 4 5 6 7 8 6 9 5 45 4 4 6 5 6 7 8 5 9 45 4 4 6 5 6 7 8 5 45 9 After the inner loop (but still inside the outer loop), there is nothing to do since the exchanges occurred inside the inner loop. a) What would be true if we scanned the unsorted part and didn t need to do any exchanges? Lecture 8 Page 4

At the start of the second interation (after the first iteration) of the outer loop, the sorted part of the array contains one element: (Recall the inner loop scans the unsorted part by comparing adjacent items and exchanging them if out of order.) 4 5 6 7 8 4 6 5 45 9 lastunsortedindex = 7 b) Write the code for the inner, for-loop. c) Add the code for the outer loop to the above code. Lecture 8 Page 5

d) Below is the textbook s code for Bubble sort (page 468). What is wrong with this code? void sortarray(int array[], int size) { bool swap; int temp; do { swap = false; for (int count = ; count < (size - ); count++) { if (array[count] > array[count + ]) { temp = array[count]; array[count] = array[count + ]; array[count + ] = temp; swap = true; } // end if } // end for } while (swap); } // end sortarray e) Correct the above Bubble sort code. Lecture 8 Page 6