Array Sorting 2D
Array Sorting 2D
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.
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}};