Java Bubble Sort

import java.util.*;
public class BubbleSort
{
        public static void main(String[] args) {
            Scanner input = new Scanner (System.in);
               int a;
                int intArray[] = new int[]{30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5};
               System.out.println("Masukan Nim Anda");
               a=input.nextInt();
               System.out.println("Nim anda : " + a);
                System.out.println("Nilai Sebelum Diurutkan");
                for(int i=0; i < intArray.length; i++){
                        System.out.print(intArray[i] + " ");
                }
              
                bubbleSort(intArray);
              
                System.out.println("");
                System.out.println("Nilai Sesudah Diurutkan");
                for(int i=0; i < intArray.length; i++){
                        System.out.print(intArray[i] + " ");
                }
        }

        private static void bubbleSort(int[] intArray)
        {
            int n = intArray.length;
            int temp = 0;
              
            for(int i=0; i < n; i++)
            {
                for(int j=1; j < (n-i); j++)
                {          
                    if(intArray[j-1] > intArray[j])
                    {
                        temp = intArray[j-1];
                        intArray[j-1] = intArray[j];
                        intArray[j] = temp;
                    }              
                }
            }
        }
}

0 Response to "Java Bubble Sort"

Posting Komentar