Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <malloc.h>
- #include <iostream>
- using namespace std;
- int* create_rand(int* A, int N, int N2); // создание массива А
- int* create_rand2(int* B, int N2); // создание массива В
- void insertSort(int* A, int N);// сортировка вставкой
- void print(int* A, int N);
- void unification(int* A, int* B, int N, int N2);// обьеденение в один массив
- int main()
- {
- int n = 0,n2=0;
- int* a = NULL;
- int* b = NULL;
- cout << "Enter the size of the array A: ";
- cin >> n;
- cout << "Enter the size of the array B: ";
- cin >> n2;
- cout << "\nEnter your array A: ";
- a = create_rand(a, n, n2);
- print(a, n);
- insertSort(a, n);
- cout << "Your sorted array: ";
- print(a, n);
- cout << "\nEnter your array B: ";
- b = create_rand2(b, n2);
- print(b, n2);
- insertSort(b, n2);
- cout << "Your sorted array: ";
- print(b, n2);
- unification(a, b, n, n2);
- cout << "\nYour unification array: ";
- print(a, n+n2);
- return 0;
- }
- int* create_rand(int* A, int N, int N2)
- {
- A = (int*)malloc((N + N2) * sizeof(int));
- for (int i = 0; i < N; ++i)
- A[i] = rand() % 100;
- return A;
- }
- int* create_rand2(int* B, int N2)
- {
- B = (int*)malloc(N2 * sizeof(int));
- for (int i = 0; i < N2; ++i)
- B[i] = rand() % 100;
- return B;
- }
- void print(int* A, int N)
- {
- for (int i = 0; i < N; i++)
- cout << A[i] << " ";
- cout << endl;
- }
- void unification(int* A, int* B, int N, int N2)
- {
- int i, k=0;
- for (i = N; i < N + N2; i++) {
- A[i] = B[k];
- k++;
- }
- insertSort(A, N + N2);
- }
- void insertSort(int* A, int N)
- {
- int tmp;
- for (int i = 1, j; i < N; ++i) // цикл проходов, i - номер прохода
- {
- tmp = A[i];
- for (j = i - 1; j >= 0 && A[j] > tmp; --j) // поиск места элемента в готовой последовательности
- A[j + 1] = A[j]; // сдвигаем элемент направо, пока не дошли
- A[j + 1] = tmp; // место найдено, вставить элемент
- }
- }
Add Comment
Please, Sign In to add comment