Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Open Search2D class and implement efficient, non-recursive search method that searches for a desired item in a rectangular two-dimensional array of integers where elements are

Open Search2D class and implement efficient, non-recursive search method that searches for a desired item in a rectangular two-dimensional array of integers where elements are sorted within each row and within each column (see the following two-dimensional array).

int matrix[][] = { {10, 20, 21, 40}, {15, 25, 26, 45}, {27, 29, 30, 48}, {32, 33, 34, 50}};

If the element is found, the method prints its position, otherwise it prints "not found".

The algorithm must take into consideration the nature of the sorted data.

public class Search2D { /**  * Searches for the desiredItem in a rectangular matrix[][] where  * elements are sorted within each row and within each column  * If the element is found, prints its position,  * otherwise prints "not found"  *  * @author Efrain Hernandez  * @version 10/30/2018  *  */   private void search(int[][] matrix, int desiredItem) { // TODO Project 5  System.out.println("Searching for " + desiredItem); } // driver to test search method public static void main(String[] args) { int matrix[][] = { {10, 20, 21, 40}, {15, 25, 26, 45}, {27, 29, 30, 48}, {32, 33, 34, 50}}; Search2D search2D = new Search2D(); System.out.println("*** These should be successful searches ***"); for (int r = 0; r < matrix.length; r++) { for (int c = 0; c < matrix[r].length; c++) { search2D.search(matrix, matrix[r][c]); } } System.out.println(" *** These should be successful searches ***"); search2D.search(matrix,28); search2D.search(matrix,5); search2D.search(matrix,100); } } 

See sample run :

*** These should be successful searches ***

Searching for 10

checking 40

checking 21

checking 20

checking 10

10 found at [0, 0]

Searching for 20

checking 40

checking 21

checking 20

20 found at [0, 1]

Searching for 21

checking 40

checking 21

21 found at [0, 2]

Searching for 40

checking 40

40 found at [0, 3]

Searching for 15

checking 40

checking 21

checking 20

checking 10

checking 15

15 found at [1, 0]

Searching for 25

checking 40

checking 21

checking 26

checking 25

25 found at [1, 1]

Searching for 26

checking 40

checking 21

checking 26

26 found at [1, 2]

Searching for 45

checking 40

checking 45

45 found at [1, 3]

Searching for 27

checking 40

checking 21

checking 26

checking 30

checking 29

checking 27

27 found at [2, 0]

Searching for 29

checking 40

checking 21

checking 26

checking 30

checking 29

29 found at [2, 1]

Searching for 30

checking 40

checking 21

checking 26

checking 30

30 found at [2, 2]

Searching for 48

checking 40

checking 45

checking 48

48 found at [2, 3]

Searching for 32

checking 40

checking 21

checking 26

checking 30

checking 34

checking 33

checking 32

32 found at [3, 0]

Searching for 33

checking 40

checking 21

checking 26

checking 30

checking 34

checking 33

33 found at [3, 1]

Searching for 34

checking 40

checking 21

checking 26

checking 30

checking 34

34 found at [3, 2]

Searching for 50

checking 40

checking 45

checking 48

checking 50

50 found at [3, 3]

*** These should be unsuccessful searches ***

Searching for 28

checking 40

checking 21

checking 26

checking 30

checking 29

checking 27

checking 32

28 not found

Searching for 5

checking 40

checking 21

checking 20

checking 10

5 not found

Searching for 100

checking 40

checking 45

checking 48

checking 50

100 not found found

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions

Question

Have roles been defined and assigned?

Answered: 1 week ago

Question

Types of cultural maps ?

Answered: 1 week ago

Question

Discuss the various types of leasing.

Answered: 1 week ago

Question

Define the term "Leasing"

Answered: 1 week ago

Question

What do you mean by Dividend ?

Answered: 1 week ago