Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <malloc.h>
- #include <iostream>
- using namespace std;
- int* create_hand(int* A, int N); // ручной ввод
- int* create_rand(int* A, int N); // случайный ввод
- void insertSort(int* a, int size);// сортировка вставкой
- void print(int* A, int N);
- int main()
- {
- int n = 0, c = 1; // Размер массива
- int* a = NULL; // массив чисел
- cout << "Select the method for filling in the array:\n\t1)Enter the array manually;\n\t2)Array of random numbers;\n\t3)End\nYour select: ";
- cin >> c;
- if (c == 1)
- {
- cout << "\nEnter the size of the array: ";
- cin >> n;
- cout << "Enter your array: ";
- a = create_hand(a, n);
- insertSort(a, n);
- cout << "Enter your sorted array: ";
- print(a, n);
- }
- if (c == 2)
- {
- cout << "\nEnter the size of the array: ";
- cin >> n;
- a = create_rand(a, n);
- cout << "Your array: ";
- print(a, n);
- insertSort(a, n);
- cout << "Enter your sorted array: ";
- print(a, n);
- }
- if (c == 3) c = 0;
- cout << endl;
- return 0;
- }
- int* create_hand(int* A, int N)
- {
- A = (int*)malloc(N * sizeof(int));
- for (int i = 0; i < N; i++)
- cin >> A[i];
- return A;
- }
- int* create_rand(int* A, int N)
- {
- A = (int*)malloc(N * sizeof(int));
- for (int i = 0; i < N; ++i)
- A[i] = rand() % 100;
- return A;
- }
- void print(int* A, int N)
- {
- for (int i = 0; i < N; i++)
- cout << A[i] << " ";
- cout << endl;
- }
- void insertSort(int* a, int size)
- {
- int tmp;
- for (int i = 1, j; i < size; ++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