forked from Asiatik/codezilla
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
39 lines (34 loc) · 866 Bytes
/
Copy pathBubbleSort.java
File metadata and controls
39 lines (34 loc) · 866 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
//Bubble sort Algorithm
// Java program for implementation of BubbleSort
class BubbleSort
{
/* Fuction Implementing Bubble Sort Algorithm */
void sort(int arr[]){
int length = arr.length;
for(int i=0; i<length; i++)
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;
}
}
/* 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 */