answersLogoWhite

0


Best Answer

Any multi-dimensional array can be flattened into a linear array. For instance,

[[1,2,3],[4,5,6],[7,8,9]]

can be flattened into

[1,2,3,4,5,6,7,8,9].

So a solution to your problem (certainly not the most efficient) would be to flatten the 2d array into a linear array, and sort using a traditional sorting algorithm or Arrays.sort. You would then insert the sorted elements back into the 2d array. This would have nlog(n) complexity.

An implementation below:

public static void sort2d(int[][] arr)

{

int r = arr.length;

int c = arr[0].length;

int[] flat = new int[r*c];

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

for (int j = 0; j < c; j++)

flat[i*c+j] = arr[i][j];

Arrays.sort(flat);

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

arr[i/r][i%c] = flat[i];

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a program to sort a two dimensional integer array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write a program that accepts 50 integer values and sort them in basic programming?

Create an array with 50 elements and input the integers one a time, filling the array. Use an insertion sort on the array for each input except the first. Alternatively, input the values first and then use insertion sort.


How many types of sorting array in C programming?

You can sort an array with any method you want, but there is a built-in qsort function, declared in stdlib.h (see the attached link).bubble sort, quick sort, insertion sort, merge sort, radix sort and lot more..merge sort is the most efficient one..


Can you modify the bubble sort algorithm to search to sort an array of characters instead of array of integers?

The bubble sort algorithm can be applied to an array of characters. Every character can be translated to an integer equivalent via the ascii table


What does the word array mean in math?

An array is a set of numbers that form some sort of regular arrangement. A linear array is a 1-dimensional array consisting of a row or a column of a set of numbers. A 2-dimensional array is a rectangular arrangement of numbers. And there are arrays with higher dimensions. The elements of an array need not be numbers: they could be variables, functions or expressions. In other words, it's a picture to describe a multiplication problem.


Write a java program to sort a list of numbers?

The simplest would be to put the numbers into an int[] (integer array) and pass that to java.util.Arrays.sort(int[]) (a static method), which will sort the array in ascending numerical order. Use a float[] or double[] if you need to sort non-whole numbers. You can also use the Collections.sort(List) method to sort the List directly. Or the Collections.sort(List, Comparator) if you wish to specify your own sorting criteria.


Program to print sorting of an array in clanguage?

/* PROGRAM TO SORT ARRAY ELEMENTS USING BUBBLE SORT*/ #include #include void main() { int i,j,n,t,a[50]; clrscr(); printf("ENTER THE ARRAY SIZE:\n"); scanf("%d",&amp;n); printf("ENTER THE ARRAY ELEMENTS:\n"); for(i=0;i


8086 assembly program to sort a list of integers?

assembly language program for sorting an array using 8086 microprocessor.


Sorting an array in PHP without using sort function?

plz as soon as possible give me the program for shorting an array in asscending order without using any sort function in c++


What is the Java program for sorting an array?

Java has a very efficient built in implementation of quick sort. You can use it on any array of primitives or Comparable Objects by invoking Arrays.sort(&lt;array&gt;) See related link.


How do you sort the given contents of an array?

You would sort the given elements of an array by a bubble sort or heap sort code!!


Write c program to find median?

If you are using an array : sort using qsort() then take middle element.


Write a program to sort elements of an array in ascending order?

import java.util.Arrays; public class arraysort { public static void main(String[] a) { int array[] = { 2, 5, -2, 6, -3 }; Arrays.sort(array); for (int i : array) { System.out.println(i); } } }