Algorithms:
An algorithm means a way of solving a specific problem. It takes a set of inputs and produces the desired output. Using this algorithm we can identify the complexity of the program.
Sample algorithm:
Printing integer array in reverse order.
//input
int array[] = {10,20,5,8,15,25,30,28};
for(int i=array.length-1;i>=0;i--) {
System.out.print(array[i]+" ");
}
//output
28 30 25 15 8 5 20 10
In programming different types of algorithms are available for sorting and searching elements in the collection framework. Collection means it may be an array, list, set, map, etc.
Searching algorithm:
This algorithm is used to search an element from a collection of elements. The collection may be an array, list, map, or set. If you are using this algorithm then you can find an element t effectively. This algorithm will have properties called time complexity and space complexity. These properties are will be used for which algorithm will be effective in search elements.
Sample algorithm for search:
int inputValue = 5;
int array[] = {10,20,5,8,15,25,30,28};
for(int i=0;i
if(array[i] == inputValue) {
System.out.println("Given value: "+ inputValue + " is avaliable in "+ i +" index.");
break;
}
}
//output is
Given value: 5 is avaliable in 2 index.
Here input value is 5 this we need to search in that array whether that value is available in that array or not. So for that, we need to compare that input value with each element of the array.
Types of searching algorithms are follows
1) Linear Search
2) Binary Search
3) Interpolation Search
Linear Search:
This algorithm is used to search elements linearly or sequentially. This means that we need to search elements one by one. It sequentially checks each element of the array until the targeted element is reached.
Example:
int inputValue = 5;
int array[] = {10,20,5,8,15,25,30,28};
for(int i=0;i
if(array[i] == inputValue) {
System.out.println("Given value: "+ inputValue + " is avaliable in "+ i +" index.");
break;
}
}
//output is
Given value: 5 is avaliable in 2 index.
The time complexity of this algorithm is...