英文:
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 << "\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 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<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 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 < 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 < size; i++) {
destination[i] = src[i];
}
Or you could use memcpy. Example from the link:
/* memcpy example */
#include <stdio.h>
#include <string.h>
struct {
char name[40];
int age;
} person, person_copy;
int main ()
{
char myname[] = "Pierre de Fermat";
/* using memcpy to copy string: */
memcpy ( person.name, myname, strlen(myname)+1 );
person.age = 46;
/* using memcpy to copy structure: */
memcpy ( &person_copy, &person, sizeof(person) );
printf ("person_copy: %s, %d \n", 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论