Sort an array using Bubble Sort without using loops | C # program for the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static List < int > bubble_sort ( List < int > ar ) { List < int > temp = new List < int > ( ) ; if ( ar . Count <= 1 ) return ar ; if ( ar . Count == 2 ) { if ( ar [ 0 ] < ar [ 1 ] ) return ar ; else { temp . Add ( ar [ 1 ] ) ; temp . Add ( ar [ 0 ] ) ; return temp ; } } int a = ar [ 0 ] ; int b = ar [ 1 ] ; List < int > bs = new List < int > ( ) ; for ( int i = 2 ; i < ar . Count ; i ++ ) bs . Add ( ar [ i ] ) ; List < int > res = new List < int > ( ) ; if ( a < b ) { List < int > temp1 = new List < int > ( ) ; temp1 . Add ( b ) ; for ( int i = 0 ; i < bs . Count ; i ++ ) temp1 . Add ( bs [ i ] ) ; List < int > v = bubble_sort ( temp1 ) ; v . Insert ( 0 , a ) ; res = v ; } else { List < int > temp1 = new List < int > ( ) ; temp1 . Add ( a ) ; for ( int i = 0 ; i < bs . Count ; i ++ ) temp1 . Add ( bs [ i ] ) ; List < int > v = bubble_sort ( temp1 ) ; v . Insert ( 0 , b ) ; res = v ; } List < int > pass = new List < int > ( ) ; for ( int i = 0 ; i < res . Count - 1 ; i ++ ) pass . Add ( res [ i ] ) ; List < int > ans = bubble_sort ( pass ) ; ans . Add ( res [ res . Count - 1 ] ) ; return ans ; } public static void Main ( ) { List < int > arr = new List < int > { 1 , 3 , 4 , 5 , 6 , 2 } ; List < int > res = bubble_sort ( arr ) ; for ( int i = 0 ; i < res . Count ; i ++ ) Console . Write ( res [ i ] + " ▁ " ) ; } }