开发者

unable to implement method correctly

I wrote this program to arrange the numbers in ascending order . But it is not giving the desired output.I have analysed the program properly and am aware that i am unable to implement sort function (below) properly . Please suggest what should be done in order that this program outputs correctly:

I have given sample outputs below the code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class sorter
{

  static int biggest;
  static int arr[];
  static int sortedArray[];
  static int count;

  public static void main(String args[]) throws IOException
  {
    System.out.print("How many numbers you want to enter : ");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String x = br.readLine();
    try
    {
      count = Integer.parseInt(x);
    }
    catch (Exception exc)
    {
      System.out.println(exc);
    }
    arr = new int[count];
    for (int i = 0; i < arr.length; i++)
    {
      System.out.print("Enter the " + (i + 1) + " number : ");
      BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
      try
      {
        arr[i] = Integer.parseInt(br2.readLine());
      }
      catch (Exception exc)
      {
        System.out.println(exc);
      }
    }

    System.out.println("We'll sort the numbers now !");

    sortedArray = new int[count];


    biggest = getBiggest(arr[0]);  // get The biggest number

    for (int i = 0; i < arr.length; i++)
    {
      sortedArray[i] = sort(arr[0]);
    }

    for (int i = 0; i < sortedArray.length; i++)
    {
      System.out.println("SortedArray is : " + sortedArray[i]);
    }
  }

  public static int getBiggest(int big)
  {
    for (int i = 1; i < arr.length; i++)
    {
      if (big < arr[i])
      {
        big = arr[i];
      }
    }
    return big;
  }

  public static int sort(int small)
  {
    for (int i = 1; i < arr.length; i++)
    {
      if (small > arr[i])
      {
        small = arr[i];
        System.out.println("BIGGEST-->" + biggest);
        arr[i] =
            ++biggest;   // i replace the smallest number with the biggest number after storing the smallest number in small
        biggest++;
      }
    }
    return small;
  }
}

In this i don't get the desired output . I am unable to perform this task . The biggest problem in this program is the working of function sort . How can i improve it's working ? so that it outputs me the desired result

Here are some sample outputs from the program :

How many numbers you want to enter : 4
Enter the 1 number : 6
Enter the 2 number : 7
Enter the 3 number : 8
Enter the 4 number : 1
We'll s开发者_如何转开发ort the numbers now !
BIGGEST-->8
SortedArray is : 1
SortedArray is : 6
SortedArray is : 6
SortedArray is : 6




How many numbers you want to enter : 6
Enter the 1 number : 6
Enter the 2 number : 8
Enter the 3 number : 55
Enter the 4 number : 99
Enter the 5 number : 44
Enter the 6 number : 557
We'll sort the numbers now !
SortedArray is : 6
SortedArray is : 6
SortedArray is : 6
SortedArray is : 6
SortedArray is : 6
SortedArray is : 6   


How about using Arrays.sort()? It would make your code a lot shorter:

//[...]

System.out.println("We'll sort the numbers now !");
Arrays.sort(arr);
for(int i : arr) {
    System.out.println("SortedArray is : " + i);
}


The problem in your code is in the line

sortedArray[i] = sort(arr[0]);

You are sending always the same element and since the sort returns that one or a bigger element of the array.

Maybe you should get some ideas first. Quicksort is one of the most popular sorting algorithms.


Why are you using arrays? Just use a Collection, like List:

List<Integer> list = new ArrayList<Integer>();
... for all lines:
list.add(Integer.parseInt(br2.readLine()));
...
Collections.sort(list);

And you're done. That's all there is to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜