Showing posts with label Arrays. Show all posts
Showing posts with label Arrays. Show all posts

Friday, June 17, 2016

Using Comparator to Sort an Array example in java

Using Comparator to Sort an Array

It offers an interface , called Comparator that is useful to impose a total ordering on a collection of elements.It can be used to sort the elements of an array into ascending order or descending order.

interface Comparator<T>,where T is generic type

Program Sample:

/**
 * 
 */
package com.collectionpack;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @author Abhinaw.Tripathi
 *
 */
class Ascending implements Comparator<Integer>
{

@Override
public int compare(Integer o1, Integer o2)
{

return o1.compareTo(o2);
}

}

class Desending implements Comparator<Integer>
{

@Override
public int compare(Integer o1, Integer o2)
{
return o2.compareTo(o1);
}

}

public class ComparatorApp
{
public static void main(String[] args) throws NumberFormatException, IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many elements? ");
int size=Integer.parseInt(br.readLine());

Integer arr[] =new Integer[size];

for(int i=0;i<size;i++)
{
System.out.println("Eneter int: ");
arr[i]=Integer.parseInt(br.readLine());
}

Arrays.sort(arr, new Ascending());
System.out.println("Sorted in asending order");
display(arr);

Arrays.sort(arr,new Desending());
System.out.println("sorted in desending order");
display(arr);
}

private static void display(Integer[] arr)
{
for(Integer i:arr)
System.out.println(i + "\t");

}


}

Output:
How many elements? 
5
Eneter int: 
26
Eneter int: 
89
Eneter int: 
69
Eneter int: 
78
Eneter int: 
456
Sorted in asending order
26
69
78
89
456
sorted in desending order
456
89
78
69
26



Array Class example java

Array Class

Arrays class provides methods to perform certain operations on any one dimensional array.All the methods of the array class are static ,So they can be called in the form of Arrays.methodname() .

Arrays Class Methods

  • static void sort(array)
  • static void sort(array,int start,int end)
  • static int binarySearch(array,element)
  • static boolean equals(array1,array2)
  • static array copyOf(source-array,int n)
  • static void fill(array,value)
Program:


/**
 * 
 */
package com.collectionpack;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

/**
 * @author Abhinaw.Tripathi
 *
 */
public class ArraysDemo 
{

public static void main(String[] args) throws NumberFormatException, IOException
    {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int arr[]=new int[5];
for(int i=0;i<5;i++)
{
System.out.println("Enter an Integer: ");
arr[i]=Integer.parseInt(br.readLine());
}
System.out.println("Contents of the Array");
display(arr);
Arrays.sort(arr);
System.out.println("The sorted array: ");
display(arr);
System.out.println("Which element to search?");
int element=Integer.parseInt(br.readLine());
int index=Arrays.binarySearch(arr, element);
if(index < 0)
System.out.println("Elemnt not found!!!");
else
System.out.println("Element is found at location: " +(index+1));
}

private static void display(int[] arr)
{
for(int i:arr)
System.out.println(i);
}

}

Output:

Enter an Integer: 
2
Enter an Integer: 
1
Enter an Integer: 
3
Enter an Integer: 
45
Enter an Integer: 
52
Contents of the Array
2
1
3
45
52
The sorted array: 
1
2
3
45
52
Which element to search?
3
Element is found at location: 3



Tuesday, June 7, 2016

Data-Structure discussion on Arrays

What is Array?

Ans: Array is the most commonly used data storage structure because they offer a convenient jumping-off place for introducing data structure and for seeing how object -oriented programming and data structure relate to one another.here we are not talking about and specific to to any language because array,s are built in most programming language.

We will also examine special kind of array,the ordered array in which data stored in ascending or descending order.This arrangement makes possible a fast way of searching for a data item;binary search.We will talk about different operation on arrays such as Insertion,Deletion and searching .

Note: nElement=Number of elements in the array.

Searching in unordered array:

public boolean find(long searchKey)
{
   int j;
for(j=0;j<nElements;j++)
{
   if(array[j]==searchKey)
   {
      if(j==nElements)
           return false;
      else
           return true;
 
   }
}
}

Insertion in unordered array :

public void insert(long value)
{
  array[nElements]=value;
  nElements++;
}

Insertion in Ordered Array:

public void insert(long value)
{
int j;
for(j=0;j<nElems;j++)
{
if(a[j] > value)
{
break;
}
for(int k=nElems; k>j;k--)
{
a[k] = a[k-1];
a[j]=value;
nElems++;
}
}
}

Searching in Ordered Array:

public int find(long searchKey)
{
int lowerBound=0;
int upperBound=nElems-1;
int currIn;

while(true)
{
currIn=(lowerBound+upperBound)/2;
if(a[currIn] == searchKey)
return true;
else if(lowerBound > upperBound)
return nElems;

else
{
if(a[currIn] < searchKey)
lowerBound=currIn+1;
else
{
upperBound=currIn-1;
}

}
}


}


Deletion in Ordered Array:

public boolean delete(long value)
{
int j=find(value);
if(j==nElems)
return false;
else
{
for(int k=j;k<nElems;k++)
{
a[k] =a[k+1];
nElems--;
return true;
}
}

}