1 Array / Array Operations. 2 Single variables Read the rainfall for the 12 month in a year: Read n 1 Read n 2 … Read n 12 n1n1 n2n2 n 12.

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

Chapter 9: Searching, Sorting, and Algorithm Analysis
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
System Programming in C Lecture 4. Lecture Summary Operations with Arrays: –Searching the array –Sorting the array.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
1 Sorting. 2 Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort a list,
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A10 – Elementary Algorithms (Revision)
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that.
Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures.
Searching Arrays Linear search Binary search small arrays
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.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
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.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Data Structures Using C++1 Search Algorithms Sequential Search (Linear Search) Binary Search.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Lecture 12. Searching Algorithms and its analysis 1.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
Chapter 2 Array Data Structure Winter Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages.
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.
Chapter Searching and Sorting Arrays 8. Introduction to Search Algorithms 8.1.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
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.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
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 & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
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.
1 Parallel Sorting Algorithm. 2 Bitonic Sequence A bitonic sequence is defined as a list with no more than one LOCAL MAXIMUM and no more than one LOCAL.
CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value.
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.
Elementary Sorting 30 January Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j List[j])
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
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 Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
Searching and Sorting Arrays
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Lecture No.45 Data Structures Dr. Sohail Aslam.
Section 2.6: Searching and Sorting
Section 10.3b Quick Sort.
Advanced Sorting Methods: Shellsort
Chapter 9 One-Dimensional Arrays
Searching and Sorting Arrays
Searching and Sorting Arrays
Module 8 – Searching & Sorting Algorithms
Module 8 – Searching & Sorting Algorithms
Module 8 – Searching & Sorting Algorithms
Applications of Arrays
Presentation transcript:

1 Array / Array Operations

2 Single variables Read the rainfall for the 12 month in a year: Read n 1 Read n 2 … Read n 12 n1n1 n2n2 n 12

3 Array Read the rainfall for the 12 month in a year: FOR month := 1 TO 12 read rainfall[month] ENDFOR rainfall rainfall[2] FOR month := january TO december read rainfall[month] ENDFOR

4 Attribute Functions Array / Struct / Class ArrayStructClass

5 class Rainfall for the 12 month of a year: class Rainfall { private: data[12] public: Rainfall(…) setRainfall(…) getRainfall(month) sum( ) average( ) getMax( ) getMin( ) … }

6 Search Methods - Linear search -Stepwise search -Binary search

7 Linear search - Strategy No need for sorted array. Search from the beginning to the end of the array until the searched record is found or we find out the the searched record does not exist. Searched record

8 Linear search Algorithm 1 LinSearch (array,max,sid) /*Linear search for a given record in an array, */ /*returning the position of the searched record. */ /*If no record found, then returning 0.*/ /*array:The array to search*/ /*max:Maximum number of elements in the array*/ /*sid:The value of the searched record*/ nr:=0 n :=1 WHILE (nr = 0) AND (n<=max) DO IF sid = array[n]THEN nr:=n ELSE n:=n + 1 ENDIF ENDWHILE Return nr

9 Linear search Algorithm 2 LinSearch(arary,max,sid) /*Linear search for a given record in an array*/ /*returning the position of the searched record.*/ /*If the record does not exist, then returning 0.*/ /*array:The array to search*/ /*max:The maximum number of elements in the arary*/ /*sid:The value of the searched record*/ n :=1 WHILE (n array[n]) DO n:=n + 1 ENDWHILE IF n <= max THEN nr := n ELSE nr := 0 Return nr

10 Linear search Access number Maximum access number: Average access number:

11 Stepwise search - Strategy The array must be sortered. Searching in step from the beginning until we have come sufficiently far. Searching in the last step. Searched record

12 Stepwise search Access number 1 Maximum access number:

13 Stepwise search Access number 2 Average access number: Optimal steplength: Average access number by optimal steplength:

14 Stepwise search Algorithm StepSeach(array,max,sid) /*Stepwise search for given record in an array*/ /*returning the postition of the searched record.*/ /*If the record does not exist, then returning 0.*/ /*arary:The array to search*/ /*max:Maksimum number of elements in the array*/ /*sid:The value of the searched record*/ nr:=0 x:=Sqrt(max) first:=1 last:=max previous:=first WHILE (first < last) AND (array[first] < sid) DO// step previous:= first first:= min(first+x,last) ENDWHILE i := first IF sid <> array[i] THEN i := previous WHILE (i < first) AND (array[i] < sid) DO// linear i := i + 1 ENDWHILE ENDIF IF sid = array[i] THEN nr := i ENDIF Return nr

15 Binary search - Strategy The array must be sorted. Divide into two equal parts og search further in actual half. Search until searched record is found or we have only one record left.

16 Binary search Access number Number of records in the array:N After halving 1 time :N/2 =N/2 1 records left After halving2 times :N/4 =N/2 2 records left After halving3 times :N/8 =N/2 3 records left After halvingA times :N/2 A records left Maximum acccess number: Average access number:

17 Binary search Algorithm 1 BinSearch (array,max,sid) /*Binary search for given record in an array*/ /*returning the position of searched record.*/ /*If the record does not exist, then returning 0.*/ /*array:The array to search*/ /*max:The maximum number of elements in the array*/ /*sid:The value of the searched record*/ nr:=0 first:=1 last:=max mid:=(first+last) DIV 2 WHILE (sid <> array[mid]) AND (first < last) DO IF sid < array[mid] THEN last := mid - 1 ELSE first := mid + 1 ENDIF mid := (first + last) DIV 2 ENDWHILE IF sid = array[mid] THEN nr := mid ENDIF Return nr

18 Binary search Algorithm 2 BinSearch (tab,forst,sist,sid) /*Binary search for a given record in an array*/ /*returning the position of the searched record.*/ /*If the record does not exist, then returning 0.*/ /*Recursive search.*/ /*array:The array to search*/ /*first:Index of the first element*/ /*last:Index of the last element*/ /*sid:The value of the searched record*/ mid:=(first+last) DIV 2 IF first > last THEN Return 0 ELSEIF sid = array[mid] THEN Return mid ELSEIF sid < array[mid] THEN Return BinSearch(array,first,mid-1,sid) ELSE Return BinSearch(array,mid+1,last,sid) ENDIF

19 Search methods [1/2] Linear search Stepwise search Binary search

20 Search methods [2/2] Linear search Stepwise search Binary search N = 2 millions

21 Sorting Often there is a need to have the data elements in a given arrangement or structure both because of quicker processing methods and for future applications. One kind of arrangement is sorting.

22 Sorting methods - Bubblesort - Bucketsort - Mergesort -Shellsort - Quicksort -...

23 Bubblesort - Example [1] * * * *

* Bubblesort - Example [2]

* Bubblesort - Example [3]

* Bubblesort - Example [4]

27 Bubblesort - Algorithm BubbleSort (array,n) /*Bubblesort of an array*/ /*array:The array to be sorted*/ /*n:Maximum number of elements in the array*/ exchange:= true j:= 1 WHILE exchage DO exchange := false FOR i:=1 TO n-j DO IF array[i] > array[i+1] THEN exchange:=true x:=array[i] array[i]:=array[i+1] array[i+1]:=x ENDIF ENDFOR j := j + 1 ENDWHILE

28 Bubblesort - Algorithm Nilsen Olsen Hansen Knutsen Persen arrayIdarrayDt BublleSort (arrayId,arrayDt,n) /*Bubblesort of an array*/ /*arrayId:Array containing the sorting key*/ /*arrayDt:The array(s) containing the rest of data*/ /*n:Maximum number of elements in the array*/ exchange:= true j:= 1 WHILE exchange DO exchange := false FOR i:=1 TO n-j DO IF arrayId[i] > arrayId[i+1] THEN exchange := true x:=arrayId[i] arrayId[i]:=arrayId[i+1] arrayId[i+1]:= x y:=arrayDt[i] arrayDt[i]:=arrayDt[i+1] arrayDt[i+1]:= y ENDIF ENDFOR j := j + 1 ENDWHILE

29 Bubblesort - Algorithm Nilsen Olsen Hansen Knutsen Persen iddt BubbleSort (array,n) /*Bubblesort of an array*/ /*array:The array to be sorted*/ /*n:Maximum number of elements in the array*/ exchange:= true j:= 1 WHILE exchange DO exchange := false FOR i:=1 TO n-j DO IF array[i].id > array[i+1].id THEN exchange := true x:=array[i] array[i]:=array[i+1] array[i+1]:= x ENDIF ENDFOR j := j + 1 ENDWHILE

30 Order Array By sorting of big arrays (or many arryas), our bubblesort can imply moving of big datasets. We can make an improvement by means of an order array. The order array we read sequentially from the beginning and indicate in which order the array(s) should be accessed. Before sortingAfter sorting

31 Bubblesort - Order Array BubbleSort (array,order,n) /*Bubblesort of an aray with order array*/ /*array:The array to be sorted*/ /*order:Order array */ /*n:Maximum number of elements in the array*/ exchange:= true i:= 1 WHILE exchange DO exchange := false FOR i:=1 TO n-j DO IF array[order[i]] > array[order[i+1]] THEN x:=order[i] order[i]:=order[i+1] order[i+1]:=x ENDIF ENDFOR j := j + 1 ENDWHILE

32 Insert / Delete in an array Insert Delete

33 Insert into an array - Algorithms Insert (array,max,n,newRecord,pos,flag) /*Insert a new record in an array*/ /*array:The array*/ /*max:Maximum number of elements in the array*/ /*n:The first free position in the array*/ /*newRecord:New record to insert into the array*/ /*pos:Position to insert into the array*/ /*flag:Return true if insert okay*/ IF n > max THEN flag := false ELSE flag:= true i:= n - 1 WHILE i >= pos DO tab[i+1]:= tab[i] i:= i - 1 ENDWHILE tab[pos]:= newRecord n := n + 1 ENDIF

34 Delete in an array - Algorithm Delete (array,n,pos) /*Delete a record in an array*/ /*array:The array*/ /*n:The first free position in the array*/ /*pos:The delete position in the array*/ i := pos WHILE i < n-1 DO array[i]:= array[i+1] i:= i + 1 ENDWHILE n := n - 1

35 Fetch in an array - Algoritme Fetch (array,n,pos,record) /*Fetch and then delete a record from an array*/ /*array:The array*/ /*n:The first free position in the array*/ /*pos:The delete position in the array*/ /*record:Return the fetched record*/ record :=array[pos] Delete(array,n,pos)

36 ENDEND