📜  使用冒泡排序对数组排序而不使用循环

📅  最后修改于: 2021-05-17 06:35:26             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是通过使用冒泡排序对给定的数组进行排序而不使用循环。

例子:

方法:在不使用循环的情况下实现气泡排序的想法是基于以下观察结果:

  • Bubble Sort的排序算法执行以下步骤:
    • 外循环遍历给定数组(N – 1)次。
    • 如果arr [i]> arr [i + 1] ,则内部循环遍历数组并交换两个相邻元素,范围为[0,N – 1]的每个i
  • 可以观察到,在每一个N – 1迭代中,在范围中的最大元素[0,N – 1 – i]于转换到位置(N – 1 – i)用于每个i在范围[0,N – 1 ]
  • 因此,我们的想法是使用递归来避免循环。

请按照以下步骤解决问题:

  • 考虑以下基本情况:
    • 如果数组包含单个元素,只需打印该数组。
    • 如果数组包含两个元素,则交换一对元素(如果需要)以获得数组元素的排序序列。打印排序后的数组。
  • 将当前数组中的前两个元素存储在变量ab中
  • 初始化一个数组,例如bs [] ,以存储其余的数组元素。
  • 将较小的值放在当前数组前面的ab之间。
  • 通过将bs []附加到ab中较大值的末尾而形成的新数组,以递归方式重复上述步骤。
  • 将返回的排序列表存储在变量中,例如res []
  • 现在,以一个新数组递归地重复上述步骤,该数组通过将res [] (不包括最后一个元素)附加到res [res.size()– 1](最后一个元素)的末尾而形成。
  • 完成上述步骤后,打印返回的列表。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to implement bubble
// sort without using loops
vector bubble_sort(vector 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 bs;
  for(int i = 2; i < ar.size(); i++)
    bs.push_back(ar[i]);
 
  // Store the list after
  // each recursive call
  vector res;
   
  // If a < b
  if (a < b){
    vector temp1;
    temp1.push_back(b);
    for(int i = 0; i < bs.size(); i++)
      temp1.push_back(bs[i]);
    vector v = bubble_sort(temp1);
    v.insert(v.begin(), a);
    res = v;
  }
 
  // Otherwise, if b >= a
  else{
    vector temp1;
    temp1.push_back(a);
    for(int i = 0; i < bs.size(); i++)
      temp1.push_back(bs[i]);
    vector 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 pass;
  for(int i = 0; i < res.size() - 1; i++)
    pass.push_back(res[i]);
 
  vector ans = bubble_sort(pass);
  ans.push_back(res[res.size() - 1]);
  return ans;
 
}
 
// Driver Code
int main()
{
 
  vector arr{1, 3, 4, 5, 6, 2};
  vector 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
// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to implement bubble
  // sort without using loops
  static ArrayList bubble_sort(ArrayList 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.get(0) < ar.get(1))
        return ar;
      else
        return new ArrayList(
        Arrays.asList(ar.get(1), ar.get(0)));
    }
 
    // Store the first two elements
    // of the list in variables a and b
    int a = ar.get(0);
    int b = ar.get(1);
 
    // Store remaining elements
    // in the list bs
    ArrayList bs = new ArrayList<>();
    for (int i = 2; i < ar.size(); i++)
      bs.add(ar.get(i));
 
    // Store the list after
    // each recursive call
    ArrayList res = new ArrayList<>();
 
    // If a < b
    if (a < b) {
      ArrayList temp1 = new ArrayList<>();
      temp1.add(b);
      for (int i = 0; i < bs.size(); i++)
        temp1.add(bs.get(i));
 
      ArrayList v = bubble_sort(temp1);
      v.add(0, a);
      res = v;
    }
 
    // Otherwise, if b >= a
    else {
      ArrayList temp1 = new ArrayList<>();
      temp1.add(a);
      for (int i = 0; i < bs.size(); i++)
        temp1.add(bs.get(i));
 
      ArrayList v = bubble_sort(temp1);
      v.add(0, b);
      res = v;
    }
 
    // Recursively call for the list
    // less than the last element and
    // and return the newly formed list
    ArrayList pass = new ArrayList<>();
    for (int i = 0; i < res.size() - 1; i++)
      pass.add(res.get(i));
 
    ArrayList ans = bubble_sort(pass);
    ans.add(res.get(res.size() - 1));
    return ans;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    ArrayList arr = new ArrayList(
      Arrays.asList(1, 3, 4, 5, 6, 2));
    ArrayList res = bubble_sort(arr);
 
    // Print the array
    for (int i = 0; i < res.size(); i++)
      System.out.print(res.get(i) + " ");
  }
}
 
// This code is contributed by Kingash.


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


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to implement bubble
// sort without using loops
static List bubble_sort(List ar)
{
     
    // Base Case: If array
    // contains a single element
    List temp = new List();
     
    if (ar.Count <= 1)
        return ar;
 
    // Base Case: If array
    // contains two elements
    if (ar.Count == 2)
    {
        if (ar[0] < ar[1])
            return ar;
        else
        {
            temp.Add(ar[1]);
            temp.Add(ar[0]);
            return temp;
        }
    }
 
    // 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
    List bs = new List();
    for(int i = 2; i < ar.Count; i++)
        bs.Add(ar[i]);
 
    // Store the list after
    // each recursive call
    List res = new List();
 
    // If a < b
    if (a < b)
    {
        List temp1 = new List();
        temp1.Add(b);
         
        for(int i = 0; i < bs.Count; i++)
            temp1.Add(bs[i]);
             
        List v = bubble_sort(temp1);
        v.Insert(0, a);
        res = v;
    }
 
    // Otherwise, if b >= a
    else
    {
        List temp1 = new List();
        temp1.Add(a);
         
        for(int i = 0; i < bs.Count; i++)
            temp1.Add(bs[i]);
             
        List v = bubble_sort(temp1);
        v.Insert(0, b);
        res = v;
    }
 
    // Recursively call for the list
    // less than the last element and
    // and return the newly formed list
    List pass = new List();
    for(int i = 0; i < res.Count - 1; i++)
        pass.Add(res[i]);
 
    List ans = bubble_sort(pass);
    ans.Add(res[res.Count - 1]);
    return ans;
}
 
// Driver Code
public static void Main()
{
    List arr = new List{ 1, 3, 4, 5, 6, 2 };
    List res = bubble_sort(arr);
 
    // Print the array
    for(int i = 0; i < res.Count; i++)
        Console.Write(res[i] + " ");
}
}
 
// This code is contributed by ukasp


输出:

1 2 3 4 5 6

时间复杂度: O(N 2 )
辅助空间: O(N)