Grouping neighbouring entrys with the same value in a 2d array.

I have a 2d array and i want to group together neighbors of the same value.

Lets say int[,] array {

0,0,0,0,0,0,1,0,0,0,
0,1,1,0,0,0,1,0,0,0,
0,1,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,1,1,1,0,
0,0,0,0,0,0,1,1,1,0
}

There are three “clusters” of 1:s. I want to add them to a dictionary with some variable for identification. So maybe first add the neighboring values to a list, add that list to a dictionary, clear the list and move onto the next cluster.

Just pointing me in the right direction, some wikipedia entry on some relevant sorting algorithm etc. would be greatly appreciated!

Look into Array.Sort (Google C# sort array)

Your array is a bit difficult to fit into the “standard” arranged this way.

If, indeed, you have rows of 10 numbers each, then I’d suggest you consider a class which contains an array of 10 entries, and store instantiations of that class as each entry in the array. That makes sorting simpler, as you provide an iComparable object that “teaches” the sorting system how to compare the class.

The general topic you’re asking about is data structures, a part of computer science dealing with how data is organized for best usage feature and performance. For example, if you read a lot more often than you insert, sorting an array is a good choice. However, if you frequently insert and remove (more than or about as much as you read), then a binary tree may perform better. Many prefer hash tables as a compromise between these two extremes, especially for small volumes.