Java Data Structure and Algorithms - Sparse Array

1. Linear and Nonlinear Structures Data structures include linear and non-linear structures. ...
linear structure
Nonlinear structure
1. Basic Introduction
2. Processing methods for sparse arrays
3. Ideas for Converting Two-Dimensional Arrays to Sparse Arrays
4. Ideas for converting dilute sulfur arrays to original two-dimensional arrays
Code - Sparse Array

1. Linear and Nonlinear Structures

Data structures include linear and non-linear structures.

linear structure

  1. As the most common data structure, linear structure is characterized by one-to-one linear relationship among data elements.
  2. Linear structures have two different storage structures, sequential storage and chain storage. A linear table stored sequentially is called a sequential table, and the elements stored in the sequential table are continuous
  3. A linear table stored in a chain is called a chain table. Stored elements in a chain table are not necessarily continuous. Data elements and address information of adjacent elements are stored in element nodes.
  4. Common linear structures are arrays, queues, chained lists, and stacks

Sequential Storage - Arrays
Chained Storage - Chain List

Nonlinear structure

Nonlinear structures include: two-dimensional arrays, multidimensional arrays, generalized tables, tree structures, graph structures

2. Sparse sparsearray arrays

Gobang: Save, Exit, Continue

1. Basic Introduction

Sparse arrays can be used to save an array when most of the elements in the array are 0 or the same value.

2. Processing methods for sparse arrays

  1. The record array has rows, columns, and different values
  2. Reduce program size by recording rows, columns, and values of elements with different values in a small array

3. Ideas for Converting Two-Dimensional Arrays to Sparse Arrays

  • Traverse the original two-dimensional array, sum the number of valid data
  • A sparseArr int[sum+1][3] array of dilute sulfur can be created from the sum
  • Save valid data from a two-dimensional array into a sparse array

4. Ideas for converting dilute sulfur arrays to original two-dimensional arrays

  • Read the first row of the dilute sulfur array first, and create the original two-dimensional array based on the data of the first row, such as chesser2 = int[11][11] above
  • Read the data in the last few rows of the dilute sulfur array and assign it to the original two-dimensional array.

Code - Sparse Array

package com.blue; public class SparseArray { public static void main(String[] args) { // Create an original two-dimensional array 11*11 // 0: No chess, 1: Black 2: Table Blue int chessArr[][] = new int[11][11]; chessArr[1][2] = 1; chessArr[2][3] = 2; chessArr[4][5] = 2; // ============The output original two-dimensional array======================= System.out.println("Output original two-dimensional array"); for (int[] row:chessArr){ for (int data:row){ System.out.printf("%d\t", data); } System.out.println(); } // ==========================Convert a two-dimensional array to a sparse array============ // 1. Number of non-zero data obtained by traversing a two-dimensional array first int sum = 0; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if (chessArr[i][j] != 0){ sum++; } } } // 2. Create corresponding dilute sulfur array int sparseArr[][] = new int[sum + 1][3]; // Assigning values to sparse groups sparseArr[0][0] = 11; sparseArr[0][1] = 11; sparseArr[0][2] = sum; // Traverse a two-dimensional array and store non-zero values in sparseArr int count = 0; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if (chessArr[i][j] != 0){ count++; sparseArr[count][0] = i; sparseArr[count][1] = j; sparseArr[count][2] = chessArr[i][j]; } } } // Output Sparse Array Form System.out.println(); System.out.println("Gets the sparse array as~~~~"); for (int i = 0; i < sparseArr.length; i++) { System.out.printf("%d\t%d\t%d\n",sparseArr[i][0],sparseArr[i][1],sparseArr[i][2]); } System.out.println(); // =============================Restore sparse array--"to original two-dimensional array================================== //1. Read the first row of the dilute sulfur array first and create the original two-dimensional array based on the data of the first row, such as chessEr2=int["1] above //2. Read the data of the last few rows of the dilute sulfur array and assign it to the original two-dimensional array. int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]]; for (int i = 1; i < sparseArr.length; i++) { chessArr2[sparseArr[i][0]][sparseArr[i][1]]=sparseArr[i][2]; } //Output Restored Two-Dimensional Array System.out.println(); System.out.println("Restored 2-D Array"); for (int[] row:chessArr2){ for (int data:row){ System.out.printf("%d\t", data); } System.out.println(); } } }

31 October 2021, 16:34 | Views: 3347

Add new comment

For adding a comment, please log in
or create account

0 comments