human
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"
How can the above be solved in Python?
 Can you provide a code to sort an array using Bubble Sort without using loops and the other steps required?
assistant
#Here is code to sort an array using Bubble Sort without using loops and the other steps required.
 def bubble_sort ( ar ) :
	if len ( ar ) <= 1 :
		return ar
	if len ( ar ) == 2 :
		return ar if ar [ 0 ] < ar [ 1 ] else [ ar [ 1 ] , ar [ 0 ] ]
	a , b = ar [ 0 ] , ar [ 1 ]
	bs = ar [ 2 : ]
	res = [ ]
	if a < b :
		res = [ a ] + bubble_sort ( [ b ] + bs )
	else :
		res = [ b ] + bubble_sort ( [ a ] + bs )
	return bubble_sort ( res [ : - 1 ] ) + res [ - 1 : ]
arr = [ 1 , 3 , 4 , 5 , 6 , 2 ]
res = bubble_sort ( arr )
print ( * res )