Bubble sort to the array ....Can someone please help me to find the mistake in this code😭😭😭 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Bubble sort to the array ....Can someone please help me to find the mistake in this code😭😭😭

public static void main(String[] arg) { int aee [] = {6,4,78,15,2,4,1,8,10,3,9}; System.out.println("the original array is : "+Arrays.toString(aee)); bubble(aee); } public static void swap(int a, int b){ int c =a; a=b; b=c; } public static void bubble(int ary[]){ int l = ary.length; int lo =0 , hi =l-1; for(int i=0; i<hi; i++) { for(int j=0; j<hi-1; j++) { if(ary[i]>ary[j+1]) swap(ary[j],ary[j+1]); } } print(ary); } public static void print(int arr[]){ int l = arr.length; System.out.println("the array is : "); for(int i=0; i<l-1; i++) { System.out.print(arr[i]+","); } }

8th May 2020, 7:52 PM
Avdhesh Gupta
Avdhesh Gupta - avatar
2 Answers
0
Array are pass by refference but not it individual values, so swap function will not work that way... Corrected code: public static void bubble(int ary[]){ int hi= ary.length; for(int i=0; i<hi; i++) { for(int j=0; j<hi-1; j++) { if(ary[j]>ary[j+1]) { int c =ary[j]; ary[j]=ary[j+1]; ary[j+1]=c; } } } System.out.println("the array is : "); for(int i=0; i<hi; i++) System.out.print(ary[i]+","); }
8th May 2020, 8:35 PM
Jayakrishna 🇮🇳