> 7. Linear Search in Java (for integers)
Arrays in Java: (Part 6)
One Dimensional Array :
7. Linear Search in Java (for integers)
8. Linear Search in Java (for Strings)
9. Print the strings that ends with a vowel from an array of strings
- Example: (Linear Search in Java -> for Integers)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Linear Search in Java -> for integers import java.util.Scanner; class test { public static void main(String ar[]) { Scanner scan = new Scanner(System.in); System.out.println("Enter the size of array:"); int size = scan.nextInt(); int array[] = new int[size]; for(int i=0;i<array.length;i++) { System.out.println("Enter "+i+" element"); array[i] = scan.nextInt(); } System.out.println("Enter the key to search"); int key = scan.nextInt(); int x=0; // variable to check if the key is not found. for(int i=0;i<array.length;i++) { if(array[i]==key) { x=0; System.out.println("Key found at: "+i); break; } else { x=1; } } if(x==1) { System.out.println("Key NOT Found!"); } } } /* OUTPUT: Enter the size of array: 5 Enter 0 element 10 Enter 1 element 20 Enter 2 element 30 Enter 3 element 4050 Enter 4 element 50 Enter the key to search 30 Key found at: 2 */ - Example: Linear Search in Java (for Strings)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Linear Search in Java -> for integers import java.util.Scanner; class test { public static void main(String ar[]) { Scanner scan = new Scanner(System.in); System.out.println("Enter the size of array:"); int size = scan.nextInt(); scan.nextLine(); //clear the buffer after integer input. String array[] = new String[size]; for(int i=0;i<array.length;i++) { System.out.println("Enter "+i+" element"); array[i] = scan.nextLine(); } System.out.println("Enter the key to search"); String key = scan.nextLine(); int x=0; for(int i=0;i<array.length;i++) { if(array[i].equals(key)) //equals() method of String class is used to compare and check equality of two strings. { x=0; System.out.println("Key found at: "+i); break; } else { x=1; } } if(x==1) { System.out.println("Key NOT Found!"); } } } /* OUTPUT: Enter the size of array: 5 Enter 0 element banana Enter 1 element mango Enter 2 element pear Enter 3 element kiwi Enter 4 element apple Enter the key to search pear Key found at: 2 */ - Example: (Linear Search - find all the possible locations of the key entered by user)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Linear Search in Java -> for integers import java.util.Scanner; class test { public static void main(String ar[]) { Scanner scan = new Scanner(System.in); System.out.println("Enter the size of array:"); int size = scan.nextInt(); scan.nextLine(); //clear the buffer after integer input. String array[] = new String[size]; for(int i=0;i<array.length;i++) { System.out.println("Enter "+i+" element"); array[i] = scan.nextLine(); } System.out.println("Enter the key to search"); String key = scan.nextLine(); int x=0; for(int i=0;i<array.length;i++) { if(array[i].equals(key)) //equals() method of String class is used to compare and check equality of two strings. { System.out.println("Key found at: "+i); } else { x++; } } if(x==array.length) // if the x has incremented array.length times, the key is not present in the array. { System.out.println("Key NOT Found!"); } } } /* OUTPUT: Enter the size of array: 3 Enter 0 element this Enter 1 element this Enter 2 element is Enter the key to search this Key found at: 0 Key found at: 1 */
Comments
Post a Comment