forked from dimpeshpanwar/javabasicprograms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubble_sort.java
42 lines (39 loc) · 1.22 KB
/
Bubble_sort.java
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
package com.medhansh;
public class Bubble_sort {
static void print (int a[]) //function to print array elements
{
int n = a.length;
int i;
for (i = 0; i < n; i++)
{
System.out.print(a[i] + " ");
}
}
static void bubbleSort (int a[]) // function to implement bubble sort
{
int n = a.length;
int i, j, temp;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
public static void main(String[] args) {
int a[] = {35, 10, 31, 11, 26};
Bubble_sort b1 = new Bubble_sort();
System.out.println("Before sorting array elements are - ");
b1.print(a);
b1.bubbleSort(a);
System.out.println();
System.out.println("After sorting array elements are - ");
b1.print(a);
}
}