Sunday, February 24, 2013

Test Bubble Sort





public class Testbubblesort {

    public static void main(String[] args) {
       
        int list[]={2, 56, 34, 25, 73, 46, 89,
                        10, 5, 16};
        int i;
       
        bubbleSort(list, 10);
       
        System.out.println("After sorting, the list elements are:");
       
        for(i=0; i<10; i++)
            System.out.print(list[i]+" ");
       
        System.out.println();

    }
   
    public static void bubbleSort(int list[], int listLength){
       
        int temp;
        int counter, index;
       
        for (counter = 0; counter<listLength -1; counter++){
            for(index=0; index<listLength -1 - counter; index++)
                if (list[index]>list[index+1]){
                    temp = list[index];
                    list[index]=list[index+1];
                    list[index+1]=temp;
                }
        }
    }

}

No comments:

Post a Comment