You are on page 1of 4

Shri Vile Parle Kelavani Mandal's

C.N.M.School & N.D.Parekh Pre-Primary School

Academic year 2023-2024


Std. X Subject: Computer Application

Worksheet – Array (Sorting )


The process of arranging data elements in the array which are randomly placed in either ascending
order or descending order is called sorting of the array.
Sorting of Array can be achieved through various ways. The two important sorting methods are:
1. Selection Sorting: The maximum or minimum element in array is determined and it is swapped
with first element, the same process is repeated for the second element, third element and so on. Such
method is called selection sorting.
import java.util.*;
class q3
{
public static void main( )
{ Scanner sc=new Scanner(System.in);
int a[] = new int [10];
System.out.println(" Enter 10 nos. for an array"); // accepting
for(int i=0;i<10;i++)
{ a[i]=sc.nextInt( );
}
int small,tmp,pos;
for(int i=0;i<10;i++)
{
small=a[i];
pos=i;
for(int j=i+1;j<10;j++)
{
if(a[j]<small)
{ small=a[j];
pos=j;
}
}
tmp=a[i];
a[i]=a[pos];
a[pos]=tmp;
}
System.out.println("Array in ascending order is --> ");
for(int i=0;i<10;i++)
System.out.print(a[i]);
} }
Bubble Sorting: - In bubble Sorting adjacent elements are compared. To arrange data elements in
ascending order, adjacent elements are compared, if the adjacent element is greater then they get inter
changed (swapping). It happens for the entire data. The process is repeated until all the data is sorted.

class Main
{
public static void main( )
{
int a[] = {64, 34, 25, 12, 22, 11, 90};
int len = a.length; // calculating the length of array
for (int i = 0; i < len-1; i++)
{
for (int j = 0; j < len-i-1; j++)
{
if (a[j] > a[j+1]) // if(a[j].compareTo(a[j+1])>0)
{
int temp = a[j]; // String temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (int i = 0; i < len; i++)
{
System.out.print(a[i] + " "); //printing the sorted array
}
}
}
Define a class and store the given city names in a single dimensional array. Sort these names in
alphabetical order using the Bubble Sort technique only.
INPUT : Delhi, Bangalore, Agra, Mumbai, Calcutta
OUTPUT : Agra, Bangalore, Calcutta, Delhi, Mumbai

************************
2D Array
A two-dimensional array, also known as a 2D array, is a collection of data elements arranged in a grid-
like structure with rows and columns. Each element in the array is referred to as a cell and can be
accessed by its row and column indices/indexes.

int Array[][]= new int[R][C];

1. Accept and display integer values in 2D array.


import java.util.Scanner;
public class Main
{
public static void main( )
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int rows = scan.nextInt();
System.out.print("Enter number of columns: ");
int columns = scan.nextInt();
int Array[][]= new int[rows][columns];

for (int i = 0; i < rows; i++) {


for (int j = 0; j < columns; j++)
{
System.out.println("Enter number: ");
Array[i][j]= scan.nextInt();
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
System.out.print(Array[i][j]+ " ");
}
System.out.println();
}
}
}
******************************
2. Assign integer values in an 2D array and display matrix along with left and right diagonal
values.
public class MatrixDiagonals {
static public void main() {
int[][] input_matrix = {{ 4, 5, 6, 7 },{ 1, 7, 3, 4 },{ 5, 8, 4, 6 }, { 3, 2, 5, 1 }};
int matrix_size = 4;
System.out.println("The matrix is defined as : ");
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++)
System.out.print( input_matrix[i][j] + " ");
System.out.print("\n");
}
int left = 0, right = 0;
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++) {
if (i == j)
left += input_matrix[i][j];
if ((i + j) == (matrix_size - 1))
right += input_matrix[i][j];
}
}
System.out.println("\n The sum of Left diagonal elements of the matrix is: " + left);
System.out.println("\n The sum of Right diagonal elements of the matrix is: " + right);
}
}}

3. Calculate the sum of elements in each row and each column of the given matrix.
public class SumofRowColumn
{
public static void main()
{
int rows, cols, sumRow, sumCol;

//Initialize matrix a
int a[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

//Calculates sum of each row of given matrix


for(int i = 0; i < 3; i++){
sumRow = 0;
for(int j = 0; j < 3; j++){
sumRow = sumRow + a[i][j];
}
System.out.println("Sum of " + (i+1) +" row: " + sumRow);
}
//Calculates sum of each column of given matrix
for(int i = 0; i < 3; i++){
sumCol = 0;
for(int j = 0; j < 3; j++){
sumCol = sumCol + a[j][i];
}
System.out.println("Sum of " + (i+1) +" column: " + sumCol);
}
}
}
*************************************

You might also like