Pergunta

I want to make a sudoku using c#. Should I use a jagged or multi-dimensional array. And why?

I know that jagged arrays are faster and more efficient for simple array logic, such as sudoku. But maybe there are other arguments?

Thanks in advance.

Edit: I need to perform some solving methods on the sudoku. Like the backtracker algorithm. I found that multi-dimensional arrays have a better syntax.

I guess my question is: which is simplest to use, implement and modify?

Foi útil?

Solução

Don't go on making things complicated just for the sake of premature optimisation. Use a simple multidimensional array and then if later on you find that it is causing performance problems, refactor your code to take this into account.

Hint: Any performance problems you may have won't come from the choice of multidimensional or jagged arrays.

Outras dicas

Choose the data structure that best represents the specific data.

Are you using the array to represent a grid of things with fixed dimensions (for example, the sudoku solution)? Then use a multi-dimensional array.

Can the number of things you want to store in the array change (for example, the sudoku start pattern or potential values a user enters for a sudoku cell)? Then maybe a jagged array is better.

You could also have multiple types of arrays in your program, for example a multi-dimensional array to store the solution and a jagged array to store other data. Or some different data structure entirely.

The bottom line is that for something like sudoku with a relatively small amount of data, performance isn't going to be your biggest problem. You will be better off structuring the program as clearly as possible and worrying about performance later.

I personally find for something like this a jagged array is clearer to read but that comes down to personal preference. At the end of the day Sudoku is not going to have performance issues so use whichever you find clearer.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top