How to sort a list using Bubble sort algorithm

• 04-05-2008, 10:09 AM
Java Tip
How to sort a list using Bubble sort algorithm
This code is the implementation of the bubble sort algorithm.

Code:

```public class BubbleSortExp {         public static void main(String[] args) {                 int s[] = { 23, 12, 466, 22, 1 };                 int temp;                 for (int i = 0; i < 4; i++) {                         for (int j = 0; j < 4 - i; j++) {                                 if (s[j + 1] < s[j]) {                                         temp = s[j + 1];                                         s[j + 1] = s[j];                                         s[j] = temp;                                 }                         }                 }                 System.out.println("Sorted Array");                 for (int i = 0; i < s.length; i++) {                         System.out.println(s[i]);                 }                 System.out.println(s);         } }```
• 04-11-2008, 06:51 AM
CaptainMorgan
Ewww... :)

The dreaded and most fundamental, good for a small number of elements, but horrible for larger than.. say.. 10 elements? :D

What else you got for us Chief ? ;)
• 04-11-2008, 07:34 AM
Eranga
What you mean Captain, its work fine for 15 numbers also. :)

I'm worried why did you say that for 10 elements it is horrible. ;)
• 04-29-2008, 08:04 PM
Zosden
it works for any number of n just it grows at n^2 so for each element you add the time growth is proportional to n^2.