Question

You are asked to write an efficient Pseudocode to search a given key within a given...

You are asked to write an efficient Pseudocode to search a given key within a given 2D array, where every row and column is sorted in an ascending order. For example consider a 2D array. Every row and column is sorted. You are searching the 2D array for key 36.  

3 7 11 23 45

5 9 13 25 50

7 14 15 30 55

10 18 22 34 62

16 24 29 38 88

Write and execute a java implementation of pseudocode your using the Codio platform. Test the correctness of your program using the test data given.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Pseudocode

  • Set a 2D array, Array
  • Set a key to search, key
  • Set rowlength=length(Array)
  • set columnlength=length(Array[0])
  • For i=1 To columnlength

             if Array[rowlength][i]==key

              Display found message

                   Exit from program

             ElseIF Array[rowlength][i]>key

                   For j=1 To rowlength

                       If Array[j][i-1]==key

                           Display found message

                           Exit from program

                      End If

                   End

             End If

          End

  • Display Not found message

Screenshot

public class TwoDSortedArraySearch 36 not Found in the array public static void main (String [] args) //Test array int testArProgram

public class TwoDSortedArraySearch {

   public static void main(String[] args) {
       //Test array
       int testArray[][]=new int[][] {{3,7,11,23,45},{5,9,13,25,50},{7,14,15,30,55},{10,18,22,34,62},{16,24,29,38,88}};
       //Test key
       int key=36;
       //Loop through last row
       for(int i=0;i<testArray[0].length;i++) {
           //Any colum match the key
           if(testArray[testArray.length-1][i]==key) {
               System.out.println(key+" Found in the row "+(testArray.length)+" column "+(i+1)+" in the array");
               System.exit(0);
           }
           //Find which column greater than key
           else if(testArray[0][i]>key) {
               //Loop through all rows 1 less than i column
               //If found match display
               for(int j=0;j<testArray.length;j++) {
                   if(testArray[j][i-1]==key) {
                       System.out.println(key+" Found in the row "+(j+1)+" column "+(i)+" in the array");
                       System.exit(0);
                   }
               }
           }
       }
       //Not found case
       System.out.println(key+" not Found in the array");
   }
}

-------------------------

Output

36 not Found in the array

Add a comment
Know the answer?
Add Answer to:
You are asked to write an efficient Pseudocode to search a given key within a given...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

  • Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D...

    Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D array of integers which is 10x10. 2. Prints out the contents of the 2D array after assigning the data to make sure correct data was assigned. 3. Figures out and prints out the square root of the sum of ALL the elements in the 2D array. 4. Figures out and prints out the average of ALL THE ELEMENTS in the 2D array. 5. Figures...

  • Java question Given an array of integer numbers, write a linear running time complexity program in...

    Java question Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A itf ATO] + A[1] + +A[iI-1] Ali+1]+ Ali+2] +... + A[n-1]; where 0 <i< n-1 Similarly, 0 is an stability index if (A[1] A[2]A[n-1]) 0 and n-1 is an stability index if (A[0] A[1]+... A[n-21) 0 Example:...

  • Using Java, write a program that creates a class to represent what you spend your time...

    Using Java, write a program that creates a class to represent what you spend your time on every week. Inside of that class, you will need a 2D array that represents each day of the week and how much time you spend on any given activity (the size should be 7 by whatever the number of activities you perform in a week). You may enter the values however you like, but you will need to display the array so that...

  • Please solve only if you know how to do it. Write the code using C++ (not...

    Please solve only if you know how to do it. Write the code using C++ (not Python or Java). Show and explain everything neatly. COMMENTS (7.5% of programming assignment grade): Your program should have at least ten (10) different detailed comments explaining the different parts of your program. Each individual comment should be, at a minimum, a sentence explaining a particular part of your code. You should make each comment as detailed as necessary to fully explain your code. You...

  • Given an array of integer numbers, write a linear running time complexity program in Java to...

    Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A if A[0] + A[1] + ... + A[i-1] = A[i+1] + A[i+2] + ... + A[n-1]; where 0 < i < n-1. Similarly, 0 is an stability index if (A[1] + A[2] + ... + A[n-1]) = 0 and...

  • IN PYTHON the original problem was Design (pseudocode) and implement (source code) a program (name it...

    IN PYTHON the original problem was Design (pseudocode) and implement (source code) a program (name it WeeklyHours) to compute the total weekly hours for 3 employees. The program main method defines a two-dimensional array of size 3x7 to store employers’ daily hours for the week. Each row represents one employee and each column represent one day of the week such that column 0 designates Monday, column 1 designates Tuesday, etc. The program main method populates the array with random numbers...

  • Need help with a few questions! Using the JAVA language please. 1) exercises - branching Write...

    Need help with a few questions! Using the JAVA language please. 1) exercises - branching Write a method whatToWear(int temp) that takes a temperature and then outputs a string for what to wear in different weather conditions. There must be at least 3 categories. For example, whatToWear(90) might return “shorts” and whatToWear(30) might return “down coat” 2) Enum exercise • Define an enum Seasons, and rewrite the whatToWear method to use enums and switch statement 8) 2D array exercise 1....

  • Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’,...

    Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9. I have posted 3 input files: sudoku1.txt, sudoku2.txt and sudoku3.txt Problem Analysis & Design - Think about how you will need to solve this problem. You should do an...

  • 8.18 Ch 8, Part 3: Tabular Output Write this program using Eclipse. Comment and style the...

    8.18 Ch 8, Part 3: Tabular Output Write this program using Eclipse. Comment and style the code according to CS 200 Style Guide. Submit the source code files (.java) below. Make sure your source files are encoded in UTF-8. Some strange compiler errors are due to the text encoding not being correct. The program you are going to write will produce a String containing a tabular representation of a 2D String array. For the 2D arrays, assume that the first...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT