SEARCHING IN ARRAYS
Linear Search in Java
Linear search is used to search a key element from multiple elements, stored in Array.
Algorithm:
o Step 1: Traverse the array
o Step 2: Match the key element with array element
o Step 3: If key element is found, return the index position of the array element
o Step 4: If key element is not found, return -1
Let's see an example of linear search in java where we are going to search an element
sequentially from an array.
1. import java.util.Scanner;
2.
3. class LinearSearchExample
4. {
5. public void main()
6. {
7. int c, n, search;
8.
9. Scanner in = new Scanner(System.in);
10. System.out.println("Enter number of elements");
11. n = in.nextInt();
12. int array[ ] = new int[n];
13.
14. System.out.println("Enter those " + n + " elements");
15.
16. for (c = 0; c < n; c++)
17. array[c] = in.nextInt();
18.
19. System.out.println("Enter value to find");
20. search = in.nextInt();
21.
22. for (c = 0; c < n; c++)
23. {
24. if (array[c] == search) /* Searching element is present */
25. {
26. System.out.println(search + " is present at location " + (c + 1) + ".");
27. break;
28. }
29. }
30. if (c == n) /* Element to search isn't present */
31. System.out.println(search + " isn't present in array.");
32. }
33. }
Output:
Enter number of elements
6
Enter those 6 elements
12
6
78
9
45
08
Enter value to find
45
45 is present at location 5.
SORTING IN ARRAYS
Sorting refers to arranging all the elements of an Array either in Ascending or
Descending order.
We can create a java program to sort array elements using bubble sort. Bubble sort
algorithm is known as the simplest sorting algorithm.
In bubble sort algorithm, array is traversed from first element to last element. Here,
current element is compared with the next element. If current element is greater than
the next element, it is swapped.
Bubble sort program for sorting in Ascending Order:
import java.util.Scanner;
class BubbleSortExample
{
public void main()
{
int num, i, j, temp;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of integers to sort:");
num = input.nextInt();
int array[ ] = new int[num];
System.out.println("Enter " + num + " integers: ");
for (i = 0; i < num; i++)
array[i] = input.nextInt();
for (i = 0; i < ( num - 1 ); i++) {
for (j = 0; j < num - i - 1; j++) {
if (array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
System.out.println("Sorted list of integers:");
for (i = 0; i < num; i++)
System.out.println(array[i]);
}
}
Output:
Enter the number of integers to sort:
6
Enter 6 integers:
12
6
78
9
45
08
Sorted list of integers:
6
8
9
12
45
78
Bubble sort program for sorting in Descending Order:
In order to sort in descending order we just need to change the logic array[j] >
array[j+1] to array[j] < array[j+1] in the above program. Complete code as follows:
import java.util.Scanner;
class BubbleSortExample
{
public void main()
{
int num, i, j, temp;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of integers to sort:");
num = input.nextInt();
int array[ ] = new int[num];
System.out.println("Enter " + num + " integers: ");
for (i = 0; i < num; i++)
array[i] = input.nextInt();
for (i = 0; i < ( num - 1 ); i++) {
for (j = 0; j < num - i - 1; j++) {
if (array[j] < array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
System.out.println("Sorted list of integers:");
for (i = 0; i < num; i++)
System.out.println(array[i]);
}
}
Output:
Enter the number of integers to sort:
6
Enter 6 integers:
89
12
45
9
56
102
Sorted list of integers:
102
89
56
45
12
9