微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我生成随机数的函数不会超过 500000

如何解决我生成随机数的函数不会超过 500000

我会直接解决我的问题。所以基本上我想做的是生成一组不同数量随机数。所以一个有 10,000,50,100,500,600,000 等等。然后我会使用快速排序对它们进行排序并将排序后的数组打印到屏幕上。此外,它运行所花费的时间也将被记录和打印。然而,我唯一遇到问题的部分是生成数组。由于某种原因,生成过去 500,000 个随机数不起作用并返回:


进程在 2.112 秒后退出,返回值为 3221225725

按任意键继续。 . . ([1]: https://i.stack.imgur.com/m83el.png)

这是我的代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void randNums(int array[],int range) {
    int i,num;
    for (i = 0; i < range; i++) {
        num = rand() % range;
        array[i] = num;
    }
}

//prints elements of given array
void display(int array[],int size) {
    int i;
    for (i = 0; i < size; i++) {
        printf("#%d. %d\n",i,array[i]);
    }
}

//displays time taken for sorting algorithm to run
void tiMetaken(char sortingalgo[],int size,clock_t start,clock_t end) {
    double seconds = end - start;
    double milliseconds = seconds / 1000;
    printf("Time taken for %s Sort to sort %d numbers was %f milliseconds or %f seconds",sortingalgo,size,milliseconds,seconds);       
}
 
//quick sort
void quickSort(int array[],int first,int last) {
    int i,j,pivot,temp;
    if (first < last) {
        pivot = first;
        i = first;
        j = last;
        while (i < j) {
            while (array[i] <= array[pivot] && i < last)
                i++;
            while (array[j] > array[pivot])
                j--;
            if (i < j) {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
        temp = array[pivot];
        array[pivot] = array[j];
        array[j] = temp;
        quickSort(array,first,j - 1);
        quickSort(array,j + 1,last);
    }
}

int main() { 
    int size = 600000;
    int myArray[size];
    time_t end,start;
    int first,last;

    randNums(myArray,size);    
    first = myArray[0];
    last = sizeof(myArray) / sizeof(myArray[0]);
    
    time(&start);
    quickSort(myArray,last);
    time(&end); 
    display(myArray,size);
    tiMetaken("Quick",start,end);

    return 0;
}

任何帮助将不胜感激,谢谢!

解决方法

这段代码中存在很多不太难解决的小错误。我将在这次重构和清理中尝试将其分解:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void randNums(int* array,int range) {
  // Declare iterator variables like `i` within the scope of the iterator.
  for (int i = 0; i < range; i++) {
    // No need for a single-use variable here,just assign directly.
    array[i] = rand () % range;
  }
}

void display(int* array,int size) {
  // for is not a function,it's a control flow mechanism,so
  // it is expressed as `for (...)` with a space. `for()` implies
  // it is a function,which it isn't.
  for (int i = 0; i < size; i++) {
    printf("#%d. %d\n",i,array[i]);
  }
}

void timeTaken(char* sortingAlgo,int size,clock_t start,clock_t end) {
  // Time calculation here needs to account for the fact that clock_t
  // does not use seconds as units,it must be converted
  // https://en.cppreference.com/w/c/chrono/clock_t

  printf(
    "Time taken for %s Sort to sort %d numbers was %.6f seconds",sortingAlgo,size,((double) (end - start)) / CLOCKS_PER_SEC
  );
}

void quickSort(int* array,int first,int last) {
   // Establish a guard condition. Rest of the function is no longer
   // nested in a control flow structure,so it simplifies the code.
   if (first >= last) {
     return;
   }

  int pivot = first;
  int i = first;
  int j = last;

  // Use `while (...)` as it's also a control flow structure.
  while (i < j) {
    // Adding space around operators improves clarity considerably. Unspaced
    // elements like `a->b()` are supposed to stand out and not be confused
    // with visually similar `a>>b()` which does something very different.
    while (array[i] <= array[pivot] && i < last) {
      i++;
    }

    // Use surrounding braces on all blocks,even single-line ones,as this
    // can avoid a whole class of errors caused by flawed assumptions.
    // while (...) { ... }
    while (array[j] > array[pivot]) {
      j--;
    }

    if (i < j) {
      int temp = array[i];
      array[i] = array[j];
      array[j] = temp;
    }
  }

  int temp = array[pivot];
  array[pivot] = array[j];
  array[j] = temp;

  quickSort(array,first,j - 1);
  quickSort(array,j + 1,last);
}

int main(int argc,char** argv) {
  int size = 600000;

  // If an argument was given...
  if (argc > 1) {
    // ...use that as the size parameter instead.
    size = atol(argv[1]);
  }

   // Allocate an array of sufficient size
  int* numbers = calloc(size,sizeof(int));

  randNums(numbers,size);

  // time_t has at best second-level precision,it's very inaccurate.
  // Use clock_t which gives far more fidelity.
  clock_t start = clock();

  // This function takes *offsets*,not values.
  quickSort(numbers,size - 1);

  clock_t end = clock();

  display(numbers,size);

  timeTaken("Quick",start,end);

  free(numbers);

  return 0;
}

这里的第一个错误是错误地调用了 quickSort()

// Represents first *value* in the array
first = myArray[0]; // Should be: 0

// Rough calculation of the size of the array,but this is off by one
last = sizeof(myArray)/sizeof(myArray[0]); // Should be: size - 1
    
quickSort(myArray,last);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。