Question

Write a program that inputs ten (10) integers and then sorts them in order from largest...

Write a program that inputs ten (10) integers and then sorts them in order from largest to smallest in the programming language called Perl. You do not need to error check the input; you can assume the user enters integers. You can select the sorting algorithm of your choice, but you must implement this algorithm yourself. You cannot use a built-in sorting function. [15 points]

Below is an example of a sample program run:

Unsorted: 10, 4, 23, 99, 7, 2, 1, 11, 44, 15

Sorted: 99, 44, 23, 15, 11, 10, 7, 4, 2, 1

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

Algorithm

1.Declare Ascending and Descending order two arrays .

2. find out min and max integer from original array

3.Put them at start of new array

4.remove them from original arrays

5.Repeat step 2.

6.finish the algorithm.

###############################

Warning:-Please remove all comments from the code and maintain proper indentation to run the code on your own compiler.

Code:-

#!/usr/bin/perl

use strict;use warnings;

my @tab = qw//; #Declaration of Array
for (1 .. 10) {
my $num = <STDIN>; #TAking array as input
chomp $num;
push @tab, $num;
}

my ($n,$max,@tri,@tri_desc); #Declaration of ascending and descending order Arrays.

$max = $tab[0];

print "@tab\n";

while(@tab){ #Sorting begins here

$_ > $max and $max = $_ for @tab;

for (@tab){ $n++;last if $max == $_}

unshift @tri,$max;

push @tri_desc,$max;

splice(@tab,$n-1,1);

$n=0;

$max = $tab[0];

}

print "ascending order:\t@tri\n";

print "descending order:\t@tri_desc\n";

############################################################

Add a comment
Know the answer?
Add Answer to:
Write a program that inputs ten (10) integers and then sorts them in order from largest...
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