I have found many topics to learn sorting,searching of 1 d array in many of books but still could not find any topic about sorting and searching in 2D ARRAY.I am a university student and i need urgent help please help me from which book or which website i could easily and throughly learn SORTING AND SEARCHING IN 2D ARRAY?
2D arrays are exactly like 1D arrays. Just apply the same methodology but include an additional for loop and do some additional logic to compare the current 1D array with the next 1D array.
constint n = 10, m = 10;
float a[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> a[i][j];
bool sorted = true;
while (sorted)
{
for (int i = 0; i < n; i++)
{
// normal sorting for array a[i][...];
for (int j = 0; j < (m-1); j++)
{
if (a[i][j] > a[i][j+1])
{
float temp = a[i][j];
a[i][j] = a[i][j+1];
a[i][j+1] = temp;
sorted = false;
}
}
// Lets compare the last value in this array to the first value in the next array
if (i < n-1) //don't do this on the last array.
{
if (a[i][m-1] > a[i+1][0])
{
float temp = a[i][m-1];
a[i][m-1] = a[i+1][0];
a[i+1][0] = temp;
sorted = false;
}
}
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cout << a[i][j] << ','; //They are all in order.
Now this is not the most efficient way to do this. It will be very slow if the array size is large, but you can see what we've done.