Skip to content

Conversation

@kamendov-maxim
Copy link
Owner

No description provided.

#include <stdlib.h>
#include <stdbool.h>

int partition(int array[], int leftElement, int rightElement);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Принято передавать массив в функцию по указателю
Размеры и индексы храним в size_t.
Везде, где мы не собираемся менять указатель, освобождать по нему дынные, нужно использовать type * const.

bool checkIfArrayIsSorted(int array[], int size);
bool testStarterForArrays(int testArray[], int answerArray[], int size);

int main()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main() -> main(void)

{
if (!test())
{
return 1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

коды ошибок и возврата выносим в define, если это не номер сломавшегося теста

}

printf("\nEnter the size of your array: ");
int size = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

смотри прошлую задачу (size_t)

scanf("%d", &size);
}

int *array = malloc(size * sizeof(int));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

нет явного приведения к (int *) перед malloc

Comment on lines 172 to 195
int binarySearch(int array[], int elementWeAreLookingFor, int leftEdge, int rightEdge)
{
while (leftEdge <= rightEdge)
{
int middleElement = leftEdge + (rightEdge - leftEdge) / 2;

if (array[middleElement] == elementWeAreLookingFor)
{
return middleElement;
}

if (array[middleElement] < elementWeAreLookingFor)
{
leftEdge = middleElement + 1;
}

else
{
rightEdge = middleElement - 1;
}
}

return -1;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Эта функция тут не нужна

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

и тесты для неё тоже


bool test(void)
{
if (!(testSwapFunction() * testPartitionFunction() * testSmartQuickSort()))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

убрать if, написать нормально через &&

Comment on lines 212 to 216
if (firstValue != 1 || secondValue != 0)
{
return false;
}
return true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

это if ну нужен

Comment on lines 250 to 255
if (!(testStarterForArrays(testArray1, answerArray1, 9) * testStarterForArrays(testArray2, answerArray2, 6) * testStarterForArrays(testArray3, answerArray3, 5) * testStarterForArrays(testArray4, answerArray4, 1)))
{
return false;
}

return true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

убрать if, написать через &&

Comment on lines 288 to 293
if (!(testStarterForArrays(testArray1, answerArray1, 5) * testStarterForArrays(testArray2, answerArray2, 5) * testStarterForArrays(testArray3, answerArray3, 1)))
{
return false;
}

return true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

убрать if, написать через &&


const bool test(void);

static void printArray(int *array, size_t size)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const int * const

printf("\n");
}

static void swap(int *firstValue, int *secondValue)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int * const

*secondValue = buffer;
}

static size_t partition(int *array, size_t leftElement, size_t rightElement)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int * const

return i;
}

static void insertSort(int array[], int leftElement, int rightElement)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int * const

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size_t для индексов

}
}

static void smartQuickSort(int array[], int leftElement, int rightElement)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int * const

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size_t для индексов

{
printf("Enter %lu number of your array: ", i + 1);
int currentNumber = 0;
scanf("%d", &currentNumber);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&array[i]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

убрать currentNumber

partition(testArray3, 0, 5);
partition(testArray4, 0, 1);

return testStarterForArrays(testArray1, answerArray1, 9) && testStarterForArrays(testArray2, answerArray2, 6) && testStarterForArrays(testArray3, answerArray3, 5) && testStarterForArrays(testArray4, answerArray4, 1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

80 символов в строке максимум - разбить на 2 строки


smartQuickSort(testArray3, 0, 1 - 1);

return testStarterForArrays(testArray1, answerArray1, 5) && testStarterForArrays(testArray2, answerArray2, 5) && testStarterForArrays(testArray3, answerArray3, 1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

см. выше

return firstValue == 1 && secondValue == 0;
}

static const bool testStarterForArrays(int testArray[], int answerArray[], int size)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const int * const
size_t для индексов
testStarterForArrays -> compareArrays

Comment on lines 155 to 167
int size = 0;
while (size <= 0)
{
printf("Enter the size of your array: ");
if (scanf("%d", &size) != 1)
{
return INPUT_ERROR;
}
if (size <= 0)
{
printf("\nSize of your array should be at least 1\n");
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size_t
%d -> %lu

return i;
}

static void insertSort(int *const array, int leftElement, size_t rightElement)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size_t


static void insertSort(int *const array, int leftElement, size_t rightElement)
{
for (int i = leftElement + 1; i < rightElement; ++i)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size_t

partition(testArray3, 0, 5);
partition(testArray4, 0, 1);

return compareArrays(testArray1, answerArray1, 9) && compareArrays(testArray2, answerArray2, 6) && compareArrays(testArray3, answerArray3, 5) && compareArrays(testArray4, answerArray4, 1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

разбить на 2 строки

smartQuickSort(testArray2, 0, 5 - 1);
smartQuickSort(testArray3, 0, 1 - 1);

return compareArrays(testArray1, answerArray1, 5) && compareArrays(testArray2, answerArray2, 5) && compareArrays(testArray3, answerArray3, 1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

разбить на 2 строки

Comment on lines 164 to 175
while (size <= 0)
{
printf("Enter the size of your array: ");
if (scanf("%lu", &size) != 1)
{
return INPUT_ERROR;
}
if (size <= 0)
{
printf("\nSize of your array should be at least 1\n");
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size_t - unsigned long - всегда неотрицательный
в данном случае никогда не зайдём в цикл

partition(testArray2, 0, 6);
partition(testArray3, 0, 5);
partition(testArray4, 0, 1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Всё ещё нет разбиения на 2 строки тут и ниже в return

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants