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

Program n20

Java Program

Uploaded by

debojit052007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Program n20

Java Program

Uploaded by

debojit052007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Program no: 20

import java.util.Scanner;

public class Program_20 {

public void insertionSortColumnWise(int[][] array) {

int m = array.length;

int n = array[0].length;//define length for the function

for (int j = 0; j < n; j++) {//sort the array column major wise

for (int i = 1; i < m; i++) {

int key = array[i][j];

int k =i-1;

while (k >= 0 && array[k][j] > key) {

array[k + 1][j] = array[k][j];//shift elements of the array

k--;

array[k + 1][j] = key;

public void printArray(int[][] array) {

for (int i = 0; i < array.length; i++) {

for (int j = 0; j < array[0].length; j++) {

System.out.print(array[i][j] + " ");//print out the array

}
System.out.println();

public void main() {

Scanner scanner = new Scanner(System.in);//accept values from user

System.out.print("Enter the number of rows (m): ");

int m = scanner.nextInt();

System.out.print("Enter the number of columns (n): ");

int n = scanner.nextInt();

int[][] array = new int[m][n];

System.out.println("Enter the array elements:");

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

for (int j = 0; j < n; j++) {

array[i][j] = scanner.nextInt();

System.out.println("Original array:");

printArray(array);

insertionSortColumnWise(array);

System.out.println("Sorted array:");

printArray(array);

}
Output:

You might also like