【问题标题】:How to make an array generate its elements randomly如何使数组随机生成其元素
【发布时间】:2021-12-31 18:05:34
【问题描述】:

基本上我需要将数组排序的执行时间与很多元素(或多或少500k个元素)进行比较,所以我想知道是否有办法让数组拿出自己的元素 https://onlinegdb.com/Hp6MkbCeM

int main()
{
    int i,n;
    printf("Enter size of array:\n");
    scanf("%d",&n);

    //Initialize array
    int arr[n];

    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }

    printf("The current array is:\n");

    double time_spent = 0.0;//starting clock
    clock_t begin = clock();
    int temp = 0;
    //Calculate length of array arr
    int length = sizeof(arr)/sizeof(arr[0]);

    for (int i = 0; i < length; i++) {
        printf("%d ", arr[i]);
    }

    //Sort the array in ascending order
    for (i = 0; i < length; i++)
    {
        for (int j = i+1; j < length; j++)
        {
           if(arr[i] > arr[j])
           {
               temp = arr[i];
               arr[i] = arr[j];
               arr[j] = temp;
           }
        }
    }

    printf("\n");

    //Displaying elements of array after sorting
    printf("Elements of array sorted in ascending order: \n");
    for (int i = 0; i < length; i++)
    {
        printf("%d ", arr[i]);
    }

    clock_t end = clock();
    time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
    printf("\nThe elapsed time is %f seconds", time_spent);

    return 0;
}

【问题讨论】:

  • 不行,你需要编写一个循环调用rand()来分配每个元素。
  • ... 或从文件中读取它们,或从诸如/dev/random 之类的设备中读取它们。如果您希望每次试验都使用相同的“随机”数字序列,那么 rand() 使用预先选择的种子或从预先生成的数字文件中读取是可行的方法。
  • 如果你想测试随机兽人名字,I have just the thing; orcish(string, sizeof string).

标签: c


【解决方案1】:

这听起来很简单,所以我使用 Ada 编程语言实现了一个程序。此解决方案应为您的 C++ 解决方案提供提示,同时让您有机会发现如何在 C++ 中实现您的答案。

我认为一个非常大的数组的输出不会有用,因为它很难读取和评估不同初始数据顺序之间的差异,所以我只输出每次填充数组时对数组进行排序所需的经过时间随机值。

Ada源码为:

with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Containers.Generic_Constrained_Array_Sort;
with Ada.Numerics.Discrete_Random;
with Ada.Calendar; use Ada.Calendar;

procedure Main is

   -- Define the range of data used in the array to be sorted
   -- This is also the index range for the array.

   type Data_Range is range 0 .. 500_000;

   -- Make an instance of the generic discrete random number
   -- generator to produce values in the range of Data_Range

   package Rand_Data is new Ada.Numerics.Discrete_Random (Data_Range);
   use Rand_Data;

   -- Define the array type to be sorted

   type Data_Array is array (Data_Range) of Data_Range;

   -- Make an instance of the generic constrained array sort procedure
   -- customized to sort instances of Data_Array

   procedure my_sort is new Ada.Containers.Generic_Constrained_Array_Sort
     (Index_Type => Data_Range, Element_Type => Data_Range,
      Array_Type => Data_Array);

   -- Define the random number generator seed

   Seed : Generator;

   -- Create a procedure to fill the array with random values in the range
   -- of Data_Range

   procedure fill_array (Arr : out Data_Array) is
   begin
      for I in Arr'Range loop
         Arr (I) := Random (Seed);
      end loop;
   end fill_array;

   -- Create a procedure to time the sorting of the array and output the
   -- elapsed time of the sort operation

   procedure Time_Sort (Arr : in out Data_Array) is
      T1 : Time := Clock;
      T2 : Time;
   begin
      my_sort (Arr);
      T2 := Clock;
      Put_Line ("Sort time:" & Duration'Image (T2 - T1) & " seconds.");
   end Time_Sort;

   -- Create an instance of type Data_Array

   The_Array : Data_Array;

begin

   -- Set the random number seed based upon the value of the system clock

   Reset (Seed);

   -- Fill and sort the array 20 times

   for I in 1 .. 20 loop
      fill_array (The_Array);
      Time_Sort (The_Array);
   end loop;

end Main;

程序执行示例的输出是:

Sort time: 0.133331800 seconds.
Sort time: 0.133276200 seconds.
Sort time: 0.139969700 seconds.
Sort time: 0.132815500 seconds.
Sort time: 0.134336500 seconds.
Sort time: 0.135018800 seconds.
Sort time: 0.132183200 seconds.
Sort time: 0.134873900 seconds.
Sort time: 0.135876900 seconds.
Sort time: 0.136931200 seconds.
Sort time: 0.134980000 seconds.
Sort time: 0.137260000 seconds.
Sort time: 0.132401800 seconds.
Sort time: 0.137554000 seconds.
Sort time: 0.133554800 seconds.
Sort time: 0.134009500 seconds.
Sort time: 0.132895800 seconds.
Sort time: 0.134184000 seconds.
Sort time: 0.130988900 seconds.
Sort time: 0.132368300 seconds.

【讨论】:

    猜你喜欢
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    相关资源
    最近更新 更多