Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Collection Insertion, sorting and searching in array

Related Tutorials


 
 

Share on Google+Share on Google+

Insertion, sorting and searching in array

Advertisement
In this section we will learn how to perform searching and sorting in array.

Insertion, sorting and searching in array

     

In this section we will learn how to perform searching and sorting in array. In the java code given below we have declare an array of integer type and then sort by using java.util.Arrays.Sort() method, also we defined two methods to show the element of the array and insert new element in array and return sorted array.

Through method java.util.Arrays.binarySearch() we will search for the item in specified array.

ArraySortSearch.java

import java.util.Arrays;
public class ArraySortSearch {
    public static void main(String args[]) throws Exception {
        //declare, initialize and then print array of integer type.
        int array[] = {1, 2, 3, -8, 9, -2, 6, -7, -5, -3, -71, 56, 81, -36};
        show("Elements in array", array);
        // sort then print the specified arrray in ascending order.
        Arrays.sort(array);
        show("\nArray elements after sorting", array);

        // Search for element in specified array
        int index = Arrays.binarySearch(array, -71);
        System.out.println("\nIndex of integer -71 : " + index);
        // when element not present in array.
        index = Arrays.binarySearch(array, -52);
        System.out.println("\nElement not present in array." + index);
        // Insert one more element at the end of array
        array = insertElement(array, 1);
        show("Elements in integer array", array);
    }
    static void show(String msg, int arr[]) {
        System.out.println(msg + " : ");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + ", ");
        }
    }
    static int[] insertElement(int original[], int element) {
        int length = original.length;
        int destination[] = new int[length + 1];
        System.arraycopy(original, 0, destination, 0, length);
        destination[length] = element;
        Arrays.sort(destination);
        return destination;
    }
}


Output :

Elements in array : 
1, 2, 3, -8, 9, -2, 6, -7, -5, -3, -71, 56, 81, -36, 
Array elements after sorting : 
-71, -36, -8, -7, -5, -3, -2, 1, 2, 3, 6, 9, 56, 81, 
Index of integer -71 : 0
Element not present in array.-2
Elements in integer array : 
-71, -36, -8, -7, -5, -3, -2, 1, 1, 2, 3, 6, 9, 56, 81, 

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 1, 2008

Related Tutorials

Discuss: Insertion, sorting and searching in array   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
Ian Kerby R. Acuņa
February 12, 2012
advance programming

aw..lisod
CH FAZAL UL HAQ BAJWA
October 22, 2012
C++ PROGRAMMING & DATA STRUCTURE

I want to make programmer. Programming is very interested for us. Specially. C++ , DATA STRUCTURE, DBMS, HTML, & JAVA
DMCA.com