Hi
good evening friends
Today
one of my friends asked about “bubble sort” related to JAVA array sorting
So this
is a description about bubble sort -
Bubble sort, sometimes referred to as sinking sort, is a
simple sorting algorithm that repeatedly steps through the list to be sorted,
compares each pair of adjacent items and swaps them if they are in the wrong
order. The pass through the list is repeated until no swaps are needed, which
indicates that the list is sorted. The algorithm, which is a comparison sort,
is named for the way smaller elements "bubble" to the top of the
list. Although the algorithm is simple, it is too slow and impractical for most
problems even when compared to insertion sort. It can be practical if the input
is usually in sort order but may occasionally have some out-of-order elements
nearly in position.
–Wiki
public class arraysort {
public static void
main(String[] args) {
int[] list =
{2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79};
// bubble sort
(array)
int
test[]={90,45,50,80,100,-45,0,235,807};
int temp,j,i;
for(j=0;j<=test.length;j++){ // I used this for
loop to continue bubble sort part until whole array is sorted
for( i=0;i<
test.length-1;i++){
if(test[i]>test[i+1]){
// if 90 > 45
temp=test[i+1];
//store 45 in temp;
test[i+1]=test[i];
//put the 45 in 90's
place
test[i]=temp;
//put the 90 in 45's
place
}}}
for(i=0;test.length>i;i++)
System.out.println(test[i]);
}
}
more examples
more examples
No comments:
Post a Comment