Algorithm

Spiral Matrix – clockwise and counter-clockwise

spiral-matrix
Written by Mushfiq Mammadov

Print matrix elements as spiral order from beginning the center element. It should be as the following picture:

Output:

 

The following video is short review about how the above code works:

You can also test it via this link.

 

Github link:

https://github.com/mmushfiq/SpiralMatrixSpringMVC

 

But for loop was used many times in the above code. So I optimized and rewrote code again:

Output:

 

Then it is added two features:

  1. If center element is more than 1 so you can choose it;
  2. All elements of matrix was included to spiral order. 

The last version of code after all updates:

Output:

 

Github link:

https://github.com/mmushfiq/SpiralMatrixConsole 

 

About the author

Mushfiq Mammadov

Leave a Comment

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.