Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions Task 3-1/3-1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}

// Реализация сортировки вставками
void insertSort(int array[], int start, int end)
{
if (start >= end)
{
return;
}

for (int i = start; i < end; i++)
{
int element = array[i];
int location = i - 1;

while (location >= 0 && array[location] > element)
{
array[location + 1] = array[location];
location = location - 1;
}

array[location + 1] = element;
}
}

// Функция, разделяющая массив относительно точки опоры
int partition(int array[], int start, int end)
{
int pivot = array[end - 1];
int index = start;

for (int i = start; i < end; i++)
{
if (array[i] <= pivot)
{
swap(&array[i], &array[index]);
++index;
}
}

swap(&array[index], &array[end - 1]);
return index;
}

// Сама функция qSort
void quickSort(int array[], int start, int end)
{
if (start >= end)
{
return;
}

if (end - start < 10)
{
insertSort(array, start, end);
}
else
{
int pivot = partition(array, start, end);
quickSort(array, start, pivot - 1);
quickSort(array, pivot + 1, end);
}
}

bool arraysEqual(int array1[], int array2[], int length)
{
bool result = true;

for (int i = 0; i < length; i++)
{
if (! (array1[i] == array2 [i]))
{
result = false;
}
}

return result;
}

bool test(int array[], int arraySorted[], int arrayLength)
{
quickSort(array, 0, arrayLength);
return arraysEqual(array, arraySorted, arrayLength);
}

bool tests()
{
int array1[] = {4, 1, 8, 5};
int array1Sorted[] = {1, 4, 5, 8};
int array1Length = 4;

int array2[] = {0};
int array2Sorted[] = {0};
int array2Length = 1;

int array3[] = {34, 2452, 143, 34, 134, 566, 3, 100, 1234, 1234, 34, 565, 544};
int array3Sorted[] = {3, 34, 34, 34, 100, 134, 143, 544, 565, 566, 1234, 1234, 2452};
int array3Length = 13;

return test(array1, array1Sorted, array1Length) && test(array2, array2Sorted, array2Length) \
&& test(array3, array3Sorted, array3Length);
}

int main()
{
if (tests())
{
printf("Все тесты пройдены\n");
return 0;
}
printf("Тесты не пройдены\n");
return -1;
}
1 change: 1 addition & 0 deletions Task 3-1/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gcc -Wall -Wextra 3-1.c