Last active
December 17, 2015 02:49
-
-
Save AlexMost/5539182 to your computer and use it in GitHub Desktop.
This file contains 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
qsort = ([f, o...]) -> | |
return [] unless f | |
less = o.filter (i) -> i < f | |
more = o.filter (i) -> i >= f | |
qsort(less).concat([f]).concat(qsort more) |
This file contains 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
template <typename T> | |
void qsort (T *result, T *list, int n) | |
{ | |
if (n == 0) return; | |
T *smallerList, *largerList; | |
smallerList = new T[n]; | |
largerList = new T[n]; | |
T pivot = list[0]; | |
int numSmaller=0, numLarger=0; | |
for (int i = 1; i < n; i++) | |
if (list[i] < pivot) | |
smallerList[numSmaller++] = list[i]; | |
else | |
largerList[numLarger++] = list[i]; | |
qsort(smallerList,smallerList,numSmaller); | |
qsort(largerList,largerList,numLarger); | |
int pos = 0; | |
for ( int i = 0; i < numSmaller; i++) | |
result[pos++] = smallerList[i]; | |
result[pos++] = pivot; | |
for ( int i = 0; i < numLarger; i++) | |
result[pos++] = largerList[i]; | |
delete [] smallerList; | |
delete [] largerList; | |
}; |
This file contains 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
qsort [] = [] | |
qsort (x:xs) = qsort less ++ [x] ++ qsort more | |
where less = filter (<x) xs | |
more = filter (>=x) xs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
qsort.clj
Clojure solution is similar to Haskell one.