Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // program to implement bubble sorting algorithm
- import java.util.Scanner;
- import java.io.*;
- import static java.lang.System.*;
- /**
- * Created by MOHIT on 31-12-2016.
- */
- public class Sorting {
- public static void main(String arg[]) {
- Scanner sc = new Scanner(in);
- System.out.print("Enter the Array to be sorted : ");
- String str1 = sc.next();
- String[] strArr = str1.split(",");
- int[] numbers = new int[strArr.length];
- int i=0;
- for (String str2 :strArr) {
- int temp = Integer.parseInt(str2);
- numbers[i] = temp;
- i++;
- }
- numbers = bubbleSort(numbers);
- System.out.println("The Array after sorting is : ");
- for(i=0; i<numbers.length; i++){
- System.out.print(numbers[i] + " ");
- }
- }
- private static int[] bubbleSort(int[] numbers) {
- int temp;
- for(int i=0; i< numbers.length-1; i++){
- for(int j=0; j<numbers.length-1; j++){
- if(numbers[j]> numbers[j+1]){
- temp = numbers[j];
- numbers[j] = numbers[j+1];
- numbers[j+1] = temp;
- }
- }
- }
- return numbers;
- }
- }
Add Comment
Please, Sign In to add comment