0

Sorting method should returns a string with a comma separated list of integers from the array, sorted in ascending order. My code does not sort for the size of 4 or more. I am also not getting anything in console for return type without system.out.println(); Any suggestion /help is appreciated.

import java.util.ArrayList; public class Sorting { public static void main(String[] args) { ArrayList<Integer> alInput = makeArray(5); ArraySorter(alInput); } public static String ArraySorter (ArrayList<Integer> alSort) { if (alSort.size()<=1) { return String.valueOf(alSort.get(0)) ; } int iMin = alSort.get(0); int iMinIndex = 0; for (int i = 0;i<alSort.size();i++ ) { if (alSort.get(i)<iMin) { iMin = alSort.get(i); iMinIndex = i; } } alSort.remove(iMinIndex); System.out.println(String.valueOf(iMin) + ArraySorter (alSort)); return String.valueOf(iMin) + ArraySorter (alSort); } public static ArrayList<Integer> makeArray(int iSize) { ArrayList<Integer> alNum = new ArrayList<Integer>(); for (int i = 0; i<iSize;i++) { alNum.add((int )(Math.random() * 100 + 1)); } System.out.println(alNum); return alNum; } }
2
  • Use simple for loop to get through all elements in array, 1 boolean variable that will state if loop is sorted or not and 1 temp variable to save element from an array while you do swapping if element[i] is greater than element[i + 1] (if you want it ascending). Commented Nov 8, 2017 at 22:30
  • Have you searched this stackoverflow.com/search?q=Sorting+ArrayList+java stackoverflow.com/search?q=quick+sort+algorithms+java Commented Nov 8, 2017 at 23:54

2 Answers 2

1

Why dont you try this easy way,

alInput.sort(null);
        String myString = "";
        for (int i=0;i<alInput.size();i++){
            myString  = myString + String.valueOf(alInput.get(i)) + ",";
        }
        myString= myString.substring(0,myString.length()-1);
        System.out.println(myString);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Rishabh! This works but I was not allowed to use .sort for this project. We needed to use recursion in the code. Thanks any way buddy! @Rishabh Dugar
Oh , that was not mentioned in problem which i why i answered . :)
0

Give this a try: I have added comments explaining what I have changed for the fixe(s). Hope it helps :)

import java.util.ArrayList;

public class Sorting {  
    public static void main(String[] args) 
    {
        ArrayList<Integer> alInput = makeArray(5);

        // now you can get the sorted string :)
        String sortedResult = ArraySorter(alInput);
        System.out.println("The sorted array is : " + sortedResult);

    }
    public static String ArraySorter (ArrayList<Integer> alSort) 
    {
        int iMin = alSort.get(0);
        int iMinIndex = 0;
        for (int i = 0;i<alSort.size();i++ )
        {
            if (alSort.get(i)<iMin)
            {
                iMin = alSort.get(i);
                iMinIndex = i;
            }
        }
        alSort.remove(iMinIndex);

        // added this one in myself, this is your base case for preventing a stack overflow:
        if (alSort.size() == 0) {
            return String.valueOf(iMin);
        }

        return String.valueOf(iMin) + "," + ArraySorter (alSort); // changed from : String.valueOf(iMin) + ArraySorter (alSort);
    }

    public static ArrayList<Integer> makeArray(int iSize)
    {
        ArrayList<Integer> alNum = new ArrayList<Integer>();

        for (int i = 0; i<iSize;i++)
        {
            alNum.add((int )(Math.random() * 100 + 1));
        }
        System.out.println(alNum);
        return alNum;
    }
}

2 Comments

Thanks buddy! this is what I was missing! Thanks for your help man! @Dragoş Bercea
No worries mate, It was pretty fun to debug.

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.