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

Program n21

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)
7 views

Program n21

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/ 4

Program no: 21

import java.util.*;

public class Program_21 {

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

int m = array.length;

int n = array[0].length;

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

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

int minIndex = j;

for (int k = j + 1; k < n; k++) {

if (array[i][k] < array[i][minIndex]) {

minIndex = k;

// Swapping elements

int temp = array[i][j];

array[i][j] = array[i][minIndex];

array[i][minIndex] = temp;

public int[] findLocation(int[][] array, int searchNum) {

int m = array.length;

int n = array[0].length;
for (int i = 0; i < m; i++) {

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

if (array[i][j] == searchNum) {

return new int[] {i, j};

return null;

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] + " ");

System.out.println();

public void main() {

Scanner scanner = new Scanner(System.in);

// Accepting array dimensions

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

// Declaring 2D array

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

// Accepting array elements row-major wise

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

// Sorting array using Selection Sort

selectionSort(array);

// Displaying sorted array

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

printArray(array);

// Accepting number to search

System.out.print("Enter a number to search: ");

int searchNum = scanner.nextInt();

// Finding location of searchNum

int[] location = findLocation(array, searchNum);

// Displaying location

if (location != null) {

System.out.println("Number found at: (" + location[0] + ", " + location[1] + ")");

} else {

System.out.println("Number not found! Please try again...");}}}


Output:

You might also like