+ 2
how to write a bubble sort program
2 Answers
+ 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.
- 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);
}
}