Searching in Arrays And Strings -2
So now you have understood the first method (Binary search) to search for any element in an array or string. Now here comes the second method to implement this search.
{ binary search : https://codingatess.blogspot.com/2023/07/searching-in-arrays-strings-1.html }
LINEAR SEARCH
A searching algorithm that sequentially examines each element in a list or array until the target element is found or the entire list is traversed.
TIME COMPLEXITY
The time complexity of linear search is O(n), where "n" is the number of elements in the list or array being searched.
In the worst-case scenario, linear search may need to check all "n" elements starting from index 0 to n-1 before finding the target or concluding that the target is not present.
CODE
import java.util.Arrays;
public class linsrchcodingates{
static void printarr(int[] arr){
for(int i=0;i<arr.length;i++){
int j = arr[i];
System.out.println(j);
}
}
static void srcheven(int[] arr) {
if (arr.length == 0) {
System.out.println("the array is empty");
}
for(int i=0;i<arr.length;i++){
int IndexElement = arr[i];
if(IndexElement%2==0){
System.out.println(IndexElement);
}
}
}
static int srch2d(int[][] arr, int target){
if(arr.length==0){
return -1;
}
for(int i=0; i<arr.length;i++){
for(int j=0; j<arr[i].length;j++){
int IndexEle = arr[i][j];
if(IndexEle == target){
return 1;
}
}
}
return -1;
}
static void min(int[] arr){
int min = arr[0];
for(int i=0;i<arr.length;i++){
int IndexEl = arr[i];
if(IndexEl<min){
min = IndexEl;
}
}
System.out.println("the minimum number in the array is "+min);
}
static int srchInRnge(int[] arr, int index1, int index2, int target){
if(arr.length==0){
return -1;
}
for(int x = index1; x<=index2; x++){
int IndexElem = arr[x];
if(IndexElem==target){
return 1;
}
}
return -1;
}
static int srchInArr(int[] arr, int i){
if(arr.length == 0){
return -1;
}
for(int p=0; p<arr.length; p++){
int element = arr[p];
if(element==i){
System.out.println(" ");
System.out.println("the element is present in the array at index " +p);
return p;
}
}
return -1;
}
static String srchInStr(String str, char target){
if(str.length()==0){
return "string is empty";
}
for(int x=0; x<str.length(); x++){
char c = str.charAt(x);
if(c==target){
System.out.println(" ");
return "yes it is present";
}
}
return "no, its not present";
}
public static void main(String[] args){
int[] arr1= {-33,-56,-79,3,-1};
int[][] arr2 = {{1,2},{3,4},{5,6}};
System.out.print(srchInArr(arr1,33)); //output : -1 i.e. element not present
System.out.print(srchInArr(arr1,-33)); //output : the element is present in the array at index 0
String a = "Aryan";
System.out.println(srchInStr(a,'r')); //output : yes it is present
System.out.println(srchInRnge(arr1,0,4,3));
min(arr1); //output : -79
System.out.println(srch2d(arr2,22)); //output : -1
System.out.println(srch2d(arr2,2)); //output : 1
srcheven(arr1); //output : nothing because there's no even number in arr1
printarr(arr1);
}
}
CODE EXPLANATION
1. `printarr`: This method takes an integer array `arr` as input and prints each element of the array on a new line.
2. `srcheven`: This method takes an integer array `arr` as input and prints all the even numbers present in the array.
3. `srch2d`: This method performs a linear search for an integer `target` in a 2D integer array `arr`. It returns 1 if the target is found and -1 if not found.
4. `min`: This method takes an integer array `arr` as input and finds the minimum number in the array. It then prints the result.
5. `srchInRnge`: This method searches for an integer `target` in the subarray of `arr`, specified by `index1` and `index2`. It returns 1 if the target is found and -1 if not found.
6. `srchInArr`: This method searches for an integer `i` in the integer array `arr`. If the element is found, it prints a message indicating the index of the element and returns the index. If the element is not found, it returns -1.
7. `srchInStr`: This method searches for a character `target` in the given string `str`. If the character is found, it returns a message indicating its presence. If not found, it returns a message indicating its absence.
8. In the `main` method, the code demonstrates the usage of the methods:
- It creates an integer array `arr1` and a 2D integer array `arr2`.
- It calls `srchInArr` to search for the integer 33 in `arr1`, which should return -1 since 33 is not present in the array.
- It calls `srchInArr` to search for the integer -33 in `arr1`, which should return 0 since -33 is present at index 0.
- It calls `srchInStr` to search for the character 'r' in the string "Aryan", which should return "yes, it is present" since 'r' is present in the string.
- It calls `srchInRnge` to search for the integer 3 in `arr1` from index 0 to 4, which should return 1 since 3 is present in the specified range.
- It calls `min` to find the minimum number in `arr1`, which should print "-79" as the minimum number in the array.
- It calls `srch2d` to search for the integer 22 in `arr2`, which should return -1 since 22 is not present in the 2D array.
- It calls `srch2d` to search for the integer 2 in `arr2`, which should return 1 since 2 is present in the 2D array.
- It calls `srcheven` to find and print all even numbers in `arr1`, but as there are no even numbers, it won't print any number.
although both search algorithms have their own advantages and disadvantages, most nerds find linear search a better way to search elements over binary search.
HAPPY CODING :)
Post a Comment