Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the method isSorted I cant get the array to sort properly. Could someone take

ID: 3860027 • Letter: I

Question

In the method isSorted I cant get the array to sort properly. Could someone take a look and see what i did wrong. Thank you!

I S S O R T E D gives the array and i try to create the method in isSorted using recursion, because no loops are allowed, but it says the array is sorted but it wont sort.

When i run the code it sorts as array: 41 7 8 12 20 21 22 87 37 55 60 65 74 83 84 is SORTED

import java.io.*;

public class Recursion
{
   public static void main(String[] args)
   {

       // T R I S T A R S
       int rows = 5;
       System.out.format("A tringle with %d rows contains %d stars ", rows, triStars(rows));

       // S U M D I G I T S
       int number = 12345;
       System.out.format("The sum of the digits in the number %d = %d ", number, sumDigits(number));

       // C O U N T 7 S
       number = 713274772;
       System.out.format("There are %d occurances of the digit 7 in the number %d ", count7s(number), number);

       // C O U N T 8 S
       number = 82338828;
       System.out.format("There are %d occurances of the digit 8 in the number %d ", count8s(number), number);

       // P O W E R N
       int base = 2, exponent = 8;
       System.out.format("%d to the power %d = %d ", base, exponent, powerN(base, exponent));

************************************************************************

       // I S S O R T E D
       // perturb values as needed to test on an unsorted array
       int[] array = { 7, 8, 12, 20, 21, 22, 37, 41, 55, 60, 65, 74, 83, 84, 87 };
       int startingAt = 0;
       boolean isSorted = isSorted(array, startingAt, array.length);
       System.out.print("array: ");
       for (int i = 0; i < array.length; ++i) System.out.print(array[i] + " ");
       if (isSorted)
               System.out.println(" is SORTED");
       else
               System.out.println(" is NOT SORTED");

***********************************************************************

       // P A L I N D R O M E
       String s = "stanleyyelnats"; // try with several differnt values that are or not palindromes
       if (isPalindrome(s, 0, s.length() - 1))
               System.out.format("%s IS a palindrome ", s);
       else
               System.out.format(" %s NOT a a palindrome ", s);

   } // END MAIN

       // count stars in a triangle using # of rows as input
       static int triStars(int rows)
       {
           if (rows != 0)
           return rows + triStars(rows - 1);
           else
           return rows;
       }
       // given a number return the sum of the digits
       static int sumDigits(int n)
       {
           if (n != 0)
           {
           return (n % 10 + sumDigits(n / 10));
           }
           else
           {
           return 0;
           }
       }
       // given a number compute the number of 7's in that number
       static int count7s(int n)
       {
       int count = 0;

           if (n == 0)
           {
           count = 0;
           }
           else if (n % 10 == 7)
           {
           count++;
           count += count7s(n / 10);
           }
           else
           {
           count += count7s(n / 10);
           }

       return count;
       }
       // given a number count the number of 8 but if an 8 has another 8 to its immdiate left count it as 2
       // the number 8802388 will return a count of 6
       static int count8s(int n)
       {
           if (n <= 0)
           {
               return 0;
           }
          
           int last;
          
           if(n % 100 == 88)
           {
               last = 2;
           }
           else if(n % 10 == 8)
           {
               last = 1;
           }
           else
           {
               last = 0;
           }
          
           int withoutLast = n/10;
           return last + count8s(withoutLast);
       }
       //compute base to the power n
       static int powerN(int base, int n)
       {
           if (n != 0)
           return (base * powerN(base, n - 1));
           else
              
       return 1;
       }

**********************************************************************************
       // return true only if the array is sorted
       static boolean isSorted(int array[], int i, int count)
       {
           if (i == count)
           return true;
           int smallest = array[0];
           for (int j = 1; j < count; j++) {
               if (array[j] < smallest)
               {
               smallest = array[j];
               }
           }
           int c = array[smallest];
           array[smallest] = array[i];
           array[i] = c;
           isSorted(array, i + 1, count);
          
       return true;
       }

***********************************************************************

       // return true if string is palindrome
       static boolean isPalindrome(String s, int lo, int hi)
       {
           if (lo >= hi)
           return true;
           if (s.charAt(lo) != s.charAt(hi))
              
           return false;
           return isPalindrome(s, ++lo, --hi);
       }
} // END CLASS Recursion

Explanation / Answer

import java.util.Arrays;
public class Recursion
{
   public static void main(String[] args)
   {

       // T R I S T A R S
       int rows = 5;
       System.out.format("A tringle with %d rows contains %d stars ", rows, triStars(rows));

       // S U M D I G I T S
       int number = 12345;
       System.out.format("The sum of the digits in the number %d = %d ", number, sumDigits(number));

       // C O U N T 7 S
       number = 713274772;
       System.out.format("There are %d occurances of the digit 7 in the number %d ", count7s(number), number);

       // C O U N T 8 S
       number = 82338828;
       System.out.format("There are %d occurances of the digit 8 in the number %d ", count8s(number), number);

       // P O W E R N
       int base = 2, exponent = 8;
       System.out.format("%d to the power %d = %d ", base, exponent, powerN(base, exponent));

//************************************************************************

       // I S S O R T E D
       // perturb values as needed to test on an unsorted array
       int[] array = { 7, 8, 12, 20, 21, 22, 37, 41, 55, 60, 65, 74, 83, 84, 87 };
       int startingAt = 0;
       int isSorted=isSorted(array, startingAt, array.length-1);
       System.out.print("array: ");
       for (int i = 0; i < array.length; ++i) System.out.print(array[i] + " ");
       if (isSorted==1)
           System.out.println(" is SORTED");
       else
           System.out.println(" is NOT SORTED");
     
//***********************************************************************

       // P A L I N D R O M E
       String s = "stanleyyelnats"; // try with several differnt values that are or not palindromes
       if (isPalindrome(s, 0, s.length() - 1))
               System.out.format("%s IS a palindrome ", s);
       else
               System.out.format(" %s NOT a a palindrome ", s);

   } // END MAIN

       // count stars in a triangle using # of rows as input
       static int triStars(int rows)
       {
           if (rows != 0)
           return rows + triStars(rows - 1);
           else
           return rows;
       }
       // given a number return the sum of the digits
       static int sumDigits(int n)
       {
           if (n != 0)
           {
           return (n % 10 + sumDigits(n / 10));
           }
           else
           {
           return 0;
           }
       }
       // given a number compute the number of 7's in that number
       static int count7s(int n)
       {
       int count = 0;

           if (n == 0)
           {
           count = 0;
           }
           else if (n % 10 == 7)
           {
           count++;
           count += count7s(n / 10);
           }
           else
           {
           count += count7s(n / 10);
           }

       return count;
       }
       // given a number count the number of 8 but if an 8 has another 8 to its immdiate left count it as 2
       // the number 8802388 will return a count of 6
       static int count8s(int n)
       {
           if (n <= 0)
           {
               return 0;
           }
        
           int last;
        
           if(n % 100 == 88)
           {
               last = 2;
           }
           else if(n % 10 == 8)
           {
               last = 1;
           }
           else
           {
               last = 0;
           }
        
           int withoutLast = n/10;
           return last + count8s(withoutLast);
       }
       //compute base to the power n
       static int powerN(int base, int n)
       {
           if (n != 0)
           return (base * powerN(base, n - 1));
           else
            
       return 1;
       }

//**********************************************************************************
       // return 1 only if the array is sorted
       static int isSorted(int array[], int start, int end)
       {
       
          if (start < end)
          {
             isSorted(array, start+1, end);
             if(array[start]<=array[end])
             {
               isSorted(array, start, end-1);
             }
             else if(array[start]>array[end])
             {
               int temp = array[start];
               array[start] = array[end];
               array[end] = temp;
               isSorted(array, start, end-1);
             }
          }
          else if(start == end)
             return 1;
          return 1;
     
       }
         
         
      

//***********************************************************************

       // return true if string is palindrome
       static boolean isPalindrome(String s, int lo, int hi)
       {
           if (lo >= hi)
           return true;
           if (s.charAt(lo) != s.charAt(hi))
            
           return false;
           return isPalindrome(s, ++lo, --hi);
       }
} // END CLASS Recursion

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote