Friday, 26 January 2018

Write a Java Program for Sorting using Bubble Sort

package com.tutorial4u;

import java.util.Scanner;

public class BubbleSort {
     public static void main(String[] args) {
           int num, swap,i,j;
           Scanner sc= new Scanner(System.in);
           System.out.print("Enter number of Integer to sort : ");
           num = sc.nextInt();
           int array[] = new int[num];
           System.out.println("Enter "+num+ " Integer :");
           for(i=0;i<num;i++){
                array[i] = sc.nextInt();
           }
           for(i=0;i<(num-1);i++){
                for(j=0;j<(num-1)-i;j++){
                      if(array[j] > array[j+1]){
                           swap = array[j];
                           array[j] = array[j+1];
                           array[j+1] = swap;
                      }
                }
           }
           System.out.println("Sorted list of number is : ");
           for(i=0; i<num; i++){
                System.out.println(array[i]);
           }
     }

}



Output : 

Enter number of Integer to sort : 5
Enter 5 Integer :
89
43
65
32
56
Sorted list of number is :
32
43
56
65
89

No comments:

Post a Comment

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...