📜  生成范围 [L, R] 中元素的随机排列(分而治之)

📅  最后修改于: 2021-09-16 11:07:22             🧑  作者: Mango

给定范围[L, R]其中L ≤ R ,任务是生成序列[L, L + 1, L + 2, …, R]的随机排列。

例子:

方法:我们将使用分而治之算法作为我们的解决方案。我们将创建一个全局向量,用于存储由递归函数generate_random_permutation()生成的随机数。
我们将传递两个参数L (start) 和R (end) 给我们的递归函数。在函数内部,它将调用另一个函数give_random_number ,该函数将返回XY之间的数字。让我们称之为N 。我们将这个随机数存储在我们的向量中,然后我们将递归调用generate_random_permutation()函数,范围[L, N – 1]和范围[N + 1, R]
如果L大于R,那么我们将在不执行任何任务的情况下从函数返回,因为这是我们的基本条件。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// To store the random permutation
vector permutation;
  
// Utility function to print
// the generated permutation
void printPermutation()
{
    for (auto i : permutation)
        cout << i << " ";
}
  
// Function to return a random
// number between x and y
int give_random_number(int l, int r)
{
    int x = rand() % (r - l + 1) + l;
    return x;
}
  
// Recursive function to generate
// the random permutation
void generate_random_permutation(int l, int r)
{
  
    // Base condition
    if (l > r)
        return;
  
    // Random number returned from the function
    int n = give_random_number(l, r);
  
    // Inserting random number in vector
    permutation.push_back(n);
  
    // Recursion call for [l, n - 1]
    generate_random_permutation(l, n - 1);
  
    // Recursion call for [n + 1, r]
    generate_random_permutation(n + 1, r);
}
  
// Driver code
int main()
{
    int l = 5;
    int r = 15;
  
    // Generate permutation
    generate_random_permutation(l, r);
  
    // Print the generated permutation
    printPermutation();
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.Vector;
  
class GFG
{
  
  
// To store the random permutation
static Vector permutation = new Vector<>();
  
// Utility function to print
// the generated permutation
static void printPermutation()
{
    permutation.stream().forEach((i) -> 
    {
        System.out.print(i+" ");
    });
}
  
// Function to return a random
// number between x and y
static int give_random_number(int l, int r)
{
    int x = (int) (Math.random()% (r - l + 1) + l);
    return x;
}
  
// Recursive function to generate
// the random permutation
static void generate_random_permutation(int l, int r)
{
  
    // Base condition
    if (l > r)
        return;
  
    // Random number returned from the function
    int n = give_random_number(l, r);
  
    // Inserting random number in vector
    permutation.add(n);
  
    // Recursion call for [l, n - 1]
    generate_random_permutation(l, n - 1);
  
    // Recursion call for [n + 1, r]
    generate_random_permutation(n + 1, r);
}
  
// Driver code
public static void main(String[] args)
{
    int l = 5;
    int r = 15;
  
    // Generate permutation
    generate_random_permutation(l, r);
  
    // Print the generated permutation
    printPermutation();
}
}
  
// This code has been contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
import random
  
# To store the random permutation
permutation = []
  
# Utility function to print
# the generated permutation
def printPermutation() :
  
    for i in permutation:
        print(i, end = " ")
  
# Function to return a random
# number between x and y
def give_random_number(l, r) :
  
    x = random.randint(l, r)
    return x
  
# Recursive function to generate
# the random permutation
def generate_random_permutation(l, r) :
  
    # Base condition
    if (l > r) :
        return
  
    # Random number returned from the function
    n = give_random_number(l, r)
  
    # Inserting random number in vector
    permutation.append(n)
  
    # Recursion call for [l, n - 1]
    generate_random_permutation(l, n - 1)
  
    # Recursion call for [n + 1, r]
    generate_random_permutation(n + 1, r)
  
# Driver code
l = 5
r = 15
  
# Generate permutation
generate_random_permutation(l, r)
  
# Print the generated permutation
printPermutation()
  
# This code is contributed by ihritik


C#
// C# implementation of the approach 
using System;
using System.Collections.Generic;
  
class GFG
{
  
  
// To store the random permutation
static List permutation = new List();
  
// Utility function to print
// the generated permutation
static void printPermutation()
{
    foreach(int i in permutation)
        Console.Write(i+" ");
}
  
// Function to return a random
// number between x and y
static int give_random_number(int l, int r)
{
    Random rnd = new Random();
    int num = rnd.Next(l, r);
    int x = (int) (num % (r - l + 1) + l);
    return x;
}
  
// Recursive function to generate
// the random permutation
static void generate_random_permutation(int l, int r)
{
  
    // Base condition
    if (l > r)
        return;
  
    // Random number returned from the function
    int n = give_random_number(l, r);
  
    // Inserting random number in vector
    permutation.Add(n);
  
    // Recursion call for [l, n - 1]
    generate_random_permutation(l, n - 1);
  
    // Recursion call for [n + 1, r]
    generate_random_permutation(n + 1, r);
}
  
// Driver code
public static void Main(String[] args)
{
    int l = 5;
    int r = 15;
  
    // Generate permutation
    generate_random_permutation(l, r);
  
    // Print the generated permutation
    printPermutation();
}
}
  
/* This code contributed by PrinciRaj1992 */


PHP
 $r)
        return;
  
    // Random number returned from the function
    $n = give_random_number($l, $r);
  
    // Inserting random number in vector
    array_push($permutation, $n);
  
    // Recursion call for [l, n - 1]
    generate_random_permutation($l, $n - 1);
  
    // Recursion call for [n + 1, r]
    generate_random_permutation($n + 1, $r);
}
  
// Driver code
$l = 5;
$r = 15;
  
// Generate permutation
generate_random_permutation($l, $r);
  
// Print the generated permutation
printPermutation();
  
// This code is contributed by mits
?>


输出:
11 9 6 5 8 7 10 12 13 15 14

时间复杂度: O(n)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程