This repository has been archived on 2026-07-28. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files

79 lines
1.3 KiB
C
Executable File

/*
22.Выполнить сортировку одномерного массива A[25] методом быстрой сортировки.
*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#define N 25
void initArray(int *array, const int MAX)
{
srand(time(0));
for (int i = 0; i < N; i++)
{
array[i] = rand() % MAX + 1;
}
}
void sortArrayQuick(int *array, int size)
{
int i = 0;
int j = size - 1;
int middle = array[size / 2];
do
{
while (array[i] < middle)
{
i++;
}
while (array[j] > middle)
{
j--;
}
if (i <= j)
{
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i++;
j--;
}
} while (i <= j);
if (j > 0)
{
sortArrayQuick(array, j + 1);
}
if (i < size)
{
sortArrayQuick(&array[i], size - i);
}
}
void printArray(int *array)
{
for (int i = 0; i < N; i++)
{
printf("%d ", array[i]);
}
}
int main()
{
int A[N];
initArray(A, 10);
sortArrayQuick(A, N);
printArray(A);
return 0;
}