java - How do I print a sorted array? -
this question has answer here:
- what's simplest way print java array? 24 answers
the following code supposed sort array initialized below.
int[] intarray = { 32, 42, 1, 23, 56, 75, 32, 23 }; int temp = 0; (int = 0; < intarray.length; i++) { (int j = 1; j < intarray.length; j++) { if (intarray[j - 1] > intarray[j]) { temp = intarray[j - 1]; intarray[j - 1] = intarray[j]; intarray[j] = temp; } } }
what should type sorted array , should type it?
i have tried options such system.out.println(temp)
between last 2 closing brackets , 1 before those, not getting values printed, , 32
printing many times instead.
writing code system.out.println(j)
or system.out.println(i)
in same area doesn't work either. can explain why these codes don't work?
if want print sorted array, print elements in loop after array sorted:
for(int i=0; < intarray.length; i++) { system.out.println(intarray[i]); }
Comments
Post a Comment