Selection Sort

# selection sort @arr=(4,6,3,1,8,9,0,2,1,2); #print scalar(@arr); for ($i=0;$i<scalar(@arr);$i++){ for ($k=$i+1;$k<scalar(@arr);$k++) { if ($arr[$i]>$arr[$k]) { $temp=$arr[$i]; $arr[$i]=$arr[$k]; $arr[$k]=$temp; } } } foreach (@arr) { print $_; }
If there are n elements to be sorted then, the process mentioned above should be repeated n-1 times to get required result

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.