Friday, October 27, 2006

Multi-dimensional arrays and the importance of arrangement

When I wrote about the lockers, I mentioned that the row of lockers was similar to a single dimension array. So, I guess it behooves me[I love to use words like behooves -makes my nose go up in the air, on its own accord ;^)] to talk about some array that has more than a single dimension. As people living in a 3D world, we are all quite comfortable with 2D and 3D but not quite as much with more than 3D [unless we are a theoritical physicists with a penchant for string theory] and hence I believe that understanding 2 dimensional and 3 dimensional arrays should be rather straight forward. But tell you what, in this article, let's not even go to 3D yet [or ever]. Let's make life simpler, and look at a 2D array.

Have you ever been to a small shoe shop? Most of them have at least an entire wall covered with pigeon holes that can hold a single shoe box each. From the floor to the roof, there are rows upon rows of pigeon holes. That entire wall and the bank of shelves there is what a 2D array looks like, and each pigeon hole is an element of that array. Basically, a 2D array is an array of arrays.

In a shoe shop near where I live, works a man, a short man, about 4ft tall on his toes. He is not very educated I think, definitely knows no programming, but nevertheless, can teach us a few things about data structures and algorithms and the importance of good data organization.

As a first step, he has built the 2D array of a shelf (with about 30 columns and 12 rows) on the wall. Then he has arranged the different makes, brands and models of shoes in such a way that each column of pigeon holes contain shoes of one brand and model. So far, so good. So, if we were to go in and ask to see a specific brand and model, he knows which column to go to.

Additionally, he has learnt from experience that the fastest selling shoes are those in sizes 6 through 8, and the next fastest moving sizes being children's sizes 3, 4, and 6. So what he has done is to keep all the shoes of sizes 6, 7, and 8 in pigeon holes that are at a convenient height for him [ in his case, rows 4 through 6]. Children sizes 3, 4, and 6 he has kept in rows 1, 2 and 3 [ for, he finds bending down to reach those shoes easier than having to use the ladder]. All other sizes, the slow moving ones, he has kept in rows 7 to 12. The little diagram below shows a section of the shelf and the arrangement of the shoes.


Finally, he has also used some judgement in assigning the various columns to the various brands and models. The more popular ones occupy columns that are closer to where he usually sits, near the entrance of the store, and the less popular ones occupy columns further inside the store.

In this way, he has ensured that he can access the most common brands, models and sizes with the least amount of effort.

Over the next few articles, lets take a leaf out of the shoe seller's book and look at ways of storing and arranging data so as to make our lives easier.

1 comment:

PVS said...

Hey what happened... u havent blogged for a long time.