Update repo

This commit is contained in:
Chloe Fontenot 🏳️‍⚧️ 2023-01-22 18:12:56 -06:00
parent 0495faaa19
commit a42477e983

View File

@ -0,0 +1,149 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.calebfontenot.mp1_calebfontenot;
/**
*
* @author caleb
*/
public class Lab3DArraysSort_CalebFontenot {
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("");
}
}
/**
* 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)//normal
{
for (int x = 0; x < ar.length; ++x) {
for (int y = 0; y < ar[x].length; ++y) {
System.out.print(ar[x][y] + "");
}
System.out.println();
}
}
public static String returnRowMajorOrder(char[] ar)//normal
{
String returnString = "";
for (int x = 0; x < ar.length; ++x) {
returnString += ar[x];
}
return returnString;
}
/**
* Sorts the methods in ascending order using Selection Sort
*
* @param names the names to be sorted
*/
public static void sortNames(char[][][] names) {
for (int array = 0; array < names.length; ++array) {
System.out.println("times looped: " + array);
for (int i = 0; i < names[array].length - 1; ++i) {
for (int j = i + 1; j < names[array].length; ++j) {
char compChar1 = names[array][i][0], compChar2 = names[array][j][0];
// Reoder entire row
for (int rowIterate = 1; rowIterate < maxNumberOfColumnsInJagged2dArray(names[array]); ++rowIterate) {
if (Character.toLowerCase(compChar1) == Character.toLowerCase(compChar2)) {
try {
compChar1 = names[array][i][rowIterate];
compChar2 = names[array][j][rowIterate];
} catch (Exception ex) {
// If it's failed, the index has gone out of range.
// Check the length of the arrays and swap the larger one with the smaller one.
if (names[array][i].length > names[array][j].length) {
System.out.println(names[array][i].length + " " + names[array][j].length);
System.out.println("Swapping " + returnRowMajorOrder(names[array][i]) + " with " + returnRowMajorOrder(names[array][j]));
char[] temp = names[array][i];
names[array][i] = names[array][j];
names[array][j] = temp;
}
break;
}
}
if (Character.toLowerCase(compChar1) > Character.toLowerCase(compChar2)) {
System.out.println("Swapping " + returnRowMajorOrder(names[array][i]) + " with " + returnRowMajorOrder(names[array][j]));
char[] temp = names[array][i];
names[array][i] = names[array][j];
names[array][j] = temp;
}
}
}
}
}
}
public static void main(String[] args) {
/*
char[][] names = {
{'j', 'o', 'h', 'n'},
{'a', 'n'},
{'b', 'y', 'r', 'o', 'n'},};
*/
///*
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'}
},
{
{'L', 'i', 's', 's', 'e', 't'},
{'E', 't', 'h', 'a', 'n'},
{'C', 'a', 'l', 'e', 'b'},
{'L', 'u', 'l', 'y'},
{'C', 'h', 'a', 'n', 'c', 'e'}
}
};
//*/
//printColumnMajorOrder(names);
printRowMajorOrder(names);
System.out.println();
sortNames(names);
System.out.println();
printRowMajorOrder(names);
//printColumnMajorOrder(names);
}
}