1.

Solve : How to Solve this Problem using Recursive Bubble Sort Algorithm ??

Answer»

Hey,

I tried to implement bubble sort using recursion and have got a response

Code: [Select]ArrayIndexOutOfBoundsException: 11
Code: [Select]public static int[] recBubSort(int []arr, int n){
if(n > arr.length-1){
return arr;
}

if(arr[n] > arr[n+1]){
swap(arr,n,n+1);
}
return recBubSort(arr,n+1);
}

public static void swap(int arr[], int minPos, int index) {
//System.out.println("SelectionSort SWAP...");
int temp = arr[minPos];
arr[minPos] = arr[index];
arr[index] = temp;
}











Because the if statement in line 6 is using an invalid index of n+1 when n=arr.length-1. May be change the if statement in line 2 to "if (n >= arr.length-1)"

Code: [Select]1 public static int[] recBubSort(int []arr, int n){
2 if(n > arr.length-1){
3 return arr;
4 }
5
6 if(arr[n] > arr[n+1]){
7 swap(arr,n,n+1);
8 }
9 return recBubSort(arr,n+1);
10 }
...



Discussion

No Comment Found