Skip to content

Instantly share code, notes, and snippets.

@yurii-litvinov
Created November 27, 2019 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yurii-litvinov/a9274c9aca9bfb1607701bbe1974612e to your computer and use it in GitHub Desktop.
Save yurii-litvinov/a9274c9aca9bfb1607701bbe1974612e to your computer and use it in GitHub Desktop.
Решение задачи 1 из теста
#include <stdio.h>
#include <malloc.h>
bool isEven(int num)
{
return num % 2 == 0;
}
void insertionSort(int num, int mass[])
{
int newElement = 0;
int location = 0;
for (int i = 1; i < num; i++)
{
if (!isEven(mass[i]))
{
continue;
}
newElement = mass[i];
location = i - 1;
int previousLocation = i;
while (location >= 0)
{
if (isEven(mass[location]))
{
if (mass[location] <= newElement)
{
break;
}
mass[previousLocation] = mass[location];
previousLocation = location;
}
location = location - 1;
}
mass[previousLocation] = newElement;
}
}
void resultOfSort()
{
int Size = 0;
printf("Enter the size of the array: ");
scanf_s("%d", &Size);
//выделение памяти под массив
int* mass;
mass = (int*)malloc(Size * sizeof(int));
//ввод элементов массива
for (int i = 0; i < Size; i++)
{
printf("Input the array elements: ");
scanf_s("%d", &mass[i]);
}
//сортировка вставками
insertionSort(Size, mass);
//вывод отсортированного массива на экран
printf("Sorted array:\n");
for (int i = 0; i < Size; i++)
{
printf("%d ", mass[i]);
}
//освобождение памяти
free(mass);
}
bool test(int len, int arrayOfNumbers[], int sortedArray[])
{
insertionSort(len, arrayOfNumbers);
for (int i = 0; i < len; ++i)
{
if (arrayOfNumbers[i] != sortedArray[i])
{
return false;
}
}
}
int main()
{
int arrayFirst[] = { 5, 4, 3, 2, 1 };
int sortedArrayFirst[] = { 5, 2, 3, 4, 1 };
if (!test(5, arrayFirst, sortedArrayFirst))
{
printf("Error");
return 1;
}
resultOfSort();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment