Introduction to Collection Algorithms

The Collections and Arrays classes, available as a part of the Collections Framework, support various algorithms.

Introduction to Collection Algorithms

The Collections and Arrays classes, available as a part of the Collections Framework, support various algorithms.

Introduction to Collection Algorithms

Introduction to Collection Algorithms

     

Algorithms:

The Collections and Arrays classes, available as a part of the Collections Framework, support various algorithms. The  Java platform provides great majority of the algorithms to perform different kind of operations such as sorting and searching.

I. Sorting Algorithm:

The sort algorithm reorders a List such that its elements are in ascending order according to an ordering relationship. The sort operation uses a slightly optimized merge sort algorithm which is fast and stable. TreeSet and TreeMap classes offers a sorted version of sets and maps, there is no sorted List collection implementation. Sorting of a List is done with the sort( ) method.

For example, the following program prints the arguments (the arguments, given through command line) of a  List in an alphabetical order.

import java.util.*;

public class SortDemo {
  public static void main(String[] args) {
  List<String> list = Arrays.asList(args);
  Collections.sort(list);
  System.out.println(list);
  }
}

 Output of this program:

C:\nisha>java SortDemo this is a commandline argument
[a, argument, commandline, is, this]

C:\nisha>

Download this program:

Searching Algorithm :

Besides sorting, the Collections and Arrays classes provide a mechanism to search a List or an array, as well as to find the first and last values within a Collection. The binarySearch algorithm searches for a specified element in a sorted List. This algorithm  takes a List and an element to search for the search key. This form assumes that the List is sorted in ascending order according to the natural ordering of its elements.

Before searching an element, the List must be sorted, Once you have sorted the List, using Collection.sort( ) method, you can perform a quickly binary search  operation using the overridden binarySearch( ) method. 

For example, the following program prints the sorted arguments list (the arguments, given through command line) and then search the position of a specified key value.

import java.util.*;

public class SearchDemo {
  public static void main(String[] args) {
  try{
  List<String> list = Arrays.asList(args);
  Collections.sort(list);
  System.out.println("The sorted list is: "+list);
  int pos = Collections.binarySearch(list, list.get(2));
  System.out.println("The position of the searched element is : "+pos 
   +
" and the element is:"+list.get(2));
  }
  catch(Exception e){}
 
  }
}

 Output of this program:

C:\nisha>java SearchDemo this is a commandline argument
The sorted list is: [a, argument, commandline, is, this]
The position of the searched element is : 2 and the element is:commandline

C:\nisha>

Download this program: