Tuesday, 27 August 2013

Bubble Sort Function In JAVA

BUBBLE SORT

This is a sorting algorithm which arrange the elements of Array in to descending or ascending type.

public static void bubbleSort(int [] array)

{
int temp;
for(int i=0;i<array.length;i++)
{
for(int j=0;j<array.length-1;i++)
{
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
}


If you want to sort array in descending order then

if(array[j]<array[j+1])
{
temp=array[j];
array[j+1]=array[j];
array[j]=temp;
}

No comments:

Post a Comment