Skip to content

Commit 268cc27

Browse files
authored
Merge pull request darpanjbora#121 from shanvijha30/BubbleAlgo
Recursive Bubble Sort Algorithm
2 parents 24b2a6a + 1a21d68 commit 268cc27

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Java program for recursive implementation
2+
// of Bubble sort
3+
4+
import java.util.Arrays;
5+
6+
public class GFG
7+
{
8+
// A function to implement bubble sort
9+
static void bubbleSort(int arr[], int n)
10+
{
11+
// Base case
12+
if (n == 1)
13+
return;
14+
15+
int count = 0;
16+
// One pass of bubble sort. After
17+
// this pass, the largest element
18+
// is moved (or bubbled) to end.
19+
for (int i=0; i<n-1; i++)
20+
if (arr[i] > arr[i+1])
21+
{
22+
// swap arr[i], arr[i+1]
23+
int temp = arr[i];
24+
arr[i] = arr[i+1];
25+
arr[i+1] = temp;
26+
count = count+1;
27+
}
28+
29+
// Check if any recursion happens or not
30+
// If any recursion is not happen then return
31+
if (count == 0)
32+
return;
33+
34+
// Largest element is fixed,
35+
// recur for remaining array
36+
bubbleSort(arr, n-1);
37+
}
38+
39+
// Driver Method
40+
public static void main(String[] args)
41+
{
42+
int arr[] = {64, 34, 25, 12, 22, 11, 90};
43+
44+
bubbleSort(arr, arr.length);
45+
46+
System.out.println("Sorted array : ");
47+
System.out.println(Arrays.toString(arr));
48+
}
49+
}
50+

0 commit comments

Comments
 (0)