0
public class BubbleSort {
    public static void main(String []args) {
        int[] values = {45, 67, 12, 34, 25, 39};
        int pass, comparison, temp;
        String results = "";

        for (pass = 1; pass <= values.length - 1; pass++) {
            for (comparison = 1; comparison <= values.length - pass; comparison++) {
                if (values[comparison - 1] > values[comparison]) {
                    temp = values[comparison - 1];
                    values[comparison - 1] = values[comparison];
                    values[comparison] = temp;
                }
            }
        }

        for (int i = 0; i < values.length; i++){
             results += values[i] + "\n";
        }
        System.out.print(results);
    }
}

That's my code. It prints out: 12 25 34 39 45 67

How do I reverse it so it prints out:

67 45 39 34 25 12

Also, how do I just print the array out normally like:

45 67 12 34 25 39

I'm stuck on how to print them out.

nullptr
  • 3,320
  • 7
  • 35
  • 68

5 Answers5

0

You have to revert it on your own. there is not method out of the box that does this for you.

for(int i = 0; i < values.length / 2; i++)
{
    int temp = values[i];
    values[i] = values[values.length - i - 1];
    values[values.length - i - 1] = temp;
}

If you want to print the originial values of the array you must copy it, before you modify it.

Philipp Sander
  • 10,139
  • 6
  • 45
  • 78
0

for reverse:

for (int i = values.length; i >= 0; i--) results += values[i] + "\n";

for original you need to copy the origina array first before processing.

myxlptlk
  • 139
  • 3
0

You can do it several ways but reversing the array is not necessary to print it reversed...

results = "";
for (int i=0; i<values.length; i++) results = results+" "+values[i];
System.out.println("Non reversed :" + results);
results = "";
for (int i=0; i<values.length; i++) results = values[i]+" "+results;
System.out.println("Reversed :" + results);

Now if you really want to reverse your array you just have to exchange values both by both, first and last, second and before-last, etc. up to the middle :

for (int i=0 i<values.length/2; i++) {
  int tmp = values[i];
  values[i] = values[values.length-1-i];
  values[values.length-1-i] = tmp;
}
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
0

This will do:

public class BubbleSort
{
    public static void main(String []args)
    {
        String results = "";
        int[] values = {45, 67, 12, 34, 25, 39};
        for (int i = 0; i < values.length; i++) results += values[i] + "\n";
        System.out.print(results);
        int pass, comparison, temp;
        results = "";
        for (pass = 1; pass <= values.length - 1; pass++)
        {
            for (comparison = 1; comparison <= values.length - pass; comparison++)
            {
                if (values[comparison - 1] > values[comparison])
                {
                    temp = values[comparison - 1];
                    values[comparison - 1] = values[comparison];
                    values[comparison] = temp;
                }
            }
        }
        for (int i = 0; i < values.length; i++) results += values[i] + "\n";
        System.out.print(results);
        results = "";
        for (int i = values.length; i >= 0; i--) results += values[i] + "\n";
        System.out.print(results);
    }
}
Lrrr
  • 4,755
  • 5
  • 41
  • 63
0

There is a method in the Collections interface which reverses a List: Collections.reverse()

List<Integer> list = new ArrayList<Integer>(values); //Arrays.asList(45, 67, 12, 34, 25, 39);
Collections.reverse(list);
System.out.println(list);

For the print part remember that you have an array and arrays are tricky to print. The list instead can be printed via println with no problems.

Community
  • 1
  • 1
Narmer
  • 1,414
  • 7
  • 16