I want to sort a two-dimensional array like this:
4 2 5
1 3 7
6 9 8
to obtain like this:
1 2 3
4 5 6
7 8 9
using language C.
A couple of options for tackling this problem could include:
- Convert the input 2-D array to a 1-D array, and use a standard library function such as sort.
As someone suggested here, I could have a define or a function to access each value using a 1-D index, and employ a insertion-sort or a bubble-sort.
ARRAY(index) array[(index) / 3][(index) % 3]
I do not like either option because the first one would require a temporary space, and the second one may be slow. My 2-D array would be large in rows and columns. I am also lazy, and wonder if I could employ a standard C library function of qsort. It seems like that I cannot use qsort with 2-D array to get a result of all elements sorted just like 1-D array.
I am given a 2-D array so converting it to a 1-D array is not an option for me.
Thank you for any suggestions.