-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperlSelectionSort.pl
executable file
·107 lines (91 loc) · 2.63 KB
/
perlSelectionSort.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/perl
###############################################################################
# perlSelectionSort.pl
# A simple perl Selection sort
#
# AUTHOR
# Vernon O' Reilly
# DATE
# 12 Sept 2015
# VERSION
# 1.0
# PURPOSE
# Enter purpose here
# USAGE
# perl perlSelectionSort.pl
# LICENSE
# You may copy and redistribute this program as you see fit, with no
# restrictions.
# WARRANTY
# This program comes with NO warranty, real or implied.
###############################################################################
use strict;
use warnings;
use v5.10;
use Data::Dumper;
my $numbers = &prompt("Enter a list of unsorted numbers seperated by a comma: ", "3,6,1,2,6,9,8");
say "The inputs were:";
say $numbers;
#First add the string into a proper array
my @numbers = split(",", $numbers);
say "The input array is:";
say @numbers;
my @sorted = selectionsort(@numbers);
say "The selection sorted list is now:";
say @sorted;
say "Output in list string format is:";
my $sortedstr;
foreach my $i (@sorted) {
$sortedstr = $sortedstr."$i,";
}
chop($sortedstr);
say $sortedstr;
######################################################################################
# subroutine: selectionsort
# Purpose: take arguments if given from command line, else will use the default 345
######################################################################################
sub selectionsort {
my @nums = @_;
# say "In selectionsort: @nums";
my $i = 0; # Starting index of a minimum-finding scan
my $j = 0; # Current index of a minimum finding scan
my $min=0;
for (my $i = 0; $i <= $#nums; $i++) {
$min = $nums[$i];
# say "min: $min";
# say "i: $i";
# say "numsi: @nums";
for (my $j = $i; $j <= $#nums; $j++) { #loop search for smallest number, loop gets smaller each turn outer loop is run
# say "j: $j";
if ($nums[$j] < $nums[$i]) {
# swap
my $tmp = $nums[$i];
$nums[$i] = $nums[$j];
$nums[$j] = $tmp;
}
# say "numsj: @nums";
}
}
# say "Before returning: @nums";
return @nums;
}
######################################################################################
# subroutine: prompt
# Purpose: take arguments if given from command line, else will use the default 345
######################################################################################
sub prompt {
my ($promptstr,$default) = @_;
if ($default) {
say $promptstr, "[", $default, "]: ";
} else {
print $promptstr, ": ";
}
$| = 1; #Flush after print
$_ = <STDIN>; #Get input for STDIN
chomp; #take off new line character
if ($default) {
return $_ ? $_ : $default; #return $_ if its got a value
} else {
return $_;
}
}