Question

Write a Unix shell script that takes three integers and sorts them from largest to smallest....

Write a Unix shell script that takes three integers and sorts them from largest to smallest. An example execution is as follows (assume the name of the script is shs): % shs 3 9 4 The order is: 9 4 3 % shs -1 0 3 The order is: 0 -1 -3

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM

# !/bin/bash
declare num=(3 9 4)
# This will print the array before sorting
echo "Original Numbers in array:"
for (( i=0; i<=2; i++ ))
do
echo ${num[$i]}
done
# Now lets do the Sorting of numbers
for (( i=0; i<=2; i++ ))
do
for (( j=$i; j<=2; j++ ))
do
if [ ${num[$j]} -gt ${num[$i]} ]; then #Just using a sorting technique with O(n^2)
t=${num[$i]}
num[$i]=${num[$j]}
num[$j]=$t
#swapping the array to get desired result
fi
done
done
# Printing the sorted array
echo -e "\nSorted Numbers in Descending Order
Order:"
for (( i=0; i <= 2; i++ ))
do
echo ${num[$i]}
done

OUTPUT OF THE PROGRAM

This is the running code..It may cause problem due to indentation because i have pasted here from my file.....HOPE YOU WILL FIND THIS HELPFUL... if you have any queries regarding this let me know.

Add a comment
Know the answer?
Add Answer to:
Write a Unix shell script that takes three integers and sorts them from largest to smallest....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT