I haven’t touched Java in 3 years. Most of the time was sorting out syntax.
I don’t know how to make the fancy videos. Also it isn’t meant to be fast.
Nerd Shit hides in here
public class collection {
//Fields
int contents[] = new int[25];
int steps2Sort = 0; //The number of times the order of values in contents[] gets changed.
//Constructors
public collection()
{
for(int i=0; i < contents.length; i++)
{
int addedContent = (int) (Math.random()*1001)+1;
if(addedContent == 0)
{
addedContent = nonZero();
}
addedContent = Math.abs(addedContent);
contents[i] = addedContent;
if(contents.length-1 == 2)
{
contents[i+1] = addedContent;
break;
}
};
};
public int sortContents(int lowest)
{
int num1 = 0;
int num2 = 0;
int steps = steps2Sort;
int numLowest = lowest;
for(int i=0; i < contents.length; i++)
{
if(i==0)
{
num1 = contents[i];
if(steps == 0)
{
numLowest = num1;
}
continue;
}
num2 = contents[i];
if(num1 > num2)
{
contents[i] = num1;
contents[i-1] = num2;
if(num2<numLowest)
{
numLowest = num2;
}
steps++;
continue;
}
num1 = num2;
}
steps2Sort = steps;
return numLowest;
};
//For every value in contents, checks if the previous value(num1) is less than the current value(num2)
//Returns output[lowest, sorted]
//sorted: 1 is true, sorted: 0 is false.
public int[] checkSort(int lowest)
{
int num1 = 0;
int num2 = 0;
int[] output = {lowest, 0};
for(int i=0; i < contents.length; i++)
{
//If first value set it as num1
if(i == 0)
{
if(contents[i] != output[0])
{
output[1] = 0;
return output;
}
num1 = contents[i];
continue;
}
num2 = contents[i];
if(num2 >= num1)
{
num1 = num2;
continue;
}
else
{
output[1] = 0;
return output;
}
};
output[1] = 1;
return output;
};
private int nonZero()
{
int non0Num;
do
{
non0Num = (int) (Math.random()*1001)+1;
non0Num = Math.abs(non0Num);
}while(non0Num == 0);
return non0Num;
}
public static void main(String[] args)
{
collection c1 = new collection();
int[] checkOutput = c1.checkSort(1005);
while(checkOutput[1] == 0)
{
int sortIterate = c1.sortContents(checkOutput[0]);
checkOutput = c1.checkSort(sortIterate);
}
for(int i=0; i < c1.contents.length; i++)
{
System.out.println(c1.contents[i]);
}
System.out.println("Steps: "+ c1.steps2Sort);
}
};```