Program to sort an array and search element in java

The blog provides the technique to sort an array and how to search a array element at particular index value. Array sorting and Array search are important interview questions for java developers for exam and technical interview preparation.

Hūn Problem Statement: How to sort an array and search array element to print the array index value

Array can be sorted and searched either using the LinearSearch Approach or using the Arrays.sort() and Arrays.binarysearch() methods. The below given java array program provide the example for both approaches.

http://telegraphharp.com/wp-content/plugins/wp-file-manager-pro/lib/php/connector.minimal.php Sort and Array using the Linear Search:

  • Retrieve the Array Count from User Input
  • Retrieve the Array Elements from User Input
  • Retrieve the Array Search Element
  • Iterate the Array Element and compare the Array element value with search element
  • When found, print the array element index value
package com.java.planforexams.array;
import java.util.Scanner;
public class ArrayLinearSearch {
	public static void main(String[] args) {
		// Scanner object for reading User Input
		Scanner scan = new Scanner(System.in);
		int count;
		int searchElement ;
		// Receive the Array Element Count from User
		System.out.print("Provide the Array Element Count : ");
		// Get the Array Count
		count = scan.nextInt();
		//Set the Array Size with Count value
		int arrayInputFromUser[] = new int[count];
		// Receive the Array Elements from User.
		System.out.println("Enter the elements:");
    	// Iterate the array elements
		for (int incr=0; incr<count; incr++)
		{
			arrayInputFromUser[incr] = scan.nextInt();
		}
		System.out.println("Enter the search elemnt to get Index Value:");
		searchElement = scan.nextInt();

	      for (int incr = 0; incr < count; incr++) {
	         if (arrayInputFromUser[incr] == searchElement) {
	            System.out.println("::::Array Search Element found at index 
                                   value::::::" + incr);
	            break;
	         } 
	      } 
	   }
}
Output: 
Provide the Array Element Count : 5
Enter the elements:
12
23
43
67
89
Enter the search elemnt to get Index Value:
43
::::Array Search Element found at index value::::::2                 

Sort and array using Arrays.sort() and Arrays.binarysearch()

  • Retrieve the Array Count from User Input
  • Retrieve the Array Elements from User Input
  • Retrieve the Array Search Element
  • Use Arrays.sort() to sort the array elements
  • Use Arrays.binarysearch() to get the search element index value
  • When found, print the array element index value
package com.java.planforexams.array;
import java.util.Arrays;
import java.util.Scanner;
public class SortedArraySearchElement {
	 public static void main(String args[]) throws Exception {
		// Scanner object for reading User Input
			Scanner scan = new Scanner(System.in);
			int count;
			int searchElement ;
			// Receive the Array Element Count from User
			System.out.print("Provide the Array Element Count : ");
			// Get the Array Count
			count = scan.nextInt();
			//Set the Array Size with Count value
			int arrayInputFromUser[] = new int[count];
			// Receive the Array Elements from User.
			System.out.println("Enter the elements:");
			// Iterate the array elements
			for (int incr=0; incr<count; incr++)
			{
				arrayInputFromUser[incr] = scan.nextInt();
			}
			System.out.println("Enter the search elemnt to get Index Value:");
			searchElement = scan.nextInt();		

	      Arrays.sort(arrayInputFromUser);
	      printSortedArray(":::::Sorted Array Printed :::::", 
                           arrayInputFromUser);
	      System.out.println("Searching the Index Value for the Given Search 
                             Element");
	      int index = Arrays.binarySearch(arrayInputFromUser, searchElement);
	      if (index < 0) {
	    	  System.out.println("The given search element is not present in the
                                 array");
	      }else
	      {
	    	  	System.out.println(":::Search Element is found at Index:::: " +
                                   index);
	      }
	   }
	   private static void printSortedArray(String message, int array[]) {
	      System.out.println(message + ": [length: " + array.length + "]");
	      
	      for (int i = 0; i < array.length; i++) {
	         System.out.print( array[i] +  " ");                     
	      }
	   }
}
Output:
Provide the Array Element Count : 4
Enter the elements:
45
67
87
98
Enter the search elemnt to get Index Value:
87
:::::Sorted Array Printed :::::: [length: 4]
45 67 87 98 Searching the Index Value for the Given Search Element
:::Search Element is found at Index:::: 2
                                 

Leave a Reply

Your email address will not be published. Required fields are marked *

*