forked from Shradha27/codezilla
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
50 lines (45 loc) · 1003 Bytes
/
Copy pathBubbleSort.java
File metadata and controls
50 lines (45 loc) · 1003 Bytes
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
//Bubble sort Algorithm
// Java program for implementation of BubbleSort
class BubbleSort
{
/* Fuction Implementing Bubble Sort Algorithm */
void sort(int arr[])
{
int swapped;
int length = arr.length;
for(int i=0; i<length; i++)
{
swapped=0;
for(int j=0; j<length-i-1; j++)
{
if(arr[j]>arr[j+1])
{
int temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
swapped=1;
}
}
if(swapped==0)
break;
}
}
/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
// Driver program
public static void main(String args[])
{
int arr[] = {10, 7, 8, 9, 1, 5};
BubbleSort ob = new BubbleSort();
ob.sort(arr);
System.out.println("sorted array");
printArray(arr);
}
}
/*This code is contributed by Ankush Rodewad */