Sorting a non-copy array

huangapple go评论100阅读模式
英文:

Sorting a non-copy array

问题

I am working on my lab #5 and it's about sorting algorithms such as Quick Sort, Heap Sort, and Merge Sort. I am somewhat done besides measuring the execution time and some other stuff, and I ran into an issue.

in this code:

  int *arr = fill();
  int size = 10;

  std::cout << "\nMERGE SORT: \n";
  std::cout << "array#1: ";
  print_arr(arr, size);

  merge_sort(arr, size);
  std::cout << "array#2: ";
  print_arr(arr, size);

  std::cout << "\nQUICK SORT: \n";
  std::cout << "array#1: ";
  print_arr(arr, size);

  quick_sort(arr, size);

  std::cout << "array#2: ";
  print_arr(arr, size);

  std::cout << "\nHEAP SORT: \n";
  std::cout << "array#1: ";
  print_arr(arr, size);

  heap_sort(arr, size);

  std::cout << "array#2: ";
  print_arr(arr, size);

  delete[] arr;

I am passing my array to all of the sorting methods, and what's happening is that the array gets sorted with Merge Sort, which works fine but then that sorted array is being passed into Quick Sort and Heap Sort.

I did some research and I found that I can use std::copy() to create copies of the array and then pass them separately as copies, but I know my professor is not a "fan" of the STL and usually tells us not to use it if it's not our last resort.

My restrictions for this assignment are some prototypes such as this function:

const int MAX = 100000;
int* fill() {
    int* arr = new int[10]; // create a new array of size 100

    // set the seed for the random number generator
    std::srand(static_cast<unsigned int>(std::time(nullptr)));

    for (int i = 0; i < 10; i++) { //!change to MAX
        arr[i] = std::rand() % 10 + 1; // fill the array with random numbers from 1 to 100
    }
    return arr;
}

and

void heap_sort(int *arr, int size);
void merge_sort(int *arr, int size);
void quick_sort(int *arr, int size);

So far all the algorithms work since I tested them separately, but when I run them all together, it sorts the already sorted array. This affects the QS algorithm.

I know it looks like spaghetti code right now, function pointers are on the TODO list.

英文:

I am working on my lab #5 and it's about sorting algorithms such as Quick Sort, Heap Sort and Merge Sort. I am somewhat done besides measuring the execution time and some other stuff, and I ran into an issue.

in this code:

  int *arr = fill();
  int size = 10;

  std::cout &lt;&lt; &quot;\nMERGE SORT: \n&quot;;
  std::cout &lt;&lt; &quot;array#1: &quot;;
  print_arr(arr, size);

  merge_sort(arr,size);
  std::cout &lt;&lt; &quot;array#2: &quot;;
  print_arr(arr,size);

  std::cout &lt;&lt; &quot;\nQUICK SORT: \n&quot;;
  std::cout &lt;&lt; &quot;array#1: &quot;;
  print_arr(arr, size);

  quick_sort(arr, size);

  std::cout &lt;&lt; &quot;array#2: &quot;;
  print_arr(arr, size);

  std::cout &lt;&lt; &quot;\nHEAP SORT: \n&quot;;
  std::cout &lt;&lt; &quot;array#1: &quot;;
  print_arr(arr, size);

  heap_sort(arr, size);

  std::cout &lt;&lt; &quot;array#2: &quot;;
  print_arr(arr, size);

  delete[] arr;

I am passing my array to all of the sorting methods and what's happening is that the array get's sorted with Merge Sort, which works fine but then that sorted array is being passed into Quick Sort and Heap Sort.

I did some research and I found that I can use std::copy() to create copies of the array and them pass them separatley as copies, but I know my professor is not a "fan" of the STL and usually tells us to not use it if it's not our last resort.

My restrictions for this assigment are some prototypes such as this function:

const int MAX = 100000;
int* fill() {
    int* arr = new int[10]; // create a new array of size 100

    // set the seed for the random number generator
    std::srand(static_cast&lt;unsigned int&gt;(std::time(nullptr)));

    for (int i = 0; i &lt; 10; i++) { //!change to MAX
        arr[i] = std::rand() % 10 + 1; // fill the array with random numbers from 1 to 100
    }
    return arr;
}

and

void heap_sort(int *arr, int size);
void merge_sort(int *arr, int size)
void qucik_sort(int *arr, int size)

So far all the algorithms work since I tested them separatley, but when I run them all together it sorts the already sorted array. This affects the QS algorithm.

I know it looks like spaghetti code right now, function pointers are on the TODO list.

答案1

得分: 3

你需要生成数组,然后复制它。你可以这样做:

int* copy_array(int* arr, std::size_t n) {
    int* new_arr = new int[n];

    for (std::size_t i = 0; i < n; i++) {
        new_arr[i] = arr[i];
    }

    return new_arr;
}

然后,在对数组进行排序之前,只需调用此函数以创建您需要的多个副本。

英文:

You need to generate the array and then copy it. You might do something like:

int* copy_array(int* arr, std::size_t n) {
    int* new_arr = new int[size];

    for (std::size_t i = 0; i &lt; n; i++) {
        new_arr[i] = arr[i];
    }

    return new_arr;
}

Then simply call this to create as many copies of your array as you need before sorting it.

答案2

得分: 1

你可以像这样手动复制你的数组:

for (int i = 0; i < size; i++) {
    destination[i] = src[i];
}

或者你可以使用 memcpy。以下是链接中的示例:

/* memcpy示例 */
#include <stdio.h>
#include <string.h>

struct {
  char name[40];
  int age;
} person, person_copy;

int main ()
{
  char myname[] = "Pierre de Fermat";

  /* 使用memcpy复制字符串: */
  memcpy (person.name, myname, strlen(myname)+1);
  person.age = 46;

  /* 使用memcpy复制结构体: */
  memcpy (&person_copy, &person, sizeof(person));

  printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age );

  return 0;
}

当然,你也可以使用 std::copy,尽管由于你的教授的偏好,这不是一个选项。

英文:

You could manually copy your array like this:

for (int i = 0; i &lt; size; i++) {
    destination[i] = src[i];
}

Or you could use memcpy. Example from the link:

/* memcpy example */
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

struct {
  char name[40];
  int age;
} person, person_copy;

int main ()
{
  char myname[] = &quot;Pierre de Fermat&quot;;

  /* using memcpy to copy string: */
  memcpy ( person.name, myname, strlen(myname)+1 );
  person.age = 46;

  /* using memcpy to copy structure: */
  memcpy ( &amp;person_copy, &amp;person, sizeof(person) );

  printf (&quot;person_copy: %s, %d \n&quot;, person_copy.name, person_copy.age );

  return 0;
}

and, of course you can use std::copy as well, even though it's not an option due to the preferences of your professor.

huangapple
  • 本文由 发表于 2023年5月21日 03:02:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定