- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort and Search an Element in Java
Sorting and searching are the basic operations we can perform on arrays. Sorting means rearranging the elements of a given list or array in ascending or descending order whereas searching means finding an element or its index in a list.
Although various algorithms are available to perform these operations, in this article, we will use a few of them to sort and search an element in java. One by one we will look at them.
Approach 1: Using inbuilt methods of Arrays
In this section, we will discuss the following methods that helps in sorting and searching of elements in an array.
sort() − It is a static method of Arrays class that sorts array passed as an argument in ascending order.
Syntax
Arrays.sort(nameOfarray);
binarySearch() − It is also a static method of Arrays class. It accepts two arguments, first is the array whose element needs to be searched and second is the element we need to find in that array.
It returns the index number of the element passed as argument.
Syntax
Arrays.binarySearch(nameOfarray, element);
Example
import java.util.*; public class Srch { public static void main(String args[]) { int araylist[] = {9, 3, 56, 0, -2, -6, 2, 1, 80}; System.out.print("The given unsorted list: "); // for each loop that prints the original array for (int print : araylist) { System.out.print(print + " "); } Arrays.sort(araylist); // method to sort given array System.out.println(); System.out.print("The newly sorted list: "); // for each loop that prints the newly sorted array for (int print : araylist) { System.out.print(print + " "); } System.out.println(); // method to search given element int position = Arrays.binarySearch(araylist, 1); if(position > -1) { System.out.print("Element is available at index: " + position); } else { System.out.print("Element is not available"); } } }
Output
The given unsorted list: 9 3 56 0 -2 -6 2 1 80 The newly sorted list: -6 -2 0 1 2 3 9 56 80 Element is available at index: 3
Approach 2: Using our custom logic
Sorting using Bubble sort
Algorithm
Step 1 − First, declare and initialize an array named ‘araylist’ and an integer variable named ‘temp’ to store shifted elements temporarily.
Step 2 − Take two for loops to compare ith postion element to ith + 1 element. Inside the 2nd for loop create an if block to check which element is greater and then we perform shifting operation to rearrange those elements in ascending order.
Step 3 − Now using for each loop we will print the sorted array.
Example
public class Bubble { public static void main(String[] args) { int araylist[] = {9, 3, 56, 0, 2, 1, 80}; int temp = 0; System.out.print("The given unsorted list: "); for (int print : araylist) { System.out.print(print + " "); } for (int i = 0; i < araylist.length; i++) { for (int j = i+1; j < araylist.length; j++) { if(araylist[i] > araylist[j]) { temp = araylist[i]; araylist[i] = araylist[j]; araylist[j] = temp; } } } System.out.println(); System.out.print("The newly sorted list: "); for (int print : araylist) { System.out.print(print + " "); } } }
Output
The given unsorted list: 9 3 56 0 2 1 80 The newly sorted list: 0 1 2 3 9 56 80
Searching using Linear Search
Algorithm
Step 1 − First, declare and initialize an array named ‘araylist’ and an integer variable named ‘searchElem’ that we are going to search in the array. We need two more integer variables ‘isFound’ and ‘locate’.
Step 2 − Now, create a for loop that will run till the length of array. In this loop, take an if block that checks whether the ‘searchElem’ is present in the array or not. If it is available store its index in variable ‘locate’ and increment the variable ‘isFound’ to 1.
Step 3 − Moving ahead, we create an if else block to check whether the variable ‘isFound’ is incremented to 1 or not. If it is equal to 1 it means element is found and we will return the index. If it is not then statement in the else block will get executed.
Example
public class Linear { public static void main(String[] args) { int araylist[] = {9, 3, 56, 0, 2, 1, 80}; int searchElem = 0; int isFound = 0; int locate = 0; for(int i = 0; i < araylist.length; i++) { if(searchElem == araylist[i]) { isFound = 1; locate = i; } } if(isFound == 1) { System.out.print("Element is available at index: " + locate); } else { System.out.print("Element is not available"); } } }
Output
Element is available at index: 3
Conclusion
In this article, we have discussed how we can sort array elements and perform a search operation to find a particular element of that array. We can use an inbuilt method named ‘sort()’ or any algorithm of sorting and searching.