Bubble Sort
import java.util.*;
public class Arrylistsort {
public static void main(String[] args) {
// Sorting and getting max value arry.
ArrayList< Integer> arrylist=new ArrayList
arrylist.add(122);
arrylist.add(233);
arrylist.add(455);
arrylist.add(366);
arrylist.add(566);
System.out.println("Unsort:"+arrylist);
Collections.sort(arrylist);
int j= Collections.max(arrylist);
System.out.println(arrylist);
System.out.println("max value" +j);
}
}
Program output :
Unsort:[122, 233, 455, 366, 566]
[122, 233, 366, 455, 566]
max value566
Here program fist sorts the arraylist and find out maximum value using Collection framework. Another
program for string sort using bubble sort algorithms.
class strsort
{
public static void main(String args[])
{
String s[]={"banglore","delhi","madras","calcutta","pune"};
String temp=null;
int l=s.length;
int i,j=0;
System.out.println("The strings before the sort are:");
for(i=0;i
System.out.println(" "+s[i]);
for(i=0;i
{
{
if(s[j].compareTo(s[i])<0 span="">
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
System.out.println(" ");
System.out.println("The array elements after sorting are:");
for(i=0;i
System.out.print(" "+s[i]);
}
}
program output :
The strings before the sort are:
banglore
delhi
madras
calcutta
pune
The array elements after sorting are: pune banglore delhi calcutta madras
Here is another example of sorting array using bubble sort and swapping concept
class numsort
{
public static void main(String args[])
{
int a[]={25,69,20,60,4,49};
int l=a.length;
int i,j;
System.out.println("the array elements before are:");
for(i=0;i
System.out.print(" "+a[i]);
for(i=0;i
{
for(j=0;j
{
if(a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println(" ");
System.out.println("the array elements after sorting are:");
for(i=0;i
System.out.print(" "+a[i]);
}
}
Program output :
the array elements before are:
25 69 20 60 4 49
the array elements after sorting are:
69 60 49 25 20 4
Bubble Sort
Reviewed by Mukesh Jha
on
11:15 PM
Rating:
No comments:
Add your comment