Last active
September 6, 2016 19:24
-
-
Save goldsborough/bae62feee7bcf8fbe49353855c5d3b7f to your computer and use it in GitHub Desktop.
Quicksort in CSS (SASS)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@function swap($list, $first, $second) { | |
@if ($first == $second) { | |
@return $list; | |
} | |
$temp: nth($list, $first); | |
$list: set-nth($list, $first, nth($list, $second)); | |
$list: set-nth($list, $second, $temp); | |
@return $list; | |
} | |
@function quicksort-impl($list, $first, $last) { | |
@if (($last - $first) < 2) { | |
@return $list; | |
} | |
$i: $first + 1; | |
$j: $last - 1; | |
$pivot: nth($list, $first); | |
@while $i != $j { | |
@while ($i != $j) and (nth($list, $i) < $pivot) { | |
$i: $i + 1; | |
} | |
@while ($i != $j) and (nth($list, $j) > $pivot) { | |
$j: $j - 1; | |
} | |
@if ($i != $j) { | |
$list: swap($list, $i, $j); | |
$i: $i + 1; | |
} | |
} | |
@if nth($list, $i) > $pivot { | |
$i: $i - 1; | |
} | |
$list: swap($list, $first, $i); | |
$list: quicksort-impl($list, $first, $i); | |
$list: quicksort-impl($list, $i + 1, $last); | |
@return $list; | |
} | |
@function quicksort($list) { | |
$list: quicksort-impl($list, 1, length($list) + 1); | |
@return $list; | |
} | |
div { | |
font-size: quicksort((1px, 9px, 4px, -3px, 6px, 8px, -2px, 10px)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment