Open In App

Sort an array using Bubble Sort without using loops

Last Updated : 22 Jun, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr[] consisting of N integers, the task is to sort the given array by using Bubble Sort without using loops.

Examples:

Input: arr[] = {1, 3, 4, 2, 5}
Output: 1 2 3 4 5

Input: arr[] = {1, 3, 4, 2}
Output: 1 2 3 4

Approach: The idea to implement Bubble Sort without using loops is based on the following observations:

  • The sorting algorithm of Bubble Sort performs the following steps:
  • It can be observed that in every N – 1 iteration, the largest element over the range [0, N – 1 – i] shifts to the position (N – 1 – i) for every i over the range [0, N – 1].
  • Therefore, the idea is to use recursion to avoid loops.

Follow the steps below to solve the problem:

  • Consider the following base cases: 
    • If the array contains a single element, simply print the array.
    • If the array contains two elements, swap the pair of elements (if required) to obtain a sorted sequence of array elements. Print the sorted array.
  • Store the first two elements from the current array in variables, say a and b.
  • Initialize an array, say bs[], to store the remaining array elements.
  • Place the smaller value among a and b at the front of the current array.
  • Recursively repeat the above steps with a new array formed by appending bs[] to the end of the larger value among a and b.
  • Store the sorted list returned in a variable, say res[].
  • Now, recursively repeat the above steps with a new array formed by appending res[] (excluding the last element) to the end of res[res.size() – 1] ( the last element ).
  • After completing the above steps, print the returned list.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to implement bubble
// sort without using loops
vector<int> bubble_sort(vector<int> ar)
{
 
  // Base Case: If array
  // contains a single element
  if (ar.size() <= 1)
    return ar;
 
  // Base Case: If array
  // contains two elements
  if (ar.size() == 2){
    if(ar[0] < ar[1])
      return ar;
    else
      return {ar[1], ar[0]};
  }
 
  // Store the first two elements
  // of the list in variables a and b
  int a = ar[0];
  int b = ar[1];
 
  // Store remaining elements
  // in the list bs
  vector<int> bs;
  for(int i = 2; i < ar.size(); i++)
    bs.push_back(ar[i]);
 
  // Store the list after
  // each recursive call
  vector<int> res;
   
  // If a < b
  if (a < b){
    vector<int> temp1;
    temp1.push_back(b);
    for(int i = 0; i < bs.size(); i++)
      temp1.push_back(bs[i]);
    vector<int> v = bubble_sort(temp1);
    v.insert(v.begin(), a);
    res = v;
  }
 
  // Otherwise, if b >= a
  else{
    vector<int> temp1;
    temp1.push_back(a);
    for(int i = 0; i < bs.size(); i++)
      temp1.push_back(bs[i]);
    vector<int> v = bubble_sort(temp1);
    v.insert(v.begin(), b);
    res = v;
  }
 
  // Recursively call for the list
  // less than the last element and
  // and return the newly formed list
  vector<int> pass;
  for(int i = 0; i < res.size() - 1; i++)
    pass.push_back(res[i]);
 
  vector<int> ans = bubble_sort(pass);
  ans.push_back(res[res.size() - 1]);
  return ans;
 
}
 
// Driver Code
int main()
{
 
  vector<int> arr{1, 3, 4, 5, 6, 2};
  vector<int> res = bubble_sort(arr);
 
  // Print the array
  for(int i = 0; i < res.size(); i++)
    cout << res[i] << " ";
}
 
// This code is contributed by ipg2016107.


Java

Python3

C#

Javascript

Output:

1 2 3 4 5 6

Time Complexity: O(N2)
Auxiliary Space: O(N)



Next Article

Similar Reads