diff --git a/sorting_algos/bubble_sort.cpp b/sorting_algos/bubble_sort.cpp index d110245..e2b00a9 100644 --- a/sorting_algos/bubble_sort.cpp +++ b/sorting_algos/bubble_sort.cpp @@ -1,3 +1,5 @@ +// recursive approach + void bubble_sort(int arr[],int size) { if (size == 0 || size == 1) @@ -12,4 +14,22 @@ void bubble_sort(int arr[],int size) } } bubble_sort(arr, size - 1); +} + +// itrative approach +void bubble_sort(int arr[],int size) +{ + for(int i = 0; i < size - 1 ; i++) + { + for(int j = 0 ; j < size - i - 1; j++) + { + if(arr[j] > arr[j + 1]) + { + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[i + j] = temp; + } + + } + } } \ No newline at end of file