Sort an array using Bubble Sort without using loops | Function to implement bubble sort without using loops ; Base Case : If array contains a single element ; Base Case : If array contains two elements ; Store the first two elements of the list in variables a and b ; Store remaining elements in the list bs ; Store the list after each recursive call ; If a < b ; Otherwise , if b >= a ; Recursively call for the list less than the last element and and return the newly formed list ; Driver Code ; Print the array
def bubble_sort ( ar ) : NEW_LINE INDENT if len ( ar ) <= 1 : NEW_LINE INDENT return ar NEW_LINE DEDENT if len ( ar ) == 2 : NEW_LINE INDENT return ar if ar [ 0 ] < ar [ 1 ] else [ ar [ 1 ] , ar [ 0 ] ] NEW_LINE DEDENT a , b = ar [ 0 ] , ar [ 1 ] NEW_LINE bs = ar [ 2 : ] NEW_LINE res = [ ] NEW_LINE if a < b : NEW_LINE INDENT res = [ a ] + bubble_sort ( [ b ] + bs ) NEW_LINE DEDENT else : NEW_LINE INDENT res = [ b ] + bubble_sort ( [ a ] + bs ) NEW_LINE DEDENT return bubble_sort ( res [ : - 1 ] ) + res [ - 1 : ] NEW_LINE DEDENT arr = [ 1 , 3 , 4 , 5 , 6 , 2 ] NEW_LINE res = bubble_sort ( arr ) NEW_LINE print ( * res ) NEW_LINE