import random
def magic(n):
matrix = []
def sort(array):
i = 0
def check(array):
for j in range(len(array) - 2):
if array[j] > array[j + 1]:
return False
return True
while True:
if array[i] > array[i + 1]:
max = array[i]
array[i] = array[i + 1]
array[i + 1] = max
i += 1
if i > len(array) - 2:
if check(array):
break
return array
def writeMatrix(matrix):
for arr in matrix:
print(arr)
for row in range(n):
matrix.append([])
for column in range(n):
matrix[row].append(random.randint(-50, 50))
print("Non sorted")
writeMatrix(matrix)
print()
arr = sort(arr)
print("Sorted rows")
writeMatrix(matrix) # sorted rows
column = []
column.append(arr[0])
column = sort(column)
for j in range(len(column) - 1):
matrix[j][0] = column[j]
print("Full sorted")
writeMatrix(matrix) # sorted rows and columns
def main():
n = int(input())
magic(n)
main()
#include <stdio.h>
#include <malloc.h>
//сортировка вставками
void InsertionSort(int n, int mass[])
{
int newElement, location;
for (int i = 1; i < n; i++)
newElement = mass[i];
location = i - 1;
while(location >= 0 && mass[location] > newElement)
mass[location+1] = mass[location];
location = location - 1;
}
mass[location+1] = newElement;
int main()
//ввод N
int N;
printf("Input N: ");
scanf("%d", &N);
//выделение памяти под массив
int* mass;
mass = (int *)malloc(N * sizeof(int));
//ввод элементов массива
printf("Input the array elements:\n");
for (int i = 0; i < N; i++)
scanf("%d", &mass[i]);
InsertionSort(N, mass);
//вывод отсортированного массива на экран
printf("Sorted array:\n");
printf("%d ", mass[i]);
printf("\n");
//освобождение памяти
free(mass);
return 0;
import random
def magic(n):
matrix = []
def sort(array):
i = 0
def check(array):
for j in range(len(array) - 2):
if array[j] > array[j + 1]:
return False
return True
while True:
if array[i] > array[i + 1]:
max = array[i]
array[i] = array[i + 1]
array[i + 1] = max
i += 1
if i > len(array) - 2:
i = 0
if check(array):
break
return array
def writeMatrix(matrix):
for arr in matrix:
print(arr)
for row in range(n):
matrix.append([])
for column in range(n):
matrix[row].append(random.randint(-50, 50))
print("Non sorted")
writeMatrix(matrix)
print()
print()
for arr in matrix:
arr = sort(arr)
print("Sorted rows")
writeMatrix(matrix) # sorted rows
column = []
for arr in matrix:
column.append(arr[0])
column = sort(column)
for j in range(len(column) - 1):
matrix[j][0] = column[j]
print()
print()
print("Full sorted")
writeMatrix(matrix) # sorted rows and columns
def main():
n = int(input())
magic(n)
main()
#include <stdio.h>
#include <malloc.h>
//сортировка вставками
void InsertionSort(int n, int mass[])
{
int newElement, location;
for (int i = 1; i < n; i++)
{
newElement = mass[i];
location = i - 1;
while(location >= 0 && mass[location] > newElement)
{
mass[location+1] = mass[location];
location = location - 1;
}
mass[location+1] = newElement;
}
}
int main()
{
//ввод N
int N;
printf("Input N: ");
scanf("%d", &N);
//выделение памяти под массив
int* mass;
mass = (int *)malloc(N * sizeof(int));
//ввод элементов массива
printf("Input the array elements:\n");
for (int i = 0; i < N; i++)
scanf("%d", &mass[i]);
//сортировка вставками
InsertionSort(N, mass);
//вывод отсортированного массива на экран
printf("Sorted array:\n");
for (int i = 0; i < N; i++)
printf("%d ", mass[i]);
printf("\n");
//освобождение памяти
free(mass);
return 0;
}