Archived
64 lines
1.0 KiB
C
Executable File
64 lines
1.0 KiB
C
Executable File
/*
|
|
23.Выполнить сортировку одномерного массива 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 sortArrayInsertion(int *array)
|
|
{
|
|
int i, key, j;
|
|
for (i = 1; i < N; i++)
|
|
{
|
|
key = array[i];
|
|
j = i - 1;
|
|
|
|
while (j >= 0 && array[j] > key)
|
|
{
|
|
array[j + 1] = array[j];
|
|
j = j - 1;
|
|
}
|
|
array[j + 1] = key;
|
|
}
|
|
}
|
|
|
|
|
|
void printArray(int *array)
|
|
{
|
|
for (int i = 0; i < N; i++)
|
|
{
|
|
printf("%d ", array[i]);
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
int A[N];
|
|
|
|
initArray(A, 10);
|
|
sortArrayInsertion(A);
|
|
printArray(A);
|
|
|
|
return 0;
|
|
} |