Java Program to Print Spiral Pattern of Numbers

Spiral patterns are a popular concept in computer graphics and can be used to visualize data in a unique and interesting way. In this section, we will explore how to create a spiral pattern of numbers using Java. We will cover the logic behind the spiral pattern and step-by-step implementation and provide the complete Java code.

Understanding the Spiral Pattern

A spiral pattern of numbers typically starts from a central point and winds its way outward in a circular manner. For simplicity, we will start from the top-left corner of a matrix and proceed in a clockwise direction until we fill the entire matrix.

Let's consider an example of a 4x4 matrix:

Step 1: Initialize the Matrix

Objective: Create an empty 2D array (matrix) to hold the numbers.

Procedure:

  1. Determine the size n of the matrix. It will be an n x n matrix.
  2. Declare and initialize a 2D array matrix of size n x n.

Step 2: Define Boundaries

Objective: Keep track of the boundaries of the matrix that need to be filled.

Procedure:

  1. Initialize variables to represent the current boundaries of the matrix:
  2. top: initially set to 0.
  3. bottom: initially set to n - 1.
  4. left: initially set to 0.
  5. right: initially set to n - 1.

Step 3: Fill the Matrix

Objective: Use a loop to fill the matrix in a spiral order by following the boundaries.

Procedure:

  1. Initialize a variable value to 1 that will be used to fill the matrix incrementally.
  2. Use a while loop to continue filling the matrix until all cells are filled (value <= n * n).
  3. Traverse from left to right on the top row:
    • Use a for loop to iterate from left to right.
    • Assign value to each element in the top row and increment value.
    • After the loop, increment the top boundary to move it down.
  4. Traverse from top to bottom on the right column:
    • Use a for loop to iterate from top to bottom.
    • Assign value to each element in the right column and increment value.
    • After the loop, decrease the right boundary to move it left.
  5. Traverse from right to left on the bottom row:
    • Use a for loop to iterate from right to left.
    • Assign value to each element in the bottom row and increment value.
    • After the loop, decrease the bottom boundary to move it up.
  6. Traverse from bottom to top on the left column:
    • Use a for loop to iterate from bottom to top.
    • Assign value to each element in the left column and increment value.
    • After the loop, increment the left boundary to move it right.
  7. Repeat the process:
    • Continue looping until all cells in the matrix are filled.

Java Implementation

Below is the complete Java code to generate and print a spiral pattern of numbers for a given n x n matrix.

File Name: SpiralPattern.java

Output:

  1   2   3   4 
 12  13  14   5 
 11  16  15   6 
 10   9   8   7

Explanation

The program begins by initializing the matrix and defining the size n (for example, 4). We then create a 2D array matrix to hold the numbers. The fillSpiralMatrix() method is responsible for filling the matrix in a spiral order. We initialize value to 1, which is the starting number, and use variables top, bottom, left, and right to keep track of the current boundaries of the matrix.

Using nested loops, we fill the matrix in a spiral order. Each loop iteration covers one side of the current boundary (top, right, bottom, left). After filling one side, we update the boundary to move inward. This process continues until all cells in the matrix are filled.

Time and Space Complexities

Time Complexity

The time complexity of the aboveprogram is O(n^2).

Space Complexity

The space complexity of the program is O(n^2).