Sort the array elements in ascending order


A class Collection contains an array of  100 integers. using the following class description create an array with common elements from two integer arrays.

Class name  : Collection
Data members
arr[], len
Member functions:
Collection()   : default constructor
Collection(int) : parameterized constructor to assign the length of the array
void input()    : to accept the array elements
Collection common(Collection) : returns a Collection containing the common elements of current Collection object and the Collection object passed as a parameter.
void arrange()  : sort the array element of the object containing common elements in ascending order using any sorting technique
void display()  : displays the array elements
Define the main() to call above member methods.

Program

import java.util.*;
class Collection
{
int arr[];
int len;
Collection()
{
arr=new int[10];
len=10;
}
Collection(int size)
{
arr=new int [size];
len=size;
}
public void input(){
Scanner sc=new Scanner(System.in);
int val=0;
for(int i=0;i<len;i++)
{
System.out.println(“Enter the elements “+(i+1));
val=sc.nextInt();
arr[i]=val;}
}
private void setLength(int le)
{
len=le;
}
public Collection common(Collection c2)
{
Collection c3=new Collection();
int val, k=0;
for(int i=0;i<len;++i)
{
val=arr[i];
for(int j=0;j<c2.arr.length ; ++j) {
if(val==c2.arr[j]) {
k++;
}
}
}c3.setLength(k);
return c3;
}
public void display()
{
for(int i=0;i<len;i++) {
System.out.print(arr[i]+””);
}
System.out.println();
}
public void arrange()
{
for(int i=len-1;i>=0;i–)
{int highestIndex=i;
for(int j=i;j>=0;j–)
{
if(arr[j]>arr[highestIndex])
highestIndex=j;
}
int temp=arr[i];
arr[i]=arr[highestIndex];
arr[highestIndex]=temp;
}
display();
}

public static void main(String args[])
{
Collection c1=new Collection(10);
Collection c7= new Collection();
c1.input();
System.out.println(“Display the entered matrix”);
c1.display();
System.out.println(“Ascending order”);
c1.arrange();
c7=c7.common(c1);
}
}

Output:

Enter the elementts 1
9
Enter the elements 2
8
Enter the elements 3
7
Enter the elements 4
6
Enter the elements 5
5
Enter the elements 6
4
Enter the elements 7
3
Enter the elements 8
2
Enter the elements 9
1
Enter the elements 10
10
Display the entered matrix
9 8 7 6 5 4 3 2 1 10
Ascending order
1 2 3 4 5 6 7 8 9 10