-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
50 lines (48 loc) · 1.37 KB
/
BubbleSort.java
File metadata and controls
50 lines (48 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Program: Bubble Sort Algorithm
// Topic: Sorting Algorithms (Arrays)
// Description: Reads an array of integers from user input and sorts it in ascending order using the Bubble Sort technique.
// Demonstrates nested loop structure for pairwise comparison and swapping of adjacent elements to achieve sorting.
package Sorting;
import java.util.*;
/**
*
* @author Samim
*/
public class BubbleSort
{
public void sort(int ar[],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(ar[j+1]<ar[j])
{
int temp=ar[j+1];
ar[j+1]=ar[j];
ar[j]=temp;
}
}
}
System.out.println("Sorted Array :");
for(int i=0;i<n;i++)
{
System.out.println(ar[i]);
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int size;
System.out.println("Enter Number oF Elements :");
size=sc.nextInt();
int ar[]=new int [size];
System.out.println("Enter The Elements Of the Array :");
for(int i=0;i<size;i++)
{
ar[i]=sc.nextInt();
}
BubbleSort obj=new BubbleSort();
obj.sort(ar, size);
}
}