Reputation: 325
I'm trying to use this method to sort an integer array in ascending order. But my for loop runs through it only once.
public void sortArray()
{
boolean sorted = false;
while(sorted == false)
{
int temp;
for(int i = 0; i < inArray.length - 1; i++)
{
if(inArray[i] > inArray[i + 1])
{
temp = inArray[i];
inArray[i] = inArray[i + 1];
anArray[i + 1] = temp;
}
}
sorted = true;
}
}
I know it has to do with how I'm handling that boolean flag, but I'm not sure how to go about fixing it. Any suggestions would be appreciated. Thanks in advance.
Upvotes: 0
Views: 1136
Reputation: 3
public class Main {
public static void main(String[] args) {
/** By Boris Elkin 21.09.2018 в 22.59 MSK You can input any digits, sorting was made without collections on purpose.
**/
int[] a1=new int[]{1,245623,3,3,3,3454,6,8123,234,123123,797897};
int[] a2=new int[]{234234, 33,4234,5,646456,9,78};
int[] a3;
a3= collide(a1, a2);
a3=sort(a3);
checkArray(a3);
}
public static int[] collide(int[]a, int[]b){
int breakpoint=0;
int size=a.length+b.length;
int[]c=new int[size];
for(int i=0;i<a.length;i++){
c[i]=a[i];
breakpoint=i;
}
for(int i=breakpoint+1,j=0;j<b.length;i++, j++){
c[i]=b[j];
}
return c;
}
public static int[] sort(int a[]){
boolean engine=true;
while(engine) {
for(int i=0;i<a.length;i++){
int temp, temp2;
if ((i + 1 < a.length) && (a[i] > a[i + 1])) {
temp = a[i];
temp2 = a[i + 1];
a[i + 1] = temp;
a[i] = temp2;
}
}
if(checkThreadLogistic(a)){
engine=false;
}
}
return a;
}
private static boolean checkThreadLogistic(int[] a) {
return checkCertainElement(a);
}
private static boolean checkCertainElement(int[] a) {
for(int i=0;i<a.length;i++){
if(i>1){
for(int j=a.length;j>i;j--){
if(j<a.length) if(a[i]>a[j])return false;
}
}
}
return true;
}
public static void checkArray(int[]array){
for (int anArray : array) {
System.out.println(anArray + "");
}
}
}
Upvotes: 0
Reputation:
You currently are setting your sorted to true allways at the end of the loop. While of course it should only be true if actually no reshuffling took place.
One way to archieve this would be to set sorted to true at the start of your while loop, and set it to false when you detect that the array is not yet sorted and you do the switching of elements:
public void sortArray()
{
boolean sorted = false;
while(!sorted)
{
sorted = true;
int temp;
for(int i = 0; i < inArray.length - 1; i++)
{
if(inArray[i] > inArray[i + 1])
{
sorted = false; // array is not yet sorted
temp = inArray[i];
inArray[i] = inArray[i + 1];
anArray[i + 1] = temp;
}
}
}
}
Upvotes: 3
Reputation: 1074335
There are multiple issues here:
while (sorted = false)
sets sorted
to false
and then tests the resulting value false
, meaning that you never enter the loop body at all (not once as per your question).
If you fix that, your code will only run the while
loop body once (thus leaving the array not sorted yet), because you have sorted = true
as an unconditional statement at the end of the loop body.
You need to have a flag that assumes the array is sorted, and then is cleared if you find evidence it wasn't, something like:
public void sortArray()
{
boolean sorted;
do
{
sorted = true; // Assume it's sorted
int temp;
for(int i = 0; i < inArray.length - 1; i++)
{
if(inArray[i] > inArray[i + 1])
{
temp = inArray[i];
inArray[i] = inArray[i + 1];
anArray[i + 1] = temp;
sorted = false; // We changed something, so assume we need to do another pass
}
}
}
while (!sorted);
}
Side note: This is just a style thing, but it's generally best to scope variables as narrowly as possible. There's no need for temp
to be outside the for
loop or even outside the if
block, move it inside the if
block
public void sortArray()
{
boolean sorted;
do
{
sorted = true; // Assume it's sorted
for(int i = 0; i < inArray.length - 1; i++)
{
if(inArray[i] > inArray[i + 1])
{
int temp = inArray[i];
inArray[i] = inArray[i + 1];
anArray[i + 1] = temp;
sorted = false; // We changed something, so assume we need to do another pass
}
}
}
while (!sorted);
}
Upvotes: 3