Upcoming SlideShare
×

# Ds 4

425 views
383 views

Published on

Published in: Technology, Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
425
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Ds 4

1. 1. Introduction to Searching techniques andMatricesObjectivesIn this lesson, you will learn to: State the algorithm for linear search Perform operations on matrices State the applications of matrices Implement Recursion Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 1 of 32
2. 2. Introduction to Searching techniques andMatricesSearching Searching is an operation that returns either: The location of a given item of information A message that the given information is not found Searching algorithm depends on the type of data structure used to store data For example, there can be a data structure with: Faster search operation (sorted array) Faster modification operation (linked list) Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 2 of 32
3. 3. Introduction to Searching techniques andMatricesSearching (Contd..) There is a trade-off in these data structures Sorted array insertion and deletion is time consuming Linked list traversal is time-consuming The different searching techniques are: Linear search Binary search Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 3 of 32
4. 4. Introduction to Searching techniques andMatricesLinear Search Is the simplest search technique In this technique begin at one end and scan until: The item is found The end of the list is reached Benefits: Can be used on any data structure No restrictions on the way data is arranged Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 4 of 32
5. 5. Introduction to Searching techniques andMatricesLinear Search (Contd..) Drawbacks: Performance is dependant on the length of the array Average number of comparisons 1/2(n+1) Even if required data is not present the complete array is searched Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 5 of 32
6. 6. Introduction to Searching techniques andMatricesBinary Search Binary search technique is faster than linear search technique After every search the number of items to be searched is reduced by half In this search technique: Start by comparing the middle element Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 6 of 32
7. 7. Introduction to Searching techniques andMatricesBinary Search (Contd..) Then the next quarter and so on until The required item is found No more data is left for searching Benefits: Faster search After each step number of items to be searched reduces by half More efficient for searching large volume of data Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 7 of 32
8. 8. Introduction to Searching techniques andMatricesBinary Search (Contd..) Average number of comparisons for 1 million items is only 20 Drawbacks: Data has to be sorted Cannot be used on all data structures Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 8 of 32
9. 9. Introduction to Searching techniques andMatricesProblem Statement 4.D.1 Write a program to accept 20 numbers and store it in an array. Now accept a number and search it in an array, make sure that, before searching a number you perform bubble sort it in. Note: Implement using arrays Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 9 of 32
10. 10. Introduction to Searching techniques andMatricesJust A Minute In a sorted array insertion and deletion are less time consuming than searching.(True/False) In a binary tree searching is more time consuming than linked list ( True/False) Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 10 of 32
11. 11. Introduction to Searching techniques andMatricesMatricesA rectangular arrangement of rows and columns is amatrix COLUMNS A B C D E F G H ROWS E F G H I J K L I J K L Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 11 of 32
12. 12. Introduction to Searching techniques andMatricesMatrices (Contd..) The size of the matrix is called the dimensions of the matrix The sum of rows and columns of a matrix identify the size Each number in a matrix is called an element Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 12 of 32
13. 13. Introduction to Searching techniques andMatricesMatrices (Contd..) A matrix with the same number of rows and columns is called a square matrix A B C E F E F G I J I J K Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 13 of 32
14. 14. Introduction to Searching techniques andMatricesMatrices (Contd..) A X D X Z D Z E The above figure shows another type of matrix that is, a sparse matrix A matrix with lot of blank elements is called a sparse matrix Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 14 of 32
15. 15. Introduction to Searching techniques andMatricesOperations on Matrices The basic operations on a matrix can be listed as: Create Store Delete Retrieve Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 15 of 32
16. 16. Introduction to Searching techniques andMatricesMulti-dimensional Matrix 4 Pages 2 Rows 3 Columns The syntax for declaring a three-dimensional matrix is same as that of two-dimensional array Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 16 of 32
17. 17. Introduction to Searching techniques andMatricesMulti-dimensional Matrix (Contd..) Example: int Newone[2][3][4]; The array Newone[2][3][4] contains 2*3*4=24 elements The subscripts of the array are called: row column page Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 17 of 32
18. 18. Introduction to Searching techniques andMatricesMulti-dimensional Matrix (Contd..) In this array, we can store 8 different set of details. Sum of row and page gives the number of different sets of information that can be stored Example: 4*2 = 8 for the array Newone Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 18 of 32
19. 19. Introduction to Searching techniques andMatricesApplications Matrices can be applied in: Finding the shortest route Map coloring Search engines for the WEB when combined with queues and hash tables Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 19 of 32
20. 20. Introduction to Searching techniques andMatricesProblem Statement 4.D.2Write a program to store the details of a student( Student name, Registration number, and the Subjectmarks) in a three-dimensional array. Also, give an optionto the user to query and print the details for a specificstudent requested by the user by entering theregistration number of the student. Accept the studentdata from the user? Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 20 of 32
21. 21. Introduction to Searching techniques andMatricesRecursion Whenever a function invokes itself, it is called recursion. Every time a recursive function calls itself, it must call itself with different values. A recursive procedure or function must satisfy the following two conditions: There must be certain values called base values for which the function will not call back itself. Each time the function refers to itself, the arguments passed must be closer to its base value. It is useful in writing clear, short, and simple programs Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 21 of 32
22. 22. Introduction to Searching techniques andMatricesPractice Statement 4.P.1 Identify the erroneous step(s) in the following Table A.2 Step 1 0!=1 Step 2 1! =1*1=1 Step 3 2!=2*1=2 Step 4 3! =3x2 =6 Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 22 of 32
23. 23. Introduction to Searching techniques andMatricesJust a Minute… The factorial() function will repeatedly invoke itself until the parameter passed to it is _____. When the above function is invoked with a parameter of 5, the factorial() function is invoked ____ times in all. State whether True or False. With large values (say,100) of the parameter passed to the factorial() function, there is a danger of the program crashing. Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 23 of 32
24. 24. Introduction to Searching techniques andMatricesSummaryIn this lesson, you learned that: Information retrieval is one of the most important applications of computers Searching is an operation, which: Returns the location in memory of some given item of information Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 24 of 32
25. 25. Introduction to Searching techniques andMatricesSummary (Contd..) The search algorithm depends mainly on the type of data structure we use to store the data in memory In a sorted array the search time can be greatly reduced by using a search technique called binary search In sorted array, there is an overhead-involved that is, insertion and deletion of data is a time-consuming process Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 25 of 32
26. 26. Introduction to Searching techniques andMatricesSummary (Contd..) In a linked List: Search time will be high Only sequential search is possible Insertion and deletion will be fast Binary Tree combines the advantage of sorted array and a linked list Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 26 of 32
27. 27. Introduction to Searching techniques andMatricesSummary (Contd..) Linear search is also referred to as sequential search is the simplest search technique In Linear Search, to search an item begin at one end of the list and scan down the list until: The desired item is found The end of the list is reached Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 27 of 32
28. 28. Introduction to Searching techniques andMatricesSummary (Contd..) The average number of comparison for a linear search can be worked out by applying the formula 1/2(n+1) Linear search technique has some drawbacks: Even if there is no match for the item in the list, the complete list is searched The search time is directly proportional to the length of the list Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 28 of 32
29. 29. Introduction to Searching techniques andMatricesSummary (Contd..) Binary search is faster than linear search technique In Binary Search, to search an item begin at the middle of the list and scan one half of the list , then the quarter and so on until: the desired item is found or there is no more data to search In a binary search, after every comparison, the data to be searched reduces by half. Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 29 of 32
30. 30. Introduction to Searching techniques andMatricesSummary (Contd..) The drawbacks of binary search are: The list has to be sorted There should be a method to access the middle element of the list A rectangular arrangement of rows and columns is called a matrix A matrix with the same number of rows and columns is called a square matrix A matrix with a lot of zero or unused elements is generally called a sparse matrix Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 30 of 32
31. 31. Introduction to Searching techniques andMatricesSummary (Contd..)The basic operations on a matrix can be listed as: Create Store Delete RetrieveThe subscripts of a three-dimensional array are called: Row Column Page Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 31 of 32
32. 32. Introduction to Searching techniques andMatricesSummary (Contd..) Matrices can be applied for: Finding a shortest route Map coloring Recursion can be defined as defining something in terms of itself. A function is recursive if it makes a call to itself, i.e. invokes itself. Advantage of recursion is that it is useful in writing clear, short, and simple programs. Introduction to Searching techniques and Matrices/Lesson ©NIIT 4/Slide 32 of 32