Sort method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • miss time
    New Member
    • Jun 2007
    • 16

    #1

    Sort method

    hi ,
    i did Sort class and i did many method for this class such as swap ,min and sortArr. I have difficulty in solve a this static method which is; a method that returns true if the array arr is sorted, otherwise it returns false.
    I start like that...
    1)
    public static boolean isSorted(int[] arr, int i){
    for(int i=0;i< arr.length-1;i++){
    if ( arr [i]>arr [i++]); return false;

    }
    } it is not compile and also i feel that my answer is wrong ..

    2)and if I want to reverse the number for example I have 1 2 3 4 and the result should be 4 3 2 1

    ,so can you help me to understand this question and explain to me how to write these code
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Let's keep this one question at a time, please. We'll focus on your first question:

    Originally posted by miss time
    I have difficulty in solve a this static method which is; a method that returns true if the array arr is sorted, otherwise it returns false.
    I start like that...
    [CODE=java]public static boolean isSorted(int[] arr, int i){
    for(int i=0;i< arr.length-1;i++){
    if ( arr [i]>arr [i++]);
    return false;

    }
    }[/CODE]it is not compile and also i feel that my answer is wrong ..
    OK, the function header looks good...except you are passing the integer i as an argument. Why? Especially when, in your for...loop, you create another integer i as part of the initialization process. The integer in the function header is unnecessary.

    Now, your for loop looks good. It's running from i = 0 to i = array.length - 2, because you are going to be comparing an element with the next adjacent element, and you can't access arr[arr.length].

    The if statement condition is wrong. i++ will take place after the > operator is checked, so you are comparing the same element to itself, then incrementing i. You also do not want to increment i inside the for loop, as your loop itself is already doing that. Instead, use i + 1 to make sure you get the next element, but don't change i. You also have a semicolon after the if statement, so the return false; statement is never reached.

    Finally, you don't allow for the fact that the array may be sorted (i.e. where's the return true?).

    Comment

    • miss time
      New Member
      • Jun 2007
      • 16

      #3
      Originally posted by Ganon11
      Let's keep this one question at a time, please. We'll focus on your first question:



      OK, the function header looks good...except you are passing the integer i as an argument. Why? Especially when, in your for...loop, you create another integer i as part of the initialization process. The integer in the function header is unnecessary.

      Now, your for loop looks good. It's running from i = 0 to i = array.length - 2, because you are going to be comparing an element with the next adjacent element, and you can't access arr[arr.length].

      The if statement condition is wrong. i++ will take place after the > operator is checked, so you are comparing the same element to itself, then incrementing i. You also do not want to increment i inside the for loop, as your loop itself is already doing that. Instead, use i + 1 to make sure you get the next element, but don't change i. You also have a semicolon after the if statement, so the return false; statement is never reached.

      Finally, you don't allow for the fact that the array may be sorted (i.e. where's the return true?).

      Thank you very much i understand what you mean and I solve it as follow:
      [CODE=java]public static boolean isSorted(int[] arr){
      for(int i=0;i<arr.lengt h - 1;i++){
      if( arr[i] > arr[i+1] ) return false;
      }
      return true;
      } [/CODE]
      and when I compile, it work

      Also i solve the second as follow:



      [CODE=java]public static void reverse1(int[] arr){
      int from = 0;
      int to = arr.length -1;
      while( from < to ){
      swap(arr, from, to);
      from++;
      to--;
      }
      }[/CODE]
      and when i test these number 1,2,3,4,5 I get 5,4,3,2,1
      thank you again for your explain..
      Last edited by Ganon11; Nov 26 '07, 02:13 PM. Reason: Please use the [CODE] tags provided.

      Comment

      Working...