Principles of Computing – UFCFA3-30-1

Slides:



Advertisements
Similar presentations
CSE Lecture 3 – Algorithms I
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Chapter 9: Searching, Sorting, and Algorithm Analysis
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
HST 952 Computing for Biomedical Scientists Lecture 9.
Searching and Sorting SLA Computer Science 4/16/08 Allison Mishkin.
CHAPTER 11 Sorting.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
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.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
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.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
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.
CSCI 51 Introduction to Programming March 12, 2009.
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.
3 – SIMPLE SORTING ALGORITHMS
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.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
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.
Searching Topics Sequential Search Binary Search.
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.
Chapter 16: Searching, Sorting, and the vector Type.
Searching and Sorting Copyright Prentice Hall (with additions / modifications by Evan Korth)
Copyright Prentice Hall Modified by Sana odeh, NYU
Searching and Sorting Searching algorithms with simple arrays
Searching and Sorting Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
Searching and Sorting Algorithms
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
3.3 Fundamentals of data representation
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Algorithms Chapter 3 With Question/Answer Animations
Searching and Sorting Topics Sequential Search on an Unordered File
Searching and Sorting Topics Sequential Search on an Unordered File
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
Standard Version of Starting Out with C++, 4th Edition
Searching and Sorting 1-D Arrays
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
Search,Sort,Recursion.
24 Searching and Sorting.
Topic 24 sorting and searching arrays
Searching and Sorting Arrays
Search,Sort,Recursion.
Searching and Sorting Arrays
Chapter 19 Searching, Sorting and Big O
Principles of Computing – UFCFA3-30-1
CHAPTER 9 SORTING & SEARCHING.
Algorithm Efficiency and Sorting
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Presentation transcript:

Principles of Computing – UFCFA3-30-1 Searching and Sorting Instructor : Mazhar H Malik Email : mazhar@gcet.edu.om Global College of Engineering and Technology

Previous Lecture Algorithm Characteristics of Algorithm Algorithm Notations, Operators and Functions Assignment Operator Arithmetical Operators Relational Operators Logical Operators Input/output Notations Mathematical Functions LO-3

Today’s Lecture Sorting Searching Linear Search Binary Search Selection Sort Insertion Sort Bubble Sort Searching & Sorting

Learning Objective Explain algorithmic behaviour of programs in appropriate formal terms. LO[3] Component B

Linear Array A linear array is the list of finite number of data elements (i.e. same type of data). Data Store in the array identify by the index numbers.

Searching A search algorithm is a method of locating a specific item of information in a larger collection of data. This section discusses two algorithms for searching the contents of an array. Linear Search Binary Search

The Linear Search This is a very simple algorithm. It uses a loop to sequentially step through an array, starting with the first element. It compares each element with the value being searched for and stops when that value is found or the end of the array is reached.

The Linear Search-Algorithm Initialize the elements of the array Input “X” number to search while i<length of array if(array[index] == X): print("%d found at %dth position"%(X,i)) break i=i+1

Efficiency of the Linear Search The advantage is its simplicity. It is easy to understand Easy to implement Does not require the array to be in order The disadvantage is its inefficiency If there are 20,000 items in the array and what you are looking for is in the 19,999th element, you need to search through the entire list.

Binary Search The binary search is much more efficient than the linear search. It requires the list to be in order. The algorithm starts searching with the middle element. If the item is less than the middle element, it starts over searching the first half of the list. If the item is greater than the middle element, the search starts over starting with the middle element in the second half of the list and it continues halving the list until the item is found.

1 11 2 22 3 33 4 44 5 55 6 66 7 77 8 88 9 99 Item := 33 Mid Beg := 1 End : = 9 Mid := Int (( Beg + End ) / 2)

1 11 2 22 3 33 4 44 5 55 6 66 7 77 8 88 9 99 Mid Item := 33 Beg := 1 End : = 4 Mid := Int (( Beg + End ) / 2)

1 11 2 22 3 33 4 44 5 55 6 66 7 77 8 88 9 99 Item := 33 Mid Beg := 3 End : = 4 Mid := Int (( Beg + End ) / 2)

1 11 2 22 3 33 4 44 5 55 6 66 7 77 8 88 9 99 Item := 33 Mid Mid := 3

Efficiency of the Binary Search Much more efficient than the linear search. Array should be in sorting condition before applying binary search

How Fast is a Binary Search? Worst case: 11 items in the list took 4 tries How about the worst case for a list with 32 items ? 1st try - list has 16 items 2nd try - list has 8 items 3rd try - list has 4 items 4th try - list has 2 items 5th try - list has 1 item

How Fast is a Binary Search? (con’t) List has 512 items 1st try - 256 items 2nd try - 128 items 3rd try - 64 items 4th try - 32 items 5th try - 16 items 6th try - 8 items 7th try - 4 items 8th try - 2 items 9th try - 1 item List has 250 items 1st try - 125 items 2nd try - 63 items 3rd try - 32 items 4th try - 16 items 5th try - 8 items 6th try - 4 items 7th try - 2 items 8th try - 1 item

What’s the Pattern? List of 11 took 4 tries List of 32 took 5 tries

Since binary algorithm is very efficient (e. g Since binary algorithm is very efficient (e.g. it requires only about 20 comparisons with an initial list of 1,000,000 elements) but observe that algorithm requires two conditions The list must be sorted One must have direct access to middle element in sub list

Sorting Sorting — arranging items in order — is the most fundamental task in computation. Sorting enables efficient searching algorithms such as binary search. Selection Sort Insertion Sort Bubble Sort

Sorting Selection sort: repeatedly pick the smallest element to append to the result. Insertion sort: repeatedly add new element to the sorted result. Bubble sort: repeatedly compare neighbor pairs and swap if necessary.

Selection Sort Selection sort is to repetitively pick up the smallest element and put it into the right position: Find the smallest element, and put it to the first position. Find the next smallest element, and put it to the second position. Repeat until all elements are in the right positions.

Selection Sort

Insertion Sort Insertion sort maintains a sorted sub-array, and repetitively inserts new elements into it. The process is as following: Take the first element as a sorted sub-array. Insert the second element into the sorted sub-array (shift elements if needed). Insert the third element into the sorted sub-array. Repeat until all elements are inserted.

Insertion Sort

Bubble Sort Bubble sort repetitively compares adjacent pairs of elements and swaps if necessary. : Scan the array, swapping adjacent pair of elements if they are not in relative order. This bubbles up the largest element to the end. Scan the array again, bubbling up the second largest element. Repeat until all elements are in order.

Bubble Sort

Bubble Sort Apply Bubble sort on the following array {38, 27, 43,9, 82, 10} 

Home Work {38, 27, 43, 3, 9, 82, 10} Using above dataset, perform {38, 27, 43, 3, 9, 82, 10}  Using above dataset, perform Searching Linear Search Binary Search Sorting Insertion Sort Selection Sort Bubble Sort Due Date: 17.12.2017

Lab Work: Linear Search Python Implementation Write a program that will input a number from user and will check whether number exist in the array or not using Linear Search Algorithm. After execution display suitable message (i.e Input number not exist or Input number is at index[n] of array.

Summary In This lecture we discussed about the array searching and Sorting techniques. Searching Techniques? Sorting Techniques?

Reference Chapter 6, Page 281-303 .Hein J H (2010). Discrete Structures, Logic, and Computability, 4th ed. Jones and Bartlett

Questions?