Please why is the code not working as expected. I inserted some integers and the code is expected to sort the out in asending | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please why is the code not working as expected. I inserted some integers and the code is expected to sort the out in asending

https://code.sololearn.com/cLsfrdhMfMsj/?ref=app

2nd May 2020, 11:02 AM
David Felix
David Felix - avatar
2 Answers
0
public class Program { public static void main(String[] args) { int [] nums = {15, 10,13,14,11,12}; int arrayLength = nums.length; System.out.println("the unsorted numbers are: "); for (int k = 0; k < arrayLength; k++){ System.out.print (nums[k]+ " "); } System.out.println (); for (int i = 0; i < arrayLength -1; i++ ){ for (int j = 0; j < arrayLength - i - 1;j++ ){ if (nums [j] > nums [j+1]){ int temp = nums [j]; nums [j] = nums [j+1]; nums [j+1] = temp; } } // System.out.print (nums [i] + " "); } System.out.println (); System.out.print("Above are the sorted numbers of the array: "); for (int k = 0; k < arrayLength; k++){ System.out.print ("'"+nums[k]+ "' "); } System.out.println (); } }
2nd May 2020, 1:43 PM
Arvind Singh Rawat
Arvind Singh Rawat - avatar
0
You printed the first array element inside the loop. 1. Print whole array after completion. Like above. In bubble sort, sorted elements are shifted to last.
2nd May 2020, 1:43 PM
Arvind Singh Rawat
Arvind Singh Rawat - avatar