+ 2

how to write a bubble sort program

20th Sep 2016, 2:49 PM
sai satwik
sai satwik - avatar
2 Respuestas
+ 1
I am just giving you the main logic.. Code snippet: for(i=0;i<a.length-1; i++) { for(j=0;j<a.length-1-i;j++) { if(a[j]>a[j+1]) { temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } } } Now your array is sorted. This is the bubble sort method.
12th Nov 2016, 12:33 PM
Rishav Choudhuri
- 4
import java.util.Arrays; import java.util.Collections; import java.util.List; public class SortListAsc { public static void main(String[] args) { // Define a sample list List<Integer> l = Arrays.asList(7, 2, 8, 42, 6); //Sort using the Collections class Collections.sort(l); //Print the list System.out.println(l); } }
20th Sep 2016, 2:53 PM
Rohan agarwal
Rohan agarwal - avatar