[ create a new paste ] login | about

Link: http://codepad.org/0k2Ou2Db    [ raw code | output | fork ]

Perl, pasted on Dec 26:
# Sort small positive integers without comparing them.

@unsorted = (111,3,1,4,1,59,2,6);

# Simple copy into @sorted
for $element (@unsorted) {
    $sorted[$element]++;
    }

# Print unsorted list.  you can comment out these 3 lines, they have no side effect except printing the original list:
print "Unsorted: ";

for($unsortedi = 0; $unsortedi<@unsorted; $unsortedi++){
  print $unsorted[$unsortedi], " ";
  }


# and print the sorted list - still no comparisons
print "\nSorted: ";

for($sortedi = 0; $sortedi<@sorted; $sortedi++){
  print "$sortedi " x $sorted[$sortedi];
  }

# magic :)


Output:
1
2
Unsorted: 111 3 1 4 1 59 2 6 
Sorted: 1 1 2 3 4 6 59 111 


Create a new paste based on this one


Comments: