officialandrey
@officialandrey

Как переставить в обратном порядке элементы массива?

Господа, пожалуйста, умоляю, я уже не знаю что читать, что нажимать, каким богам молиться...? Необходимо переставить в обратном порядке элементы массива между максимальным и минимальным:
input 1 2 3 4 5
output 1 4 3 2 5
#!/bin/bash

read N
arr=($(cat))

maxPos=0
minPos=0
j=0

for ((i=0; i < N; i++))
do
	if ((${arr[minPos]} > ${arr[i]}))
	then
		minPos=$i
    fi
    
    if ((${arr[maxPos]} < ${arr[i]}))
    then
		maxPos=$i
    fi
done

if (($minPos<$maxPos))
then
	first=$minPos
	second=$maxPos
else
    first=$maxPos
    second=$minPos
fi

for ((i=0; i<first; i++))
do
	echo ${arr[i]}
done

for ((i=second; i>=first; i--))
do
	echo ${arr[i]}
done

for ((i=second+1; i<N; i++))
do
	echo ${arr[i]}
done
  • Вопрос задан
  • 244 просмотра
Решения вопроса 1
officialandrey
@officialandrey Автор вопроса
Все уже решил проблему.
Если кому-то пригодиться, вот код:
#!/bin/bash

read N
arr=($(cat))

maxPos=0
minPos=0
j=0

for ((i=0; i < N; i++))
do
  if ((${arr[minPos]} > ${arr[i]}))
  then
    minPos=$i
    fi
    
    if ((${arr[maxPos]} < ${arr[i]}))
    then
    maxPos=$i
    fi
done

if (($minPos<$maxPos))
then
  first=$minPos
  second=$maxPos
else
    first=$maxPos
    second=$minPos
fi

for ((i=0; i<first+1; i++))
do
  echo ${arr[i]}
done

for ((i=second-1; i>=first+1; i--))
do
  echo ${arr[i]}
done

for ((i=second; i<N; i++))
do
  echo ${arr[i]}
done
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы