Skip to content

Commit 27d7926

Browse files
Recursion Day 6 is completed
1 parent 095a677 commit 27d7926

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

Recursion/BubbleSort.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Recursion;
2+
3+
import java.util.Arrays;
4+
5+
public class BubbleSort {
6+
static void sort(int arr[],int row,int col) {
7+
if(row == 0)
8+
return;
9+
if(row>col) {
10+
if(arr[col]>arr[col+1]) {
11+
int temp = arr[col];
12+
arr[col] = arr[col+1];
13+
arr[col+1] = temp;
14+
}
15+
sort(arr, row, col+1);
16+
} else
17+
sort(arr, row-1, 0);
18+
}
19+
public static void main(String[] args) {
20+
int arr[] = {4, 1, 3, 9, 7 },row = arr.length-1,col=0;
21+
sort(arr,row,col);
22+
System.out.println(Arrays.toString(arr));
23+
}
24+
}

Recursion/SelectionSort.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Recursion;
2+
3+
import java.util.Arrays;
4+
5+
public class SelectionSort {
6+
static void sort(int arr[],int row,int col,int max) {
7+
if(row == 0)
8+
return;
9+
if(row>col) {
10+
if(arr[col]>arr[max]) {
11+
max = col;
12+
}
13+
sort(arr, row, col+1,max);
14+
} else{
15+
int temp = arr[col-1];
16+
arr[col-1] = arr[max];
17+
arr[max] = temp;
18+
sort(arr, row-1, 0,0);
19+
}
20+
}
21+
public static void main(String[] args) {
22+
int arr[] = {4, 1, 3, 9, 7 },row = arr.length-1,col=0,max = 0;
23+
sort(arr,row,col,max);
24+
System.out.println(Arrays.toString(arr));
25+
}
26+
}

0 commit comments

Comments
 (0)