Archived
67 lines
1.1 KiB
C
Executable File
67 lines
1.1 KiB
C
Executable File
/*
|
|
21.Выполнить сортировку одномерного массива 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 _swap(int *x, int *y)
|
|
{
|
|
int temp = *x;
|
|
*x = *y;
|
|
*y = temp;
|
|
}
|
|
|
|
void sortArrayBubble(int *array)
|
|
{
|
|
int i, j;
|
|
bool is_swap;
|
|
for (i = 0; i < N - 1; i++)
|
|
{
|
|
is_swap = false;
|
|
for (j = 0; j < N - i - 1; j++)
|
|
{
|
|
if (array[j] > array[j + 1])
|
|
{
|
|
_swap(&array[j], &array[j + 1]);
|
|
is_swap = true;
|
|
}
|
|
}
|
|
|
|
if (is_swap == false)
|
|
break;
|
|
}
|
|
}
|
|
|
|
void printArray(int *array)
|
|
{
|
|
for (int i = 0; i < N; i++)
|
|
{
|
|
printf("%d ", array[i]);
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
int A[N];
|
|
|
|
initArray(A, 10);
|
|
sortArrayBubble(A);
|
|
printArray(A);
|
|
|
|
return 0;
|
|
} |