0% found this document useful (0 votes)
57 views2 pages

Import Public Class Int Static New Public Static Void: // Length Array //array // Add Values To Array

This document contains the code for a bubble sort algorithm in Java. It prompts the user to enter the size of an array and then values for each element. It then prints the unsorted array. The bubble method performs the bubble sort on the input array, repeatedly iterating through adjacent elements and swapping any out of order. Finally, it prints the sorted array.

Uploaded by

sara amer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views2 pages

Import Public Class Int Static New Public Static Void: // Length Array //array // Add Values To Array

This document contains the code for a bubble sort algorithm in Java. It prompts the user to enter the size of an array and then values for each element. It then prints the unsorted array. The bubble method performs the bubble sort on the input array, repeatedly iterating through adjacent elements and swapping any out of order. Finally, it prints the sorted array.

Uploaded by

sara amer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Bubble_sort.

java

1 import java .util.Scanner;


2 public class Bubble_sort {
3 int a [];
4
5 static Scanner scan = new Scanner (System.in) ;
6
7 public static void main (String args[]){
8
9 System.out.println("Hello I'm Bubble sort my operation basic is sorted array ");
10 System.out.println("");
11 System.out.println("Time efficiency : O(n^2) & space efficiency : O(1)");
12 System.out.println("");
13 System.out.println( "Pleas enter size array " );
14 System.out.println("");
15
16 int n=scan.nextInt(); // length array
17
18 int a []=new int [n]; //array
19
20 // add values to array
21
22 for(int i = 0 ; i<=n-1 ; i++){
23 System.out.println( "Pleas enter value "+ (i+1) + " into array" );
24 System.out.println("");
25
26 int s= scan .nextInt();
27 a[i]=s;
28 }
29 System.out.println( "befor sorting : " );
30 System.out.println("");
31
32 for(int k = 0 ; k<=n-1 ; k++)
33
34 System.out.print( "a["+k+"]"+ "=" + a[k] +" " );
35 System.out.println("");
36 System.out.println("");
37 bubble(a);
38 }
39 public static void bubble (int[] aa) {
40 int n = aa.length;
41 int j, i;
42 int temp=0;
43 for(i = 0 ; i<n ; i++){
44 for( j= 1 ; j<(n-i) ; j++){
45 if (aa[j-1]> aa[j]){
46
47 temp =aa[j-1];
48 aa[j-1]=aa[j];
49 aa[j]=temp;}
50 }
51
52 }//end for
53
54 System.out.println( "after sorting : " );
55 System.out.println("");
56
57 for(int k = 0 ; k<=n-1 ; k++)
58
59 System.out.print( "a["+(k)+"]"+ "=" + aa[k] +" " );
60
61 }//end method
62 }

Page 1
Bubble_sort.java

63

Page 2

You might also like