Sort and Search an Element in Java

In Java sorting and searching an element in an array is very easy. Unlike C, where we have to make all the functions to work, Java has inbuilt functions to do the same work. To sort an array there is a sort function and to search an element in a sorted array there is a binarySearch() function. To learn more about these functions please follow the articles mentioned below:

Sorting: Arrays.sort() in Java with Examples

Time Complexity: O (n log n)

Syntax for Sorting:

Arrays.sort(array_name);

Searching: Arrays.binarySearch() in Java with Examples

For using Binary search array must be sorted previously



Important Points:

  • If the input array is not sorted, the results are undefined and negative.
  • If there are duplicates, there is no guarantee which one will be found.

Time Complexity: O (log n)

Syntax for Binary Searching:

Arrays.binarySearch(array_name, key);

We could always create user-defined functions but the inbuilt functions in Java are already using the least complexity algorithms so there is no need to make such functions.

Example 1: Finding an element in a sorted array.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find an 
// element in an sorted array
  
// Importing util files
import java.util.*;
  
public class Gfg {
  
    // Main function
    public static void main(String args[]) throws Exception
    {
  
        // Sorted Array
        int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  
        // Using binarySearch to search for desired element
        int index = Arrays.binarySearch(array, 2);
  
        // Printing result
        if (index >= 0)
            System.out.println("Element 2 found at index: "
                               + index);
        else
            System.out.println("Element not found");
    }
}

chevron_right


Output:

Element 2 found at index: 1

Example 2: Sorting an array and searching for an element in the sorted array.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for Sorting an array and
// searching for an element in the 
// sorted array
  
// Importing util files
import java.util.*;
  
public class Gfg {
  
    // Main function
    public static void main(String args[]) throws Exception
    {
  
        // Unsorted Array
        int array[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
  
        // Sorting the array
        Arrays.sort(array);
  
        // Printing sorted array
        System.out.println("Sorted Array:");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
  
        // Using binarySearch to search for desired element
        int index = Arrays.binarySearch(array, 0);
  
        // Printing result
        if (index >= 0)
            System.out.println("Element 2 found at index: "
                               + index);
        else
            System.out.println("Element not found");
    }
}

chevron_right


Output:

Sorted Array:
1 2 3 4 5 6 7 8 9 10 
Element 2 found at index: 1

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.