in reply to Sort routine runs into infinite loop
Hi punitpawar,
I suppose you want to implement bubble sort (http://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort#Perl).
This is a working version of your code.
#!/usr/bin/perl use strict; my @array=(12,6,2,9,15); my ($i,$j,$tmp); for ($i=0; $i<@array; $i++){ for ($j=$i+1;$j<@array;$j++){ if ($array[$j]<$array[$i]){ # '>' reverses the order $tmp = $array[$j]; $array[$j] = $array[$i]; $array[$i] = $tmp; } } } print "array : @array \n";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Sort routine runs into infinite loop
by GrandFather (Saint) on Jan 02, 2016 at 21:34 UTC | |
Re^2: Sort routine runs into infinite loop
by hotchiwawa (Scribe) on Jan 02, 2016 at 17:50 UTC | |
by oiskuu (Hermit) on Jan 02, 2016 at 18:47 UTC | |
by Laurent_R (Canon) on Jan 02, 2016 at 19:51 UTC | |
by hotchiwawa (Scribe) on Jan 02, 2016 at 19:56 UTC |
In Section
Seekers of Perl Wisdom