Search and Sort Algorithm
Search and Sort Algorithm
1.
public class LinearSearchExample1{
public static int linearSearch(int[] arr, int key,int key1){
for(int i=0;i<arr.length;i++){
if(arr[i] == key||arr[i]==key1){
return i;
}
}
return -1;
}
public static void main(String a[]){
int[] a1= {10,20,30,50,70,90};
int key = 20;
int key1=30;
System.out.println(key+" is found at index: "+linearSearch(a1, key,key1));
}
}
2. import java.util.Scanner;
class LinearSearchExample2
{
public static void main(String args[])
{
int c, n, search, array[];
for (c = 0; c < n; c++) //to iterate over the array and search for the key
element
{
if (array[c] == search) /* Searching element is present */
{
System.out.println(search + " is present at location " + (c) + ".");
break;
}
}
if (c == n) /* Element to search isn't present */
System.out.println(search + " isn't present in array.");
}
}
n[0] n[1] n[2] n[3] n[4] n[5] ... n[16] n[17] n[18] n[19]
45 65 77 71 90 67 ... 82 19 31 52
Sample Output: 71, 67, 19, 31*/
import java.util.*;
System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
System.out.println("Prime Numbers:");
for (int i = 0; i < arr.length; i++) {
if (c == 2)
System.out.print(arr[i] + ", ");
}
}
}
4.
// Java program to find minimum (or maximum)
// element in an array.
//import java.io.*;
class ArrayMinMax {
5.
/*Write a program in Java to store 20 numbers (even and odd numbers) in a
Single Dimensional Array (SDA). Calculate and display the sum of all even
numbers and all odd numbers separately.*/
import java.util.Scanner;
public class SDAOddEvenSum
{
public static void main(String args[]) {
System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
15 21 -32 -41 54 61 71 -19 -44 52
Sample Output: -32, -41, -19, -44, 15, 21, 54, 61, 71, 52*/
import java.util.Scanner;
• Step VI In case the element not found then repeat the process from Step III unless First
is greater than the last
• Finally the process will be completed resulting in “Search is unsuccessfull”
• Repeatedly select the smallest key in the remaining
unsorted array and bring it to its right position
• In an unsorted array the first smallest will be
brought to first position second smallest to
second position third smallest to third position and so on
Compare minimum with the third element. Again, if the third element
is smaller, then assign minimum to the third element otherwise do
nothing. The process goes on until the last element.
• Compare two adjoining values and exchange them if they are not in
proper order
• This process is repeated for every pair of adjoining elements in the array
• In every pass, the heaviest element settles as its appropriate position in
the bottom
• Bubble Sort is the simplest sorting algorithm that works by repeatedly
swapping the adjacent elements if they are in wrong order.
• Example:
• First Pass:
• ( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two
elements, and swaps since 5 > 1.
• ( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
• ( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
• ( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in
order (8 > 5), algorithm does not swap them.
• Second Pass:
• ( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
• ( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
• ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
• ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
• Now, the array is already sorted, but our algorithm does not know if it is
completed. The algorithm needs one whole pass without any swap to
know it is sorted.
• Third Pass:
• ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
• ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
• ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
// Java program for implementation of Bubble Sort
//int arr[] = {64, 34, 25, 12, 22, 11, 90};
class BubbleSort
{
void bubbleSort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)//Pass
for (int j = 0; j < n-i-1; j++)// Iteration
if (arr[j] > arr[j+1])
{
// swap arr[j+1] and arr[j]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
/* Prints the array */
void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}