0

I'm trying to input the length of array and then input the array elements. I was able to check the greatest value in case 1 but I make case 2 to re-sort the array from the smallest number to the biggest, but I wasn't able to do it.

Here's my code:

package peopleinfo;

import java.util.Scanner;

public class mainapp
{
  public static void main(String[] args)
  {
    Scanner sc1 = new Scanner(System.in);
    System.out.println("please input the length of the array:");
    arraybuilder(sc1.nextInt());
  }

  public static void arraybuilder(int i)
  {
    Scanner sc = new Scanner(System.in);
    int[] array = new int[i];
    for (int j = 0; j < array.length; j++)
    {
      int o = j;
      System.out.println("please input the" + ++o + " number");
      array[j] = sc.nextInt();
    }
    for (int j2 = 0; j2 < array.length; j2++)
      System.out.print(array[j2] + "\t");
    System.out.println("what do you want to do ?\n 1 check the biggest number\n2 sort the array from the smallest\nterminate the app");
    int dwc = sc.nextInt();
    switch (dwc)
    {
      case 1:
        int k = 0;
        int max = 0;
        for (int j = 0; j < array.length; j++)
        {
          if (array[j] > k)
          {
            max = j;
            k = array[j];
          }
        }
        System.out.println("the largest number is " + array[max]);
        break;

      case 2:
        int temp;
        boolean fixed = false;
        while (fixed = false)
        {
          fixed = true;
          for (int u = 0; u < array.length - 1; u++)
          {
            if (array[u] > array[u + 1])
            {
              temp = array[u + 1];
              array[u + 1] = array[u];
              array[u] = temp;
              fixed = false;
            }
          }
        }
        for (int j = 0; j < array.length; j++)
        {
          System.out.print(array[j] + "\t");
        }
        break;
      default:
        System.out.println("Error, please rerun the code:)");
        break;
    }
  }
}

I did not understand what am doing wrong?

2
  • 3
    P.N: should be while(!fixed){ rather than while(fixed=false){ . Commented Jul 1, 2015 at 5:30
  • Making the change in the above comment will not fix the problem as the sorting algorithm being used is incorrect. The while loop serves no purpose and should be removed. Commented Jul 1, 2015 at 5:51

5 Answers 5

1
 case 2:
                int temp;
                boolean fixed = false;
               while (fixed == false) {    // See the change here
                    fixed = true;
                    for (int u = 0; u < array.length - 1; u++) {
                        if (array[u] > array[u + 1]) {
                            temp = array[u + 1];
                            array[u + 1] = array[u];
                            array[u] = temp;
                            fixed = false;
                        }
                    }
                }
                for (int j = 0; j < array.length; j++) {
                    System.out.print(array[j] + "\t");
                }

                break;

In your case 2 the while() loop has fixed = false

Is should be fixed == false i.e. while(fixed==false).

For more info follow the link... Using the assignment operator instead of the equality operator

Sign up to request clarification or add additional context in comments.

Comments

0

while (fixed = false) fixed=false is an assignment, and it will result as a "false" because fixed is assigned as false, which makes the while loop never start.

Comments

0

You are assigning the value false to fixed instead of comparing.Change the code as below and check it should work.

 case 2:
            int temp;
            boolean fixed = false;
            while (fixed == false) { //check this line
                fixed = true;
                for (int u = 0; u < array.length - 1; u++) {
                    if (array[u] > array[u + 1]) {
                        temp = array[u + 1];
                        array[u + 1] = array[u];
                        array[u] = temp;
                        fixed = false;
                    }
                }
            }
            for (int j = 0; j < array.length; j++) {
                System.out.print(array[j] + "\t");
            }

            break;

Comments

0

Your code for the second case is incorrect. You appear to be trying to do a Bubble Sort, but you are missing a second loop. I verified that your current code does not sort the array. Try this code instead:

case 2:
    int temp;
    // Bubble sort the array
    for (int i=0; i < array.length; i++) {
        for (int j=1; j < (array.length - i); j++) {
            if (array[j-1] > array[j]) {
                temp = array[j-1];
                array[j-1] = array[j];
                array[j] = temp;
            }
        }
    }

    for (int j=0; j < array.length; ++j) {
        System.out.print(array[j] + "\t");
    }
    break;

I also removed the while loop logic because it didn't seem to serve any purpose.

1 Comment

you to made the same mistake... as he had made... just change it... and his code works fine...
0

You have checked for a boolean as while (fixed = false). Please note that you are assigning value there and to check boolean you need to use == like while (fixed == false) or while (!fixed).

Because, when you use while (fixed = false) it doesn't return a boolean value, instead the fixed is assigned as false. You can see that the condition would always be false and the loop wouldn't execute.

4 Comments

Even with this change the sorting code won't produce the desired result.
@TimBiegeleisen Correcting the algorithm as while (fixed == false) works and produces the desired result in sorting the array in ascending order. I have tested it as well. Please let me know if you have any scenario it doesn't work for.
So you are basically saying that an array of size N can always be sorted in O(N) time? This doesn't jive with what we know about sorting algorithms.
@TimBiegeleisen I think this question was not about a simple sorting algorithm. It's a single dimensional array in this problem and he tries a simple sorting. The reply was to solve this simple sorting which I think is resolved. The question wasn't even about which algorithm should I be using or shortest path or efficiency, which would have attracted a best approach :) Please add your solution for the efficiency purpose. The usage of fixed == false in while loop haven't been checked by me. I just informed about the code correction to make it work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.