Mason Help
This commit is contained in:
14
ASDV-Help/Mason/pom.xml
Normal file
14
ASDV-Help/Mason/pom.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.calebfontenot</groupId>
|
||||
<artifactId>Mason</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<exec.mainClass>com.calebfontenot.mason.Mason</exec.mainClass>
|
||||
</properties>
|
||||
</project>
|
211
ASDV-Help/Mason/src/main/java/com/calebfontenot/mason/Mason.java
Normal file
211
ASDV-Help/Mason/src/main/java/com/calebfontenot/mason/Mason.java
Normal file
@@ -0,0 +1,211 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
|
||||
package com.calebfontenot.mason;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class Mason {
|
||||
|
||||
public static void sortNames(char[][] names)
|
||||
{
|
||||
|
||||
for (int i = 0; i < names.length - 1; ++i)
|
||||
{
|
||||
|
||||
for (int j = i + 1; j < names.length; ++j)
|
||||
{
|
||||
//>compare character by character names[i] to names[j]
|
||||
for (int index1 = 0, index2 = 0;//index1 to traverse names[i]
|
||||
//index2 to traverse names[j]
|
||||
index1 < names[i].length
|
||||
&& index2 < names[j].length;
|
||||
++index1, ++index2)
|
||||
{
|
||||
|
||||
//>>SWAP name[i] with names[j]
|
||||
//if names[i] > names[j] OR the name at i has more chars after the tie
|
||||
// ( i.e. if "bbbc" at names[i] and "bbb" at names[j] at j then we swap them )
|
||||
if (names[i][index1] > names[j][index2]
|
||||
|| (index2 == names[j].length - 1 && index1 < names[i].length - 1))
|
||||
{
|
||||
//>> copy to tempI the names[i]
|
||||
char[] temp = names[i];
|
||||
names[i] = names[j];
|
||||
names[j] = temp;
|
||||
break;///you must break , no need to continue comparing characters at index1, index2
|
||||
}
|
||||
//>>if the characters are equal, continue
|
||||
if (names[i][index1] == names[j][index2])
|
||||
{
|
||||
continue;//do nothing, but continue
|
||||
} else //name at i is smaller than name
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void sortNames(char[][][] tables)
|
||||
{
|
||||
for (int numTable = 0; numTable < tables.length; ++numTable)
|
||||
{
|
||||
for (int i = 0; i < tables[numTable].length - 1; ++i)
|
||||
{
|
||||
|
||||
for (int j = i + 1; j < tables[numTable].length; ++j)
|
||||
{
|
||||
//>compare character by character names[i] to names[j]
|
||||
for (int index1 = 0, index2 = 0;//index1 to traverse names[i]
|
||||
//index2 to traverse names[j]
|
||||
index1 < tables[i][i].length
|
||||
&& index2 < tables[j][i].length;
|
||||
++index1, ++index2)
|
||||
{
|
||||
|
||||
//>>SWAP name[i] with names[j]
|
||||
//if names[i] > names[j] OR the name at i has more chars after the tie
|
||||
// ( i.e. if "bbbc" at names[i] and "bbb" at names[j] at j then we swap them )
|
||||
if (tables[numTable][i][index1] > tables[numTable][j][index2]
|
||||
|| (index2 == tables[numTable][j].length - 1 && index1 < tables[numTable][i].length - 1))
|
||||
{
|
||||
//>> copy to tempI the names[i]
|
||||
char[][] temp = tables[i];
|
||||
tables[i] = tables[j];
|
||||
tables[j] = temp;
|
||||
break;///you must break , no need to continue comparing characters at index1, index2
|
||||
}
|
||||
//>>if the characters are equal, continue
|
||||
if (tables[i][index1] == tables[j][index2])
|
||||
{
|
||||
continue;//do nothing, but continue
|
||||
} else //name at i is smaller than name
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints row by row
|
||||
*
|
||||
* @param ar
|
||||
*/
|
||||
public static void printRowMajorOrder(char[][] ar)
|
||||
{
|
||||
for (int i = 0; i < ar.length; ++i)
|
||||
{
|
||||
for (int j = 0; j < ar[i].length; ++j)
|
||||
{
|
||||
System.out.print(ar[i][j]);
|
||||
}
|
||||
System.out.print(" ");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static void printRowMajorOrder(char[][][] tables)
|
||||
{
|
||||
|
||||
for (int numTable = 0; numTable < tables.length; ++numTable)
|
||||
{
|
||||
for (int i = 0; i < tables[numTable].length; ++i)
|
||||
{
|
||||
for (int j = 0; j < tables[numTable][i].length; ++j)
|
||||
{
|
||||
System.out.print(tables[numTable][i][j]);
|
||||
|
||||
}
|
||||
System.out.print(" ");
|
||||
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
char[][] names =
|
||||
{
|
||||
{
|
||||
'j', 'o', 'h', 'n'
|
||||
},
|
||||
{
|
||||
'a', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'y', 'r', 'o', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'y', 'r', 'o', 'n', 'i'
|
||||
},
|
||||
{
|
||||
'a', 'a', 'o', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b', 'b'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b', 'c'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b'
|
||||
}
|
||||
};
|
||||
|
||||
char[][][] tables =
|
||||
{
|
||||
{
|
||||
{
|
||||
'j', 'o', 'h', 'n'
|
||||
},
|
||||
{
|
||||
'a', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'y', 'r', 'o', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'y', 'r', 'o', 'n', 'i'
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
'a', 'a', 'o', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b', 'b'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b', 'c'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b'
|
||||
}
|
||||
}
|
||||
};
|
||||
// System.out.println("ORIGINAL LIST");
|
||||
// printRowMajorOrder(names);
|
||||
//
|
||||
// System.out.println("\n\nSORTED LIST");
|
||||
// sortNames(names);
|
||||
// printRowMajorOrder(names);
|
||||
|
||||
System.out.println("ORIGINAL LIST");
|
||||
printRowMajorOrder(tables);
|
||||
System.out.println("\n\nSORTED LIST");
|
||||
sortNames(tables);
|
||||
printRowMajorOrder(tables);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.calebfontenot.mason;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class Mason_2 {
|
||||
|
||||
public static int maxNumberOfColumnsInJagged2dArray(char[][] ar) {
|
||||
int maxNumberOfColumns = 0;
|
||||
for (int row = 0; row < ar.length; ++row) {
|
||||
if (ar[row].length > maxNumberOfColumns) {
|
||||
maxNumberOfColumns = ar[row].length;
|
||||
}
|
||||
}
|
||||
|
||||
return maxNumberOfColumns;
|
||||
}
|
||||
|
||||
public static void printColumnMajorOrder(char[][] ar) {
|
||||
int row = 0;
|
||||
int column = 0;
|
||||
int max = maxNumberOfColumnsInJagged2dArray(ar);
|
||||
for (column = 0; column < max; ++column) {
|
||||
|
||||
for (row = 0; row < ar.length; ++row) {
|
||||
if (column < ar[row].length) {
|
||||
System.out.print(ar[row][column] + " ");
|
||||
}
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
|
||||
public static void sortNames(char[][][] names) {
|
||||
for (int numTable = 0; numTable < names.length; ++numTable) {
|
||||
for (int i = 0; i < names[numTable].length - 1; ++i) {
|
||||
|
||||
for (int j = i + 1; j < names[numTable].length; ++j) {
|
||||
//>compare character by character names[i] to names[j]
|
||||
for (int index1 = 0, index2 = 0;//index1 to traverse names[i]
|
||||
//index2 to traverse names[j]
|
||||
index1 < names[numTable][i].length
|
||||
&& index2 < names[numTable][j].length;
|
||||
++index1, ++index2) {
|
||||
|
||||
//>>SWAP name[i] with names[j]
|
||||
//if names[i] > names[j] OR the name at i has more chars after the tie
|
||||
// ( i.e. if "bbbc" at names[i] and "bbb" at names[j] at j then we swap them )
|
||||
if (names[numTable][i][index1] > names[numTable][j][index2]
|
||||
|| (index2 == names[numTable][j].length - 1 && index1 < names[numTable][i].length - 1)) {
|
||||
//>> copy to tempI the names[i]
|
||||
char[] temp = names[numTable][i];
|
||||
names[numTable][i] = names[numTable][j];
|
||||
names[numTable][j] = temp;
|
||||
break;///you must break , no need to continue comparing characters at index1, index2
|
||||
}
|
||||
//>>if the characters are equal, continue
|
||||
if (names[numTable][i][index1] == names[numTable][j][index2]) {
|
||||
continue;//do nothing, but continue
|
||||
} else //name at i is smaller than name
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints row by row
|
||||
*
|
||||
* @param ar
|
||||
*/
|
||||
public static void printRowMajorOrder(char[][][] array) {
|
||||
for (char[][] i : array) {
|
||||
System.out.println("---------------------------------");
|
||||
printRowMajorOrder(i);
|
||||
}
|
||||
}
|
||||
|
||||
public static void printRowMajorOrder(char[][] ar) {
|
||||
for (int i = 0; i < ar.length; ++i) {
|
||||
for (int j = 0; j < ar[i].length; ++j) {
|
||||
System.out.print(ar[i][j]);
|
||||
}
|
||||
System.out.print(" ");
|
||||
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
char[][] names
|
||||
= {
|
||||
{'j', 'o', 'h', 'n'}, {'a', 'n'}, {'b', 'y', 'r', 'o', 'n'},
|
||||
{'b', 'y', 'r', 'o', 'n', 'i'}, {'a', 'a', 'o', 'n'},
|
||||
{'b', 'b', 'b', 'b'}, {'b', 'b', 'b', 'c'}, {'b', 'b', 'b'}
|
||||
};
|
||||
char[][][] tables
|
||||
= {
|
||||
{
|
||||
{
|
||||
'j', 'o', 'h', 'n'
|
||||
},
|
||||
{
|
||||
'a', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'y', 'r', 'o', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'y', 'r', 'o', 'n', 'i'
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
'a', 'a', 'o', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b', 'b'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b', 'c'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b'
|
||||
}
|
||||
}
|
||||
};
|
||||
System.out.println("ORIGINAL LIST");
|
||||
printRowMajorOrder(tables);
|
||||
|
||||
System.out.println("\n\nSORTED LIST");
|
||||
sortNames(tables);
|
||||
printRowMajorOrder(tables);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user