-
Couldn't load subscription status.
- Fork 0
qsort task completed #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
semester_1/homework3/qsort.c
Outdated
| #include <stdlib.h> | ||
| #include <stdbool.h> | ||
|
|
||
| int partition(int array[], int leftElement, int rightElement); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Принято передавать массив в функцию по указателю
Размеры и индексы храним в size_t.
Везде, где мы не собираемся менять указатель, освобождать по нему дынные, нужно использовать type * const.
semester_1/homework3/qsort.c
Outdated
| bool checkIfArrayIsSorted(int array[], int size); | ||
| bool testStarterForArrays(int testArray[], int answerArray[], int size); | ||
|
|
||
| int main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
main() -> main(void)
semester_1/homework3/qsort.c
Outdated
| { | ||
| if (!test()) | ||
| { | ||
| return 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
коды ошибок и возврата выносим в define, если это не номер сломавшегося теста
semester_1/homework3/qsort.c
Outdated
| } | ||
|
|
||
| printf("\nEnter the size of your array: "); | ||
| int size = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
смотри прошлую задачу (size_t)
semester_1/homework3/qsort.c
Outdated
| scanf("%d", &size); | ||
| } | ||
|
|
||
| int *array = malloc(size * sizeof(int)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
нет явного приведения к (int *) перед malloc
semester_1/homework3/qsort.c
Outdated
| 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; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Эта функция тут не нужна
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
и тесты для неё тоже
semester_1/homework3/qsort.c
Outdated
|
|
||
| bool test(void) | ||
| { | ||
| if (!(testSwapFunction() * testPartitionFunction() * testSmartQuickSort())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
убрать if, написать нормально через &&
semester_1/homework3/qsort.c
Outdated
| if (firstValue != 1 || secondValue != 0) | ||
| { | ||
| return false; | ||
| } | ||
| return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
это if ну нужен
semester_1/homework3/qsort.c
Outdated
| if (!(testStarterForArrays(testArray1, answerArray1, 9) * testStarterForArrays(testArray2, answerArray2, 6) * testStarterForArrays(testArray3, answerArray3, 5) * testStarterForArrays(testArray4, answerArray4, 1))) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
убрать if, написать через &&
semester_1/homework3/qsort.c
Outdated
| if (!(testStarterForArrays(testArray1, answerArray1, 5) * testStarterForArrays(testArray2, answerArray2, 5) * testStarterForArrays(testArray3, answerArray3, 1))) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
убрать if, написать через &&
semester_1/homework3/qsort.c
Outdated
|
|
||
| const bool test(void); | ||
|
|
||
| static void printArray(int *array, size_t size) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const int * const
semester_1/homework3/qsort.c
Outdated
| printf("\n"); | ||
| } | ||
|
|
||
| static void swap(int *firstValue, int *secondValue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int * const
semester_1/homework3/qsort.c
Outdated
| *secondValue = buffer; | ||
| } | ||
|
|
||
| static size_t partition(int *array, size_t leftElement, size_t rightElement) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int * const
semester_1/homework3/qsort.c
Outdated
| return i; | ||
| } | ||
|
|
||
| static void insertSort(int array[], int leftElement, int rightElement) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int * const
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
size_t для индексов
semester_1/homework3/qsort.c
Outdated
| } | ||
| } | ||
|
|
||
| static void smartQuickSort(int array[], int leftElement, int rightElement) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int * const
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
size_t для индексов
semester_1/homework3/qsort.c
Outdated
| { | ||
| printf("Enter %lu number of your array: ", i + 1); | ||
| int currentNumber = 0; | ||
| scanf("%d", ¤tNumber); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
&array[i]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
убрать currentNumber
semester_1/homework3/qsort.c
Outdated
| 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
80 символов в строке максимум - разбить на 2 строки
semester_1/homework3/qsort.c
Outdated
|
|
||
| smartQuickSort(testArray3, 0, 1 - 1); | ||
|
|
||
| return testStarterForArrays(testArray1, answerArray1, 5) && testStarterForArrays(testArray2, answerArray2, 5) && testStarterForArrays(testArray3, answerArray3, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
см. выше
semester_1/homework3/qsort.c
Outdated
| return firstValue == 1 && secondValue == 0; | ||
| } | ||
|
|
||
| static const bool testStarterForArrays(int testArray[], int answerArray[], int size) |
There was a problem hiding this comment.
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
semester_1/homework3/qsort.c
Outdated
| 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"); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
size_t
%d -> %lu
semester_1/homework3/qsort.c
Outdated
| return i; | ||
| } | ||
|
|
||
| static void insertSort(int *const array, int leftElement, size_t rightElement) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
size_t
semester_1/homework3/qsort.c
Outdated
|
|
||
| static void insertSort(int *const array, int leftElement, size_t rightElement) | ||
| { | ||
| for (int i = leftElement + 1; i < rightElement; ++i) |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
разбить на 2 строки
semester_1/homework3/qsort.c
Outdated
| 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"); | ||
| } | ||
| } |
There was a problem hiding this comment.
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); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Всё ещё нет разбиения на 2 строки тут и ниже в return
No description provided.