Data Searching and Sorting algorithms

Slides:



Advertisements
Similar presentations
HST 952 Computing for Biomedical Scientists Lecture 9.
Advertisements

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.
CHAPTER 11 Sorting.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
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]
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Chapter 19: Searching and Sorting Algorithms
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
Data Structure Introduction.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
ALGORITHMS.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski.
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.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Copyright Prentice Hall Modified by Sana odeh, NYU
Searching and Sorting Searching algorithms with simple arrays
Searching and Sorting Algorithms
Data Structures I (CPCS-204)
Searching Given a collection and an element (key) to find… Output
Lecture 14 Searching and Sorting Richard Gesick.
IGCSE 6 Cambridge Effectiveness of algorithms Computer Science
Computer Science 101 A Survey of Computer Science
Chapter 8 Arrays Objectives
Chapter 7 Arrays.
Teach A level Computing: Algorithms and Data Structures
Searching.
Sorting Data are arranged according to their values.
Adapted from slides by Marty Stepp and Stuart Reges
Linear and Binary Search
Algorithm design and Analysis
Searching.
CSc 110, Spring 2017 Lecture 39: searching.
Data Structures and Algorithms
Chapter 8 Search and Sort
Binary Search and Intro to Sorting
كلية المجتمع الخرج البرمجة - المستوى الثاني
Chapter 8 Arrays Objectives
Sorting Data are arranged according to their values.
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
IT 4043 Data Structures and Algorithms
Searching: linear & binary
Search,Sort,Recursion.
Searching CLRS, Sections 9.1 – 9.3.
24 Searching and Sorting.
Data Structures (CS212D) Week # 2: Arrays.
Sub-Quadratic Sorting Algorithms
ITEC 2620M Introduction to Data Structures
Principles of Computing – UFCFA3-30-1
Arrays Week 2.
Introduction to Data Structures
Search,Sort,Recursion.
Chapter 8 Arrays Objectives
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Principles of Computing – UFCFA3-30-1
Presentation transcript:

Data Searching and Sorting algorithms Ahti Lohk Teaching Assistant in Department of Software Science PhD (in Computer Science)

Agenda This presentation answers to the following questions: Why to use searching algorithms? What is the idea of the Linear search? What is the idea of Binary search? What are efficiency indicators of a program? Why to use sorting algorithms? What is the idea of Bubble sort? What is the idea of Shell sort?

Why to use searching algorithms? To check if an item is in a set (included different type of sequences: string, array, collection, file, etc). F.e., to check if the user has permission for certain actions. To get additional information about the searchable. Of course, the result (and its representation) depends on from where do we search – www.google.com, yippy.com, pipl.com or from specific table

Background of search algorithms There are lot of search algorithms in use. Different data structures (array, linked list, search trees, hash tables) use different algorithms. In this lecture we focus on two search algorithms that are used on sequences (an array, sequence of data in Excel worksheet column or row): Linear search (also known as sequential search) Binary search

Searching algorithms

How the linear search works? The idea is to go through all items in the sequence and compare each of them with a searchable/lookup value. If the searchable value is equal to an item value in the sequence the result will be the sequence number of found item. If the searchable value is not in the sequence the result will be -1.

Linear search VBA code and usage (the lookup value (Siim) is in the sequence (names))

Linear search VBA code and usage (the lookup value (Toivo) is not in the sequance (names))

How the binary search works? The binary search assumes that the target sequence is sorted. This method reduces the search area by half (bisection) repeatedly and try to find target value. More precisely, this method follows the following steps: Repeat until the search area size is 0: Calculate the middle point of the current search area If the target is at the middle, stop. Otherwise, if the target is less that what is at the middle, repeat, changing the end point to be just to the left (if sequence is like a row) of the middle. Otherwise, if the target is greater than what is at the middle , repeat, changing the start point to be just to the right (if sequence is like a row) of the middle. https://www.youtube.com/watch?v=5xlIPT1FRcA

Binary search VBA code and usage (the lookup value (Julia) is in the sequance (names))

Binary search VBA code and usage (the lookup value (Sander) is not in the sequance (names))

The efficiency indicators of a program There are three well known efficiency indicators: 1. Number of comparisons – how many comparisons the algorithm does. 2. Number of actions – how many sentences the program has to execute to get the result. 3. Memory capacity – how much memory is used during the work of the program.

Comparing linear and binary search The efficiency of the both methods depends on where the lookup value in the sequence is located. It is typical to evaluate the algorithm according to its worst case. In the case of the linear search the worst is when the lookup value is not in the sequence or it is in the last position. In the case of the binary search the worst case is when the lookup value is not in the sequence.

Comparing linear and binary search 2 Comparing based on the worst case of the number of comparisons. If the sequence length is 100 items then in the case of linear search the number of comparison is also 100. But in the case of binary search this number is perceptively 7.

Why sorting? Sorting is the easiest way to arrange data. The idea of all given sorting algorithms is as follows: Compare the items of a sequence with each other and (Inter)change their positions if it is needed.

Some well-known sorting algorithms Simple sorts Efficient Sorts

A question to Barack Obama What is the efficient way to sort a million 32-bit integers? See the answer in the following YouTube video: http://www.youtube.com/watch?v=k4RRi_ntQc8&feature=related

Comparisons (in seconds) Nr of items Bubble Insertion Shell Shell Mod Quick 10 000 4 2 0,055 0,031 0,000 50 000 96 46 0,52 0,14 0,070 100 000 0,22 0,040 1 000 000 3,14 0,38 10 000 000 4,20

Sequential sort Sub SequentialSort(V As Range, nrOfValues&) Dim i&, j&, tmp& For i = 1 To nrOfValues - 1 For j = i + 1 To nrOfValues If V(i) > V(j) Then tmp = V(i): V(i) = V(j): V(j) = tmp Next j Next i End Sub

Bubble sort Sub Bubble_Sort(V As Range, nrOfValues&) Dim i&, k&, tmp&, sorted As Boolean k = 0 Do sorted = True k = k + 1 For i = 1 To nrOfValues - k If V(i) > V(i + 1) Then tmp = V(i): V(i) = V(i + 1): V(i + 1) = tmp sorted = False End If DoEvents Next i Loop Until sorted = True End Sub

Shell Sort Sub ShellSort(V as range, nrOfValues&) End Sub Dim gap&, i&, k&, tmp& Dim As Boolean gap = nrOfValues Do While gap > 1 gap = gap \ 2 Do noexchange = True For i = 1 To nrOfValues - gap k = i + gap If V(k) < V(i) Then tmp = V(k): V(k) = V(i): V(i) = tmp: noexchange = False Next i Loop Until noexchange = True Loop End Sub