TecHAyusH6476 / DSA-Prep-HacktoberFest22

DSA important questions in terms of interview and OA of product based companies.
MIT License
18 stars 105 forks source link

Recursive Bubble Sort in java #109

Closed cureius closed 2 years ago

cureius commented 2 years ago

Recursive_Bubble_Sort

`import java.util.Arrays;

public class recursion { static void bubbleSort(int arr[], int n) { if (n == 1) return ;

    int count = 0;
    for (int i=0; i<n-1; i++)
        if (arr[i] > arr[i+1])
        {
            // swap arr[i], arr[i+1]
            int temp = arr[i];
            arr[i] = arr[i+1];
            arr[i+1] = temp;
            count = count+1;
        }
    if (count == 0)
        return;
    bubbleSort(arr, n-1);
}
public static void main(String[] args)
{
    int arr[] = {64, 34, 25, 12, 22, 11, 90};

    bubbleSort(arr, arr.length);

    System.out.println("Sorted array : ");
    System.out.println(Arrays.toString(arr));
}

}`