0% found this document useful (0 votes)
82 views

Arrays Theory & Sorting

Java arrays allow storing of fixed number of elements of the same type. Elements are stored in contiguous memory locations and accessed via indexes. Arrays can be one-dimensional, storing elements in a single list, or multi-dimensional, storing elements in a grid. Common operations on arrays include sorting, retrieving, and random access of elements. While arrays provide efficient access and sorting, their size is fixed at creation time.

Uploaded by

McDonald Trumpen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Arrays Theory & Sorting

Java arrays allow storing of fixed number of elements of the same type. Elements are stored in contiguous memory locations and accessed via indexes. Arrays can be one-dimensional, storing elements in a single list, or multi-dimensional, storing elements in a grid. Common operations on arrays include sorting, retrieving, and random access of elements. While arrays provide efficient access and sorting, their size is fixed at creation time.

Uploaded by

McDonald Trumpen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Java Arrays

Normally, an array is a collection of similar type of elements which


have a contiguous memory location.

Java array is an object which contains elements of a similar data


type. Additionally, The elements of an array are stored in a contiguous
memory location. It is a data structure where we store similar
elements. We can store only a fixed set of elements in a Java array.

Array in Java is index-based, the first element of the array is stored at


the 0th index, 2nd element is stored on 1st index and so on.

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

Multidimensional Array in Java


In such case, data is stored in row and column based index (also
known as matrix form).

Example to instantiate Multidimensional Array in Java

1. int arr[][]=new int[3][3];//3 row and 3 column

Example to initialize Multidimensional Array in Java


class Testarray3{
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
// 2D array
// input
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
A[i][j]=sc.nextInt();
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}}
Output:
1 2 3
2 4 5
4 4 5

SINGLE DIMENSIONAL ARRAY


Q. 1. Program to input numbers in array and print the largest and smallest
number of array along with position in array.
import java.util.*;
class maxmin
{
int arr[];
int size;
int max, min,x,y;
static Scanner sc=new Scanner(System.in);
public maxmin(int s)
{
size=s;
max=0;
min=0;
x=0;
y=0;
arr=new int[size];
}
public void getdata()
{
System.out.println("Enter numbers in array");
for(int i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
}
public void find()
{
max=arr[0];
min=arr[0];
int i;
for(i=1;i<size;i++)
{
if(arr[i]>max)
{
max=arr[i];
x=i;
}
if(arr[i]<min)
{
min=arr[i];
y=i;
}
}
}
public void display()
{
System.out.println("largest number is :"+max+ ”at Position”+x);
System.out.println("smallest number is : "+min+” at Position “+y);
}
public static void main(String args[])
{
System.out.println("Eneter the size of array");
int a=sc.nextInt();
maxmin obj=new maxmin(a);
obj.getdata();
obj.find();
obj.display();
}
}
Sorting : Arranging data in ascending or descending order is
called sorting.
Bubble sort logic of arranging data in descending order
int n = arr.length;
int temp=0;
for(int i=0; i < n-1; i++){
for(int j=0; j < (n-1-i); j++)
{
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}

}
}

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

Q. 2 Program to input numbers in array and arrange the


numbers in descending order using bubble sort technique.

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()
{

System.out.println("Enter numbers in array");


for(int i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
}
public void arrange()
{
int i,j,x=0;
for(i=0;i<size-1;i++)
{
for(j=0;j<size-1-i;j++)
{
if(arr[j]<arr[j+1])
{
x=arr[j];
arr[j]=arr[j+1];
arr[j+1]=x;
}
}
}
}
public void display()
{

for (int i=0;i<size;i++)


{
System.out.println(arr[i]);
}
}
public static void main(String args[])
{
System.out.println("Enter the size");
int a=sc.nextInt();
bubsort obj=new bubsort(a);
obj.getdata();
System.out.println("Original numbers are");
obj.display();
obj.arrange();
System.out.println("Numbers is descending order are");
obj.display();
}
}

Selection sort logic of arranging data in descending order:


int n = arr.length;
int temp=0,x,p;
for(int i=0; i < n-1; i++)
{
x=arr[i];
p=i;
for(int j=i+1; j < n; j++)
{
if(arr[j] > x)
{
x=arr[j];
P=j;
}
}
temp = arr[p];
arr[p] = arr[i];
arr[i] = temp;
}
Q. 3 Program to input numbers in array and arrange the
numbers in descending order using selection sort
technique.

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()
{

System.out.println("Enter numbers in array");


for(int i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
}
public void arrange()
{
int i,j,x=0,p,y;
for(i=0;i<size-1;i++)
{
x=arr[i];
p=i;
for(j=i+1;j<size;j++)
{
if(arr[j]>x)
{
x=arr[j];
p=j;

}
y=arr[p];
arr[p]=arr[i];
arr[i]=y;
}
}
}
public void display()
{

for (int i=0;i<size;i++)


{
System.out.println(arr[i]);
}
}
public static void main(String args[])
{
System.out.println("Enter the size");
int a=sc.nextInt();
selection obj=new selction(a);
obj.getdata();
System.out.println("Original numbers are");
obj.display();
obj.arrange();
System.out.println("Numbers is descending order are");
obj.display();
}
}

Q. 4 Program to input numbers in array and arrange the


numbers in descending order using Insertion sort
technique.

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()
{

System.out.println("Enter numbers in array");


for(int i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
}
public void arrange()
{
int x,y,i;
for(i=1;i<size;i++)
{
x=arr[i];
y=i-1;
while(y>=0&&arr[y]<x)
{
arr[y+1]=arr[y];
y--;
}
arr[y+1]=x;
}

}
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();
}
}

You might also like