0

Why is this bubble sort code not working?

private static int[] array = { 121, 19, 49, 11, 7, 13, 25 }; public int[] bubbleSort(int[] a) { for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) { if (a[i] > a[j]) { int num = a[i]; a[i] = a[j]; a[j] = num; } } } return a; }

13th Nov 2016, 6:13 PM
Andrew Marion
Andrew Marion - avatar
4 Answers
+ 2
public class Program { private static int[] array = { 121, 19, 49, 11, 7, 13, 25 }; public static void main(String[] args) { int[] arr; arr=bubbleSort(array); for(int i=0;i<arr.length;i++) System.out.println(arr[i]); } public static int[] bubbleSort(int[] a) { for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) { if (a[i] > a[j]) { int num = a[i]; a[i] = a[j]; a[j] = num; } } } return a; } }
14th Nov 2016, 4:44 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 2
Class A { Scanner sc=new Scanner(System.in); System.out.println("Enter the Numbers "); int temp=0; int n=sc.NextInt(); int a[]=new int[n]; System.out.println("Enter elements to be short"); for(int i=0;i<n;i++) { a[i]=sc.NextInt(); } System.out.println("Elements without Sorting"); for(int i=0;i<n;i++) { System.out.println(a[i]);} for(Int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(a[i]<b[j]) { temp =b[j]; b[j]=a[i]; a[i]=temp;} System.out.println(“After shorting "); for(int i=0;i<n;i++) { System.out.println(a[i]; } } }
14th Nov 2016, 6:44 PM
Mukesh Kumaar
Mukesh Kumaar - avatar
+ 1
Here are some things you might want to try: - Instead of running this entirely in for loops, try starting with a while loop, with the exit condition being whether or not the loop is sorted. Then run a single for loop inside of that. - Your second for loop is going to run out of bounds. Since j is always initially set to i + 1, when i reaches the last index of your array, j is going to be 1 past the array's length. So make sure i is always less than a.length - 1. - Methods can't be nested inside other methods. Try moving bubbleSort(int[] a) outside of the main method, then from the main method, call bubbleSort. - Instead of trying to reassign the values in a, try creating a new array and copying everything from a into that. To do that, write: int[] sorted = a; before you do any sorting operations. Then do all the sorting on the "sorted" array. Hope this helps :)
14th Nov 2016, 5:15 AM
DaemonThread
DaemonThread - avatar
+ 1
import java.util.Scanner; public class Program2 { public static void main(String[] args) { // TODO Auto-generated method stub int n; Scanner scan = new Scanner(System.in); System.out.println("enter the number"); n=scan.nextInt(); int a[]=new int[n]; System.out.println("enter "+n+" elements to sort"); for(int i=0;i<n;i++){ a[i]=scan.nextInt(); } System.out.println("elements before sorting....................."); for(int i=0;i<n;i++){ System.out.println(a[i]+" "); } int temp; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(a[i]<a[j]){ temp=a[i]; a[i]=a[j]; a[j]=temp; } } } System.out.println("elements after sorting.................."); for(int i=0;i<n;i++){ System.out.println(a[i]+" "); } } }
14th Nov 2016, 6:44 PM
Mukesh Kumaar
Mukesh Kumaar - avatar