Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Insertion, sorting and searching in array 
 

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

                         

» View all related tutorials
Related Tags: java c string ide array io word vi new key this id element elements for example keyword with to ini

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.