Why am I getting 100% accuracy in my Java MLP program? - java

I am writing a code in java that uses an MLP algorithm on a digit recognition dataset. I have 2 excel files. One for training and one for testing and they both contain 65 columns and 2810 rows. Below is my code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Random;
public class Main {
static int hiddenNeurons = 64; // Number of hidden neurons
static int[][] WH = new int[hiddenNeurons][64]; // Weight between the input and the hidden neurons
static int[][] WO = new int[10][hiddenNeurons]; // Weight between the hidden neurons and the output
static double[] outputHidden = new double[hiddenNeurons];
static double[] outputNeuron = new double[10];
static int[] dataSample = new int[64];
static int[] mapTarget = new int[10];
static int[] errorOutput = new int[10];
static int[] errorHidden = new int[hiddenNeurons];
static void initializeWeights(){
Random randomNumber = new Random();
for (int i = 0; i < WH.length; i++) {
for (int j = 0; j < WH[i].length; j++) {
WH[i][j] = randomNumber.nextInt(10);
}
}
for (int i = 0; i < WO.length; i++) {
for (int j = 0; j < WO[i].length; j++) {
WO[i][j] = randomNumber.nextInt(10);
}
}
}
public static void feedForward(){
double sigmoid = 0;
double sumWH = 0;
for (int i = 0; i < hiddenNeurons; i++) {
for (int j = 0; j < dataSample.length; j++) {
sumWH += dataSample[j] * WH[i][j];
}
sigmoid = 1 / (1 + Math.exp(-sumWH));
outputHidden[i] = sigmoid;
}
for (int i = 0; i < outputNeuron.length; i++) {
double sumWO = 0;
for (int j = 0; j < dataSample.length; j++) {
sumWO += outputHidden[j] * WO[i][j];
}
outputNeuron[i] = sumWO >= 0 ? 1 : 0;
}
}
static boolean testError() {
for (int i = 0; i < outputNeuron.length; i++) {
if (mapTarget[i] != outputNeuron[i]) {
return false;
}
}
return true;
}
static void training(){
double learningRate = 0.1;
for (int i = 0; i < outputNeuron.length; i++) {
errorOutput[i] = (int) (mapTarget[i] - outputNeuron[i]);
}
for (int j = 0; j < hiddenNeurons; j++) {
double errorTemp = 0;
for (int i = 0; i < outputNeuron.length; i++) {
errorTemp += errorOutput[i] * WO[i][j]; //try [j][i]
}
errorHidden[j] = (int) (outputHidden[j] * (1-outputHidden[j]) * errorTemp);
}
for (int i = 0; i < outputNeuron.length; i++) {
for (int j = 0; j < hiddenNeurons; j++) {
WO[i][j] = (int) (WO[i][j] + learningRate * outputHidden[j] * errorOutput[i]);
}
}
for (int i = 0; i < hiddenNeurons; i++) {
for (int j = 0; j < dataSample.length; j++) {
WH[i][j] = (int) (WH[i][j] + learningRate * dataSample[j] * errorHidden[i]);
}
}
}
static void testing(){
double success = 0;
try(BufferedReader br = new BufferedReader(new FileReader("test.csv"))) {
String line;
int numOfRows = 0;
boolean errorTested = false;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
for (int i = 0; i < 64; i++) {
dataSample[i] = Integer.parseInt(values[i]);
}
int targetOutput = Integer.parseInt(values[values.length - 1]);
for (int i = 0; i < 10; i++) {
mapTarget[i] = 0;
}
numOfRows++;
mapTarget[targetOutput] = 1;
feedForward();
errorTested = testError();
if(!errorTested){
success++;
}
}
double accuracy = success / numOfRows;
System.out.println("testing dataset accuracy: " + accuracy * 100);
}
catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
double success = 0;
initializeWeights();
try (BufferedReader br = new BufferedReader(new FileReader("train.csv"))) {
String line;
int numOfRows = 0;
double accuracy = 0;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
for (int i = 0; i < 64; i++) {
dataSample[i] = Integer.parseInt(values[i]);
}
int targetOutput = Integer.parseInt(values[values.length - 1]);
for (int i = 0; i < 10; i++) {
mapTarget[i] = 0;
}
mapTarget[targetOutput] = 1;
feedForward();
if (testError()) {
training();
} else {
success++;
}
numOfRows++;
accuracy = success / numOfRows;
}
System.out.println("training dataset: " + accuracy*100);
testing();
}
catch (Exception e){
e.printStackTrace();
}
}
}
This code, when run, gives me this output:
training dataset: 100.0
testing dataset accuracy: 100.0
What is the issue that seems to show me 100% accuracy even though that isn't possible.

Related

Java, Class' s property is not updated when it is changed with method

App Link: Replit
I have a Neuron_ class
public class Neuron_ {
private double[] weights;
double learningRate;
public Neuron_ (int inputNeurons, double learningRate) {
this.weights = new double[inputNeurons];
this.learningRate = learningRate;
for (int i = 0; i < inputNeurons; i++) {
this.weights[i] = Math.random();
}
}
public double calculate(double[] inputs) {
double output = 0;
for(int i = 0; i < inputs.length; i++) {
output += inputs[i] * this.weights[i];
}
return output;
}
public void decreaseWeight(double[] inputs) {
for (int i = 0; i < this.weights.length; i++) {
this.weights[i] -= learningRate * inputs[i] ;
}
}
public void increaseWeight(double[] inputs) {
for (int i = 0; i < this.weights.length; i++) {
this.weights[i] += learningRate * inputs[i] ;
}
}
}
and I call increaseWeight and decreaseWeight methods from NeuralNetwork class
if(biggestIndex != correctIndex) {
System.out.println("Wrong output, changing weights");
for (int i = 0; i < this.neurons.length; i++) {
for (int j = 0; j < outputNeurons.length; j++) {
System.out.println("Changing weights for neuron " + i + " for output " + outputNeurons[j]);
if (j == correctIndex) {
this.neurons[i].increaseWeight(inputs);
} else if (j == biggestIndex) {
this.neurons[i].decreaseWeight(inputs);
}
}
}
}
neurons array is created like this
public NeuralNetwork_ (int inputNeurons, String[] outputNeurons, double learningRate) {
this.outputNeurons = outputNeurons;
this.neurons = new Neuron_[outputNeurons.length];
for(int i = 0; i<outputNeurons.length; i++) {
this.neurons[i] = new Neuron_(inputNeurons, learningRate);
}
}
When I call decreaseWeight and increaseWeight methods from another class, Logs show like they were changed but when I log weights at the beginning and at the end of the training, they were same every time.
weights array is not updating.
weights change, but finally the result is the same for maths ops.
Try modify you method with more log:
public void decreaseWeight(double[] inputs) {
double[] newWeights = new double[inputs.length];
for (int i = 0; i < this.weights.length; i++) {
newWeights[i] = this.weights[i] - learningRate;
}
this.weights = newWeights;
System.out.println("intermedial weights: " + Arrays.toString(this.weights));
}
public void increaseWeight(double[] inputs) {
double[] newWeights = new double[inputs.length];
for (int i = 0; i < this.weights.length; i++) {
newWeights[i] = this.weights[i] + learningRate;
}
this.weights = newWeights;
System.out.println("intermedial weights: " + Arrays.toString(this.weights));
}
and study the result.
For each neurons iterator you are increasing and then decreasing weights whith the same value, so finally neuron weights are the same

HackerEarth exercise running out of time

I solved the following problem from HackerEarth. All test cases are correct except the last one bacause it runs out of time. I tried optimizing my solution but I cannot optimize it better.
Here is my solution:
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
class TestClass {
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
Set<Integer> perfectNumbers = new HashSet<>();
for (int i = 0; i <= 20; i++) {
perfectNumbers.add(i * i * i);
}
for (int i = 1; i <= 44; i++) {
perfectNumbers.add(i * i);
}
int t = sc.nextInt();
for (int k = 0; k < t; k++) {
int db = 0;
int n = sc.nextInt();
int[] a = new int[1001];
int[] b = new int[1001];
int[] numbers = new int[n];
for (int j = 0; j < n; j++) {
int x = sc.nextInt();
numbers[j] = x;
for (Integer perfect : perfectNumbers) {
if (x == perfect - x) {
b[x]++;
} else if (perfect - x >= 0 && perfect - x <= 1000)
a[perfect - x]++;
}
}
for (int j = 0; j < n; j++) {
db += a[numbers[j]];
}
for (int j = 0; j <= 1000; j++) {
if (b[j] > 1) {
db += b[j] * (b[j] - 1);
}
}
System.out.println(db / 2);
}
}
}
Probably you should consider reading the input with some faster method because Scanner is really slow. You could for example wrap Scanner into a BufferedReader.
I modified the code. Now it passes all test cases:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
class TestClass {
public static void main(String args[]) throws Exception {
FastReader sc = new FastReader();
Set<Integer> perfectNumbers = new HashSet<>();
for (int i = 0; i <= 20; i++) {
perfectNumbers.add(i * i * i);
}
for (int i = 1; i <= 44; i++) {
perfectNumbers.add(i * i);
}
int t = sc.nextInt();
for (int k = 0; k < t; k++) {
int db = 0;
int n = sc.nextInt();
int[] a = new int[1001];
int[] b = new int[1001];
int[] numbers = new int[n];
for (int j = 0; j < n; j++) {
int x = sc.nextInt();
numbers[j] = x;
for (Integer perfect : perfectNumbers) {
if (x == perfect - x) {
b[x]++;
} else if (perfect - x >= 0 && perfect - x <= 1000)
a[perfect - x]++;
}
}
for (int j = 0; j < n; j++) {
db += a[numbers[j]];
}
for (int j = 0; j <= 1000; j++) {
if (b[j] > 1) {
db += b[j] * (b[j] - 1);
}
}
System.out.println(db / 2);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
Integer nr=null;
try {
nr = Integer.parseInt(next());
} catch (Exception e) {
//something went wrong
}
return nr;
}
}
}

Assigning A Random value 3D Array to an Array of class

I have a class with three fields:
public class CCTest {
public double f;
public double[][][] x;
public double counter;
}
I am trying to assign a random number to it. I have the method below for random data generation:
public static double[][][] getRandomX(int x, int y, int z) {
double[][][] result = new double[x][y][z];
Random r = new Random();
for (int i = 0; i < z; i++) {
for (int j = 0; j < y; j++) {
for (int k = 0; k < x; k++) {
result[k][j][i] = r.nextDouble();
}
}
}
// System.out.println(Arrays.deepToString(result));
return result;
}
As for the issue. I have for example an array with 5 CCTest-objects:
CCTest[] cls = new CCTest[5];
How can I assign a random number to each of the 5 CCTest-objects?
I tried this:
for (int i = 0; i < Size =5; i++) {
cls[i].x = new double[this.c][this.D][this.Size];
for (int j = 0; j < this.D; j++) {
cls[i].X= getRandomX(this.c, this.D, this.Size);
}
The result should have following structure:
X(:,:,1) =
0.8909 0.5472
0.9593 0.1386
X(:,:,2) =
0.1493 0.8407
0.2575 0.2543
But the code did not produce it. Could anyone guide me to a solution, please?
The problem is that you haven't made any CCTest-instances.
So after you make the CCTest[] cls = new CCTest[5]; the five CCTest-objects are null. You should create them if they don't exist yet:
CCTest[] cls = new CCTest[5];
for (int i = 0; i < (Size = 5); i++) {
// We create a new CCTest-instance if it doesn't exist yet:
if(cls[i] == null){
cls[i] = new CCTest();
}
cls[i].x = new double[this.c][this.D][this.Size];
for (int j = 0; j < this.D; j++) {
cls[i].x = getRandomX(this.c, this.D, this.Size);
}
}
Alternatively, you could create them first, and then do the for-loop to assign the random doubles:
CCTest[] cls = new CCTest[5];
for (int i = 0; i < cls.length; i++) {
cls[i] = new CCTest();
}
for (int i = 0; i < (Size = 5); i++) {
cls[i].x = new double[this.c][this.D][this.Size];
for (int j = 0; j < this.D; j++) {
cls[i].x = getRandomX(this.c, this.D, this.Size);
}
}

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Why do I get this exception? How to fix it?
public class NQueen_Pro {
// int[] Fitness;
/*
* int[] NQueens1; int[] NQueens2; int[] NQueens3; int[] NQueens4;
*/
int[][] NQueens;
int[][] CrossOver;
int[] Fitness;
int nFitSelected;
long startingTime ;
/*
* int[] CrossOver1; int[] CrossOver2; int[] CrossOver3; int[] CrossOver4;
*/
Random ran;
int length;
public NQueen_Pro(int n) {
Fitness = new int[n];
/*
* NQueens1 = new int[n]; NQueens2 = new int[n]; NQueens3 = new int[n];
* NQueens4 = new int[n];
*/
NQueens = new int[n][n];
CrossOver = new int[n][n];
nFitSelected = 40;
/*
* CrossOver1 = new int[n]; CrossOver2 = new int[n]; CrossOver3 = new
* int[n]; CrossOver4 = new int[n];
*/
length = n;
startingTime = System.currentTimeMillis();
ran = new Random();
while(true)
genetics();
}
public void fillValues() {
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++)
NQueens[i][j] = ran.nextInt(length);
}
}
private void genetics() {
fillValues();
for (int i = 0; i < length; i++) {
countFitness(i, NQueens[i]);
}
ArrangeFromFitness();
CrossOver();
Mutate();
IsDone();
}
private void IsDone() {
/*
* if (IsFit(CrossOver1)) print(CrossOver1); else if (IsFit(CrossOver2))
* print(CrossOver2); else if (IsFit(CrossOver3)) print(CrossOver3);
* else if (IsFit(CrossOver4)) print(CrossOver4); else{ System.gc();
* generics(); }
*/
int gotFit = 0;
for (int i = 0; i < nFitSelected; i++) {
if (IsFit(CrossOver[i])) {
print(CrossOver[i]);
gotFit = 1;
}
}
if (gotFit == 1) {
System.out.println("Total Time : \"" + ((System.currentTimeMillis()-startingTime)/(float)1000) + "\" Seconds.");
System.exit(1);
}
}
private void print(int[] Array) {
for (int i = 0; i < Array.length; i++) {
for (int j = 0; j < Array.length; j++) {
if (Array[i] == j)
System.out.print(" Q ");
else
System.out.print(" - ");
}
System.out.println();
}
}
private boolean IsFit(int[] crossOver) {
// Vertical Checking
for (int i = 0; i < crossOver.length; i++)
for (int j = i + 1; j < crossOver.length; j++)
if (crossOver[i] == crossOver[j])
return false;
// Diagonal Right Checking
for (int i = 0; i < crossOver.length; i++) {
int temp = 1;
for (int j = i + 1; j < crossOver.length; j++) {
if ((crossOver[i] + temp) == crossOver[j])
return false;
temp++;
}
}
// Diagonal Left Checking
/*
* for (int i = 0; i < crossOver.length; i++) { int temp = 1; for (int j
* = i; j < crossOver.length; j++) { if ((crossOver[i] - temp) ==
* crossOver[j]) return false; temp++; } }
*/
// Diagonal Left Checking
for (int i = 0; i < crossOver.length; i++) {
int temp = 1;
for (int j = i + 1; j < crossOver.length; j++) {
if (crossOver[i] - temp == crossOver[j])
return false;
temp++;
}
}
return true;
}
private void Mutate() {
for (int i = 0; i < nFitSelected; i++) {
int nMutations = ran.nextInt(length);
for (int j = 0; j < nMutations; j++)
CrossOver[i][ran.nextInt(length)] = ran.nextInt(length);
}
/*
* CrossOver1[mutateIndexBit] = ran.nextInt(length); mutateIndexBit =
* ran.nextInt(length); CrossOver2[mutateIndexBit] =
* ran.nextInt(length); mutateIndexBit = ran.nextInt(length);
* CrossOver3[mutateIndexBit] = ran.nextInt(length); mutateIndexBit =
* ran.nextInt(length); CrossOver4[mutateIndexBit] =
* ran.nextInt(length);
*/
}
private void CrossOver() {
int crossOvered = ran.nextInt(length);
for (int i = 0; i < nFitSelected; i += 2) {
for (int j = 0; j < crossOvered; j++) {
CrossOver[i][j] = NQueens[i][j];
CrossOver[i+1][j] = NQueens[i+1][j];
}
for (int j = crossOvered; j < length; j++) {
CrossOver[i+1][j] = NQueens[i][j];
CrossOver[i][j] = NQueens[i+1][j];
}
}
/*
* // FIRST GROUP
* for (int i = 0; i < halfLength; i++)
* CrossOver1[i] = NQueens1[i];
* for (int i = halfLength; i < NQueens1.length; i++)
* CrossOver1[i] = NQueens2[i];
*
* // SECOND GROUP for (int i = 0; i < halfLength; i++) CrossOver2[i] =
* NQueens2[i]; for (int i = halfLength; i < NQueens1.length; i++)
* CrossOver2[i] = NQueens1[i];
*
* // THIRD GROUP for (int i = 0; i < halfLength; i++) CrossOver3[i] =
* NQueens3[i]; for (int i = halfLength; i < NQueens1.length; i++)
* CrossOver3[i] = NQueens4[i];
*
* // FOURTH GROUP for (int i = 0; i < halfLength; i++) CrossOver4[i] =
* NQueens4[i]; for (int i = halfLength; i < NQueens1.length; i++)
* CrossOver4[i] = NQueens3[i];
*/
/*
* for (int i = 0; i < NQueens1.length; i++)
* System.out.print(CrossOver1[i]); System.out.println(); for (int i =
* 0; i < NQueens1.length; i++) System.out.print(CrossOver2[i]);
*/
}
private void ArrangeFromFitness() {
int[] index = new int[length];
for(int i=0;i<length;i++)
index[i] = i;
for (int i = 0; i < Fitness.length; i++) {
for (int j = 0; j < Fitness.length - 1; j++) {
if (Fitness[j] > Fitness[j + 1]) {
int temp = Fitness[j];
Fitness[j] = Fitness[j + 1];
Fitness[j + 1] = temp;
temp = index[j];
index[j] = index[j + 1];
index[j + 1] = temp;
}
}
}
int[][] tempQueens = NQueens;
/*NQueens[0] = tempQueens[index[0]];
NQueens[1] = tempQueens[index[1]];
NQueens[2] = tempQueens[index[0]];
NQueens[3] = tempQueens[index[2]];*/
int temp = 1;
for(int i=0;i<nFitSelected;i+=2){
NQueens[i] = tempQueens[index[0]];
NQueens[i+1] = tempQueens[index[temp]];
temp++;
}
}
/*private void printAll() {
for (int i = 0; i < NQueens1.length; i++)
System.out.print(NQueens1[i]);
System.out.println(" " + Fitness[0]);
for (int i = 0; i < NQueens1.length; i++)
System.out.print(NQueens2[i]);
System.out.println(" " + Fitness[1]);
for (int i = 0; i < NQueens1.length; i++)
System.out.print(NQueens3[i]);
System.out.println(" " + Fitness[2]);
for (int i = 0; i < NQueens1.length; i++)
System.out.print(NQueens4[i]);
System.out.println(" " + Fitness[3]);
}*/
private void countFitness(int nth, int[] Nqueen) {
for (int i = 0; i < Fitness.length; i++)
Fitness[i] = 0;
// Vertical Checking
for (int i = 0; i < Nqueen.length; i++)
for (int j = i + 1; j < Nqueen.length; j++)
if (Nqueen[i] == Nqueen[j])
Fitness[nth]++;
// Diagonal Right Checking
for (int i = 0; i < Nqueen.length; i++) {
int temp = 1;
for (int j = i + 1; j < Nqueen.length; j++) {
if ((Nqueen[i] + temp) == Nqueen[j])
Fitness[nth]++;
temp++;
}
}
// Diagonal Left Checking
for (int i = 0; i < Nqueen.length; i++) {
int temp = 1;
for (int j = i + 1; j < Nqueen.length; j++) {
if (Nqueen[i] - temp == Nqueen[j])
Fitness[nth]++;
temp++;
}
}
}
public static void main(String[] arg) {
new NQueen_Pro(8);
}
}
nFitSelected is 40. The size of NQueens is 8 by 8. You got your condition wrong in the for loop :
for(int i=0;i<nFitSelected;i+=2){
NQueens[i] = tempQueens[index[0]];
NQueens[i+1] = tempQueens[index[temp]];
temp++;
}
If you let i reach as high as 38, NQueens[i] would throw an exception when i reaches 8.
The definitions of the arrays :
public NQueen_Pro(int n) {
Fitness = new int[n];
NQueens = new int[n][n]; // 8 x 8
CrossOver = new int[n][n];
nFitSelected = 40;
...
public static void main(String[] arg)
{
new NQueen_Pro(8); // the value of n
}
Meta-notes: Your code needs better formatting - indentation and removal of commented-out-code.
anyway I like using command line to debug:
Follow what it saws, you need to import that Random class. Where is RAndom? A google search yields It is in Util. So add in:
import java.util.*;

multipath two-phase natural balanced merge

I have a problem. I need implement multipath two-phase natural balanced merge using java. I saw merger sort example here http://www.vogella.de/articles/JavaAlgorithmsMergesort/article.html
I understand how it works, but I do not know how implement two-phase merge. One function divide array for indices and the second is to arrange a merger. Also it should be implemented for array and files (instead array used files). Who knows how to do it ?
public class Mergerer {
private int countWay = 0;
private int countIndex = 0;
private Double[] base;
private Double[] sortBase;
private Double[] tempBase;
private Double[][] arrayWay;
private String[] array1;
private String[] array2;
private String[] array3;
private String[] array4;
private String[] array5;
private String[] array6;
List<String[]> listA = new ArrayList<String[]>();
private int countElements = 0;
public Double[] getSortBase() {
return sortBase;
}
public Double[] sort() {
divide();
merger(base);
return sortBase;
}
public int getCountWay() {
return countWay;
}
public Mergerer(Double[] base, int countWay) {
this.countWay = countWay;
this.arrayWay = new Double[countWay][base.length + 1];
tempBase = base;
countElements = base.length;
array1 = new String[base.length * 2];
array2 = new String[base.length * 2];
array3 = new String[base.length * 2];
array4 = new String[base.length * 2];
array5 = new String[base.length * 2];
array6 = new String[base.length * 2];
listA.add(array1);
listA.add(array2);
listA.add(array3);
listA.add(array4);
listA.add(array5);
listA.add(array6);
}
public int divide() {
int[][] posWay = new int[countWay][1];
int beginIndexPos = 0;
int numberWay = 0;
int pos = 0;
for (int i = 1; i < tempBase.length; i++) {
if (tempBase[i] < tempBase[i - 1]) {
for (int j = beginIndexPos; j < i; j++) {
listA.get(numberWay)[posWay[numberWay][0]] = Double.toString(tempBase[j]);
arrayWay[numberWay][posWay[numberWay][0]] = tempBase[j];
posWay[numberWay][0]++;
countIndex++;
}
listA.get(numberWay)[posWay[numberWay][0]] = "'";
posWay[numberWay][0]++;
beginIndexPos = i;
if (numberWay == countWay - 1)
numberWay = 0;
else
numberWay++;
pos = 0;
}
}
for (int i = beginIndexPos; i < tempBase.length; i++) {
listA.get(numberWay)[posWay[numberWay][0]] = Double.toString(tempBase[i]);
arrayWay[numberWay][posWay[numberWay][0]] = tempBase[i];
posWay[numberWay][0]++;
countIndex++;
}
listA.get(numberWay)[posWay[numberWay][0]] = "'";
return countIndex;
}
public void printIndex() {
for (int i = 0; i < countWay; i++) {
for (int j = 0; j < countElements; j++) {
if (arrayWay[i][j] != null)
System.out.print(arrayWay[i][j]);
}
System.out.println();
}
}
public void printIndexList() {
for (int i = 0; i < countWay; i++) {
for (int j = 0; j < countElements; j++) {
if (listA.get(i)[j] != null)
System.out.print(listA.get(i)[j]);
}
System.out.println();
}
}
public void merger(Double[] base) {
List<String[]> listB = new ArrayList<String[]>();
for (int i = 0; i < countWay; i++) {
listB.add(new String[base.length * 2]);
}
int numberWay = 0;
int[][] posWay = new int[countWay][1];
double lastAdded = 0;
int countSeries = 0;
int[] mem = new int[countWay];
for (int i = 0; i < countWay; i++) {
mem[i] = 0;
}
while (true) {
double tmp = 999999999;
int way = -1;
for (int i = 0; i < countWay; i++) {
if (listA.get(i)[mem[i]] != null) {
String str = listA.get(i)[mem[i]];
if (str.equals("'") && listA.get(i)[mem[i] + 1] != null) {
mem[i]++;
str = listA.get(i)[mem[i]];
}
if (str != null && !str.equals("'")) {
double step = Double.parseDouble(str);
if (tmp >= step) {
tmp = Double.parseDouble(str);
way = i;
}
}
}
}
if (lastAdded > tmp || way == -1) {
listB.get(numberWay)[posWay[numberWay][0]] = "'";
countSeries++;
if (numberWay == countWay - 1) {
numberWay = 0;
} else
numberWay++;
}
if (way == -1) {
break;
}
mem[way]++;
lastAdded = tmp;
listB.get(numberWay)[posWay[numberWay][0]] = Double.toString(tmp);
posWay[numberWay][0]++;
}
listA = listB;
if (countSeries > 1) {
merger(base);
} else {
sortBase = new Double[countElements];
for (int i = 0; i < countWay; i++) {
for (int j = 0; j < countElements; j++) {
if (listA.get(i)[j] != null)
sortBase[j] = Double.parseDouble(listA.get(i)[j]);
}
}
return;
}
}
Double[] array = new Double[10];
for (int i=10; i>0; i--) {
array[10-i] = i;
}
Mergerer merg = new Mergerer(array, 4);
merg.divide();
merg.merger(array);

Categories

Resources