Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
1.0 kB
5
Indexable
Never
package assignment3;

import java.util.Scanner;

public class BubbleSort {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		System.out.println("What is the size of the array?");
		int size = scan.nextInt();
		int [] given = new int[size];
		int[] assorted = new int[size];
		
		for (int i=0; i< assorted.length; i++) {
			System.out.println("Enter the number: ");
			assorted[i] = scan.nextInt();
			given[i] = assorted[i];			
		}
		
		for (int i = 0; i < (assorted.length - 1); i++ )
			for (int j= 0; j< (assorted.length - i -1); j ++)
				if (assorted[j] > assorted[j + 1] ) {
					int n = assorted [j];
					assorted[j] = assorted[j+1];
					assorted[j+1] = n;
				}		
							
		System. out. print ("Given values:");
		for (int begin = 0; begin < (given.length); begin++) {
			System.out.print(" " + given [begin]) ;
		}
		
		System.out.println("");
		System.out.print("Sorted values:");
		for (int end = 0; end < (assorted. length); end++) {
			System. out.print(" " + assorted[end] );
		}
	}

}
Leave a Comment