8000 Merge pull request #1 from AllAlgorithms/sorting · AllAlgorithms/java@aae4ff8 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit aae4ff8

Browse files
authored
Merge pull request #1 from AllAlgorithms/sorting
Create BubbleSort.java
2 parents 7e7e066 + 5a711fe commit aae4ff8

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

BubbleSort.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Java implementation of bubble sort
3+
*
4+
* @author Carlos Abraham Hernandez (abraham@abranhe.com)
5+
*/
6+
7+
8+
public class BubbleSort {
9+
10+
void bubbleSort(int arr[]){
11+
for (int i = 0; i < arr.length-1; i++)
12+
for (int j = 0; j < arr.length-i-1; j++)
13+
if (arr[j] > arr[j+1]){
14+
int temp = arr[j];
15+
arr[j] = arr[j+1];
16+
arr[j+1] = temp;
17+
}
18+
}
19+
20+
// Function to print elements
21+
void printArray(int arr[]){
22+
for (int i=0; i<arr.length; ++i)
23+
System.out.print(arr[i] + " ");
24+
System.out.println();
25+
}
26+
27+
// Driver method to test above
28+
public static void main(String args[]){
29+
30+
BubbleSort bubble_sort = new BubbleSort();
31+
int arr[] = {46, 24, 33, 10, 2, 81, 50};
32+
33+
System.out.println("Unsorted array:");
34+
bubble_sort.printArray(arr);
35+
36+
System.out.println("Sorted array:");
37+
console.log(bubble_sort.printArray(arr));
38+
}
39+
}

0 commit comments

Comments
 (0)
0