Computer Science Canada Sort 2D array |
Author: | javanoob [ Thu Jan 22, 2009 12:07 pm ] | ||
Post subject: | Sort 2D array | ||
I've been given an assignment that requires me to (among other things) sort a 2d array of integers using bubble sort. I know how I would sort a single dimension of integers but I'm a little lost on how to extrapolate that to 2d.
So obviously the 2d bubble sort will go in the function sort2D, and the linear search I haven't coded yet...but that shouldn't be a problem. My teacher said something about treating the array as if it were only a single dimension, but I'm still not exactly sure how that would work?? Any help is appreciated. |
Author: | DemonWasp [ Thu Jan 22, 2009 12:22 pm ] | ||
Post subject: | RE:Sort 2D array | ||
The key here is that a 2-dimensional array isn't actually 2-dimensional in memory. Being able to address it as if it were a 2d array is just syntactic "sugar". In reality, the array is stored as a 1d array (this is by necessity; memory is actually essentially just one really big 1d array with elements 0..4 billion (for a machine with 4GB RAM)). How is it stored? The array is stored in (as I recall) row-major order. This means that the array first contains the first row, then the second row, then the third row, etc, all stuck together.
What this means is that you can use some C silliness to treat your (supposedly) 2D array as a 1D array. You can then sort the 1D array. If you think about it for a few minutes, I'm sure you can come up with a relatively simple way of translating the 1D coordinate and the 2D coordinates back and forth trivially; all you'll need to know is the length of each row (which appears to be N, from a glance at your code). |
Author: | javanoob [ Thu Jan 22, 2009 12:50 pm ] |
Post subject: | Re: Sort 2D array |
OK...is that what this would be for then? i = x * M + y x = i / M y = i % M I remember him briefly talking about this...but I can't remember exactly what each value would be. M is obviously the number of rows. Would i be the element I'm trying to find, and x and y be the row and column number? |
Author: | DemonWasp [ Thu Jan 22, 2009 2:11 pm ] | ||
Post subject: | RE:Sort 2D array | ||
You're pretty close. Here's how you logic it out, using our sample array:
If we want something from the first row (something, 0), then the value for i is simple (it's "something"). If we want something from the second row (something, 1), then the value for i is ( 1 row worth of columns ) + "something". In fact, i = y * (columns) + x in general. This is because there are (y rows) * (#columns) entries before that row begins, and x more on that row. To go the other way, we can use division similar to what you've got, but not quite the same. y = i / (#columns) x = i % (#columns) All you'd need to do to use this is code your sort to use a method called swap ( int pos1, int pos 2 ) which takes positions for a 1d array and converts them into positions for the 2d array. Your sort method can pretend it's dealing with a 1D array - in fact, it's identical; the only thing that's I should mention that there are ways of making C do that work for you, but they require a fairly advanced knowledge of exactly how C works. |