leaguensa.blogg.se

Java program for bubble sort
Java program for bubble sort






( 1 5 4 2 8 ) → ( 1 4 5 2 8 ), Swap since 5 > 4 ( 1 4 5 2 8 ) → ( 1 4 2 5 8 ), Swap since 5 > 2 ( 1 4 2 5 8 ) → ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them. Three passes will be required įirst Pass ( 5 1 4 2 8 ) → ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. In each step, elements written in bold are being compared. Take an array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. Its average speed is comparable to faster algorithms like quicksort. Comb sort compares elements separated by large gaps, and can move turtles extremely quickly before proceeding to smaller and smaller gaps to smooth out the list.








Java program for bubble sort