There are three ways to sort a two-dimensional array. We can sort each row so that the rows remain where they are but the elements within each row are sorted. Or we can sort the rows themselves so the elements within each row remain where they are but the rows are sorted. Or we can do both, sorting the elements in each row and then sorting the rows. The following program demonstrates all three methods using the same bubble-sort method (see the sort function near the top of the code).
Keep in mind that a two-dimensional array is nothing more than a one-dimensional array of one-dimensional arrays. That is, array A[2][3] is a one-dimensional array of 2 elements where each element is a one-dimensional array of 3 elements. In other words, 2 rows with 3 elements per row. However, while static two-dimensional arrays must have the same number of elements in each row, dynamic two-dimensional arrays can have variable length rows. This is achieved by maintaining a one-dimensional array of pointers on the stack or on the heap, where each pointer refers to a separate row. The elements within each row must be contiguous, but the rows themselves can reside anywhere in memory, non-contiguously, and each row can have a different length. If the rows are the same length then you can access the individual elements just as you would with a static array using array subscripts. However, with variable length rows, you must dereference the row and determine its length before accessing the elements within that row.
The program below uses vectors rather than arrays, but a vector is really nothing more than a one-dimensional dynamic array that knows its own size at all times. Vectors are therefore much easier to work with than arrays because all the memory allocations are done for us Behind the Scenes. A two-dimensional vector is nothing more than a vector of vectors which is easier to imagine as begin a vector of rows, where each row has variable length. However, the example below uses fixed-length vectors to implement an array of 5 rows with 4 elements per row.
Due to the fact that every row must be contiguous within itself and that rows may have variable length, it is always going to be easier to sort two-dimensional arrays by row rather than by column. If you need to sort by column rather than row, rather than write a separate and needlessly complex function to cater for this, it is simpler to just orient your array in memory or on disk so that you can sort by row instead. Note that it is trivial to change the orientation of an array whilst printing the array (simply swap the inner and outer loops) so there is no need to physically store the data in the same orientation as the output. The only thing that actually changes is how you visualise the data: whether as a vector of horizontal rows (actual rows) or as a vector of vertical rows (actual columns).
#include<iostream>
#include<iomanip>
#include<vector>
#include<random>
#include<time.h>
template<typename T>
void sort (std::vector<T>& A)
{
unsigned unsorted = A.size();
if (unsorted > 1)
{
do
{
unsigned new_size = 0;
for (unsigned index=1; index<unsorted; ++index)
{
if (A[index]<A[index-1])
{
std::swap (A[index], A[index-1]);
new_size = index;
}
}
unsorted = new_size;
} while (unsorted);
}
}
template<typename T>
void print_array(std::vector< std::vector<T> >& arr)
{
for (unsigned row=0; row<arr.size(); ++row)
{
for (unsigned col=0; col<arr[row].size(); ++col)
{
std::cout << '\t' << arr[row][col];
}
std::cout << std::endl;
}
std::cout << std::endl;
}
template<typename T>
void randomise_array(std::vector< std::vector<T> >& arr)
{
// Pseudo-random number generator (range: 1 to 50).
std::default_random_engine generator;
generator.seed ((unsigned) time (NULL));
std::uniform_int_distribution<unsigned> distribution (1, 50);
for (unsigned row=0; row<arr.size(); ++row)
{
for (unsigned col=0; col<arr[row].size(); ++col)
{
arr[row][col] = distribution (generator);
}
}
}
int main()
{
// Instantiate a 5x4 array
std::vector< std::vector<unsigned> > original (5);
for (unsigned row=0; row<5; ++row)
original[row].resize (4);
randomise_array(original);
// Always work from a copy of the original
std::vector< std::vector<unsigned> > copy = original;
std::cout << "Original array:\n" << std::endl;
print_array(copy);
std::cout << "Sort each row individually:\n" << std::endl;
for (unsigned row=0; row<copy.size(); ++row)
sort (copy[row]);
print_array(copy);
copy = original;
std::cout << "Original array:\n" << std::endl;
print_array(copy);
std::cout << "Sort the rows:\n" << std::endl;
sort(copy);
print_array(copy);
copy = original;
std::cout << "Original array:\n" << std::endl;
print_array(copy);
std::cout << "Sort each row individually and then sort the rows:\n" << std::endl;
for (unsigned row=0; row<copy.size(); ++row)
sort (copy[row]);
sort (copy);
print_array(copy);
}
Example output:
Original array:
43 14 8 5
15 12 49 9
31 27 4 35
15 11 37 6
32 42 35 4
Sort each row individually:
5 8 14 43
9 12 15 49
4 27 31 35
6 11 15 37
4 32 35 42
Original array:
43 14 8 5
15 12 49 9
31 27 4 35
15 11 37 6
32 42 35 4
Sort the rows:
15 11 37 6
15 12 49 9
31 27 4 35
32 42 35 4
43 14 8 5
Original array:
43 14 8 5
15 12 49 9
31 27 4 35
15 11 37 6
32 42 35 4
Sort each row individually and then sort the rows:
4 27 31 35
4 32 35 42
5 8 14 43
6 11 15 37
9 12 15 49
Wright a 'C' program for storage representation of 2-D array.
A single dimensional array is an array of items. A two-dimensional array is an array of arrays of items.
yes
A two dimensional array is a one-dimensional array of one-dimensional arrays. That is, just as we can have an array of integers, we can also have an array of integer arrays. This idea can be extended such that we can have an array of two-dimensional arrays (a three-dimensional array), and so on. We typically use a two-dimensional array to represent a table of rows and columns, where each row is a one-dimensional array.
one dementional array and two dementional array
Wright a 'C' program for storage representation of 2-D array.
A single dimensional array is an array of items. A two-dimensional array is an array of arrays of items.
yes
A one dimensional array is a scalar value repeated one or more times.A two dimensional array is an array of one dimensional arrays.A three dimensional array is an array of two dimensional arrays, and so forth.The one dimensional array is like a list of things, where the two dimensional array is like an array of things. (Think one row of a spreadsheet versus the whole spreadsheet.)[addendum]Every level of array depth is also a level of pointer depth. For example: A 3 dimensional int array is an int***. So a one dimensional int array is an int*, and a two dimensional int array is an int**. This is only important if you are doing pointer work, but it can become very important.
array type
A two-dimensional array is the simplest multi-dimensional array and is implemented as a one-dimensional array where every element is itself a one-dimensional array. We can imagine a two-dimensional array as being a table of rows and columns where every row is an array in its own right. A three-dimensional array is simply a one-dimensional array of two-dimensional arrays, which can be imagined as being an array of tables. Extending the concept, a four-dimensional array is a table of tables. Multi-dimensional arrays may be jagged. That is, a two-dimensional array may have rows of unequal length. Unlike regular arrays, jagged arrays cannot be allocated in contiguous memory. Instead, we use the outer array (the first dimension) to store pointers to the inner arrays. An array of strings (character arrays) is an example of a two-dimensional jagged array.
A two dimensional array is a one-dimensional array of one-dimensional arrays. That is, just as we can have an array of integers, we can also have an array of integer arrays. This idea can be extended such that we can have an array of two-dimensional arrays (a three-dimensional array), and so on. We typically use a two-dimensional array to represent a table of rows and columns, where each row is a one-dimensional array.
one dementional array and two dementional array
A one dimensional array is an array of objects that goes in one "direction". Any array with only one [] is a one dimensional array. For example: int numbers[6]; is a one dimensional array. int numbers[6][3]; is a two dimensional array.Graphical terms:One dimensional array[4]:14 - 75 - 8164 - 234Two dimensional array[2][3]:47 - 178108 - 8517 - 128It didn't come out quite how I wanted it...
Members of the Priority Club Rewards program are privy to a vast array of benefits and discounts. One can apply to become a member through the company's official website.
A two-dimensional array.
An array is simply a contiguous block of memory containing two or more elements. There are two types of array: a static array which is allocated on the stack at compile time; and a dynamic array which is allocated on the heap at runtime. Both can be one-dimensional or multi-dimensional. A one-dimensional array can be likened to a row (or column) of chessboard squares, with as many squares as required to store all the elements. A multi-dimensional array is any array with two or more dimensions. A two-dimensional array can be likened to the whole chessboard, where any square can be identified by its row and column index. However the dimensions needn't be equal. A two-dimensional array can also be imagined as a one-dimensional array where every element is simply another one-dimensional array. Three-dimensional arrays can be likened to a cube, or as a one-dimensional array of two-dimensional arrays. A four-dimensional array can be linked to a one-dimensional array of three-dimensional arrays, and so on. Although every one-dimensional array must be allocated in contiguous memory, multi-dimensional arrays can be dynamically allocated so that each dimension is itself a separately allocated one-dimensional array of pointers to the next dimension, making it possible to allocate extremely large arrays over a series of smaller allocations rather than as a single contiguous block.