Bubble Sort

# bubble sort @arr=(4,6,3,1,8,9,0,2,1,2); #print scalar(@arr); for ($i=0;$i<scalar(@arr);$i++){ # print "i: $arr[$i]\n"; for ($k=0;$k<scalar(@arr)-1;$k++) { #print "$arr[$i] - $arr[$k]\n"; if ($arr[$k]>$arr[$k+1]) { $temp=$arr[$k]; $arr[$k]=$arr[$k+1]; $arr[$k+1]=$temp; } } } foreach (@arr) { print $_; }
The complexity of bubble sort is O(n2) in both worst and average cases, because the entire array needs to be iterated for every element.

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.