Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

Garfield AP Computer Science
CSE Lecture 3 – Algorithms I
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
Visual C++ Programming: Concepts and Projects
Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and.
CPS120: Introduction to Computer Science Searching and Sorting.
HST 952 Computing for Biomedical Scientists Lecture 9.
Searching and Sorting Topics  Sequential Search on an Unordered File  Sequential Search on an Ordered File  Binary Search  Bubble Sort  Insertion.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
CHAPTER 11 Sorting.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Searching Arrays Linear search Binary search small arrays
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Searching and Sorting Arrays
1 Two-Dimensional Arrays. 2 Can be visualized as consisting m rows, each of n columns Syntax: datatype arrayname [row] [ column] ; Example: int val[3]
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
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.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Searching and Sorting Topics Sequential Search on an Unordered File
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Chapter 10 Applications of Arrays and Strings. Chapter Objectives Learn how to implement the sequential search algorithm Explore how to sort an array.
Computer Science Searching & Sorting.
Searching and Sorting Chapter Sorting Arrays.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
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.
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.
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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.
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
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.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Searching and Sorting Searching algorithms with simple arrays
Sort Algorithm.
Searching and Sorting Arrays
Lecture 14 Searching and Sorting Richard Gesick.
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Data Structures: Searching
Searching and Sorting Arrays
Searching and Sorting Arrays
Chapter 19 Searching, Sorting and Big O
Algorithm Efficiency and Sorting
Presentation transcript:

Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques

2 Contents Introduction to searching techniques Types of Searching Introduction to sorting techniques Types of Sorting Introducing the concept of analysis of algorithms

3 Objectives At the end of this section, you should be able to: Different Types of search methods –Linear search –Binary search Different Types of Sort Methods –Selection sort –Bubble sort Analysis of algorithms

4 Search Concept Searching is Locating a particular data item in a list of data elements is known as searching. Search takes a problem as input and returns a solution to the problem It is the process of locating data like a word, a file etc There are different Types of search- Few of them are –Linear Search –Binary Search

5 Linear Search One of the Famous Searching Techniques Linear search also as sequential search In Linear search each item is verified one at a time, in sequence, until a matching result is found. It also means check item starting at the beginning of the data and checking each item in turn until either the desired item is found or the end of the data is reached.

6 Linear Search Algorithm 1.Initialize the index to start of array. 2.Repeat through step 4 n times (n - number of elements in the array) 3.Compare search element with current index element. 4.If equal return the index of current element. 5.Return -1 (indicates the element was not found)

7 Binary Search Binary Search - Searches a sorted array by repeatedly dividing the search interval in half. Binary search finds a particular value in a sorted array, by "ruling out" half of the data at each step. Important point to be noted Before Using Binary Search –Requires the input array to be sorted. –Should not be used when array involves lots of insertions and deletions. It is faster than Linear search – since it follows divide and conquer rule

8 Binary Search Algorithm 1.Initialize Low to the index of the first element. 2.Initialize High to the index of the last element. 3.Repeat through step 6 while Low <= High 4.Middle=(Low+High)/2 5.If search element is equal to middle element return Index of middle element 6. If search element is less than middle element High=Middle-1 else Low=Middle+1 7.return -1 (indicates element not found)

9 Sort Concept Sorting is the process of arranging data items in a particular order (Ascending or descending). Sorting is a feature in which the elements of a list are put in certain order There are different Types of search- Few of them are –Selection sort –Bubble sort

10 Selection Sort In Selection sort it selects the smallest unsorted item remaining in the list, and then swapping it with the item in the next position to be filled. It Beginning with the first element in the array, a search is performed to locate the element which has the smallest key. When the element is found it is interchanged with the first element. This process is continued for all the elements in the array. It is mainly used with small list of elements

11 Selection Sort Algorithm 1. Repeat thru steps 5 a total of n-1 times. 2. Record the number portion of the vector already used. 3. Repeat step 4 for the elements in the unsorted portion of the vector. 4. Record location of smallest elements in unsorted vector 5. Exchange first element in unsorted vector with smallest element.

12 Example - Selection sort Original Array Pass Number

13 Bubble Sort In bubble sort instead of finding the smallest element and then exchanging, it compares and exchanges with the adjacent element. It starts by comparing the first item to the second, the second to the third and so on until it finds one item out of order. It then swaps the two items and starts over. Is used for Sequencing small list.

14 Bubble Sort Algorithm 1. Repeat through step 4 a total of n-1 times. 2. Repeat step 3 for elements in unsorted portion of the vector. 3. If the current element in the vector > next element in the vector then exchange the elements. 4. If no exchanges were made then return else reduce the size of the unsorted vector by one.

15 Example - Bubble sort Original Array Pass Number

16 Space Complexity –Indicates how much extra memory the algorithm needs Time Complexity –Indicates how fast the algorithm runs Analysis of algorithms

17 Key Points Searching is Locating a particular data item in a list of data elements is known as searching. Few of the Searching types are Linear search & Binary search Sorting is the process of arranging data items in a particular order (Ascending or descending). Few of the types of Sorting Techniques are Selection sort & Bubble sort Analysis of algorithms

18 Questions and Comments