1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#include <iostream>
using namespace std;
void FillArray(int array[], int size);
void Duplicate(int first[], int second[], int size);
void Print(int array[], int size);
int main() {
int size = 42;
int numbers[42] = {0}, spare[42] = {0};
FillArray(numbers, size);
Duplicate(numbers, spare, size);
cout << "numbers[]:\n";
Print(numbers, size);
cout << "\nspare[]:\n";
Print(spare, size);
return 0;
}
void FillArray(int array[], int size) {
int parent = 0, son1 = 0, son2 = 0, smaller = 0;;
for (int i = 1; i < size; i++) {
array[i] = rand()%100+1;
parent = i;
while (parent / 2 > 0 && array[parent] < array[parent / 2]) {
swap(array[parent], array[parent / 2]);
parent = parent / 2;
}
}
}
void Duplicate(int first[], int second[], int size) {
for (int i = 0; i < size; i++) {
second[i] = first[i];
}
}
void Print(int array[], int size) {
int parent = 1, son1 = 0, son2 = 0, smaller = 0;
while (size > 0) {
cout << array[1] << " ";
array[1] = array[size];
size--;
int done = 0;
while (done = 0) {
son1 = parent * 2;
son2 = son1 + 1;
if (son1 > size) done = 1;
else if (son1 == size) {
if (array[parent] > array[son1]) {
swap(array[parent], array[son1]);
done = 1;
}
}
else {
smaller = son1;
if (array[son1] > array[son2])
smaller = son2;
if (array[parent] > array[smaller])
swap(array[parent], array[smaller]);
done = 1;
}
}
}
}
|