Arrays Theory & Sorting
Arrays Theory & Sorting
Advantages
o We can retrieve or sort the data efficiently.
o Random access: We can get any data located at an index
position.
o Declaration,allocation and initialization can be done in one
line
Disadvantages
o Size Limit: We can store only the fixed size of elements in the
array. It doesn't grow its size at runtime.
class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
//initialization
// int a[]={10,20,70,40,50};
// char a[]={‘C’,’O’,’M’,’P’,’U’,’T’,’E’,’R’};
//String a[]={“Amit”,”Ajay”,”Vikas”};
//input in array
for(int i=0;i<5;i++)
{
a[i]=sc.nextInt();
}
//traversing array
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
Output:
10
20
70
40
50
}
}
50 30 90 46 10
30 50 90 46 10
30 50 46 90 10
30 50 46 10 90 // first time when inner loop will get terminate
30 46 50 10 90
30 46 10 50 90 // second time “ “ “
30 10 46 50 90 // third time
10 30 46 50 90 // fourth time
import java.util.*;
class bubsort
{
int arr[];
int size;
static Scanner sc = new Scanner(System.in);
public bubsort(int s)
{
size=s;
arr=new int[size];
}
public void getdata()
{
import java.util.*;
class selection
{
int arr[];
int size;
static Scanner sc = new Scanner(System.in);
public selection(int s)
{
size=s;
arr=new int[size];
}
public void getdata()
{
}
y=arr[p];
arr[p]=arr[i];
arr[i]=y;
}
}
}
public void display()
{
import java.util.*;
class Insertion
{
int arr[];
int size;
static Scanner sc=new Scanner(System.in);
public Insertion(int s)
{
size=s;
arr=new int[size];
}
public void getdata()
{
}
public void display()
{
for(int i=0;i<size;i++)
{
System.out.println(arr[i]);
}
}
public static void main(String args[])
{
System.out.println("Eneter the size of array");
int a=sc.nextInt();
Insertion obj=new Insertion(a);
obj.getdata();
System.out.println("Original numbers are");
obj.display();
obj.arrange();
System.out.println("Numbers in descending order are");
obj.display();
}
}