randomSort
1
Name:
Anonymous
2008-04-07 11:06
#include <cstdio>
#include <ctime>
#include <cstdlib>
using namespace std;
bool isSorted(int* list, int l)
{
for (int i=1; i<l; i++)
if (list[i-1]>list[i] )
return false;
return true;
}
int*& randomSort(int*& list, int l)
{
int i;
int j;
int temp;
srand(time(0));
while (!isSorted(list,l))
{
i=rand()%l;
j=rand()%l;
temp = list[i] ;
list[i] = list[j];
list[j] = temp;
}
return list;
}
int main(void)
{
int k=14;
int* a = new int[15];
for (int i=0; i<15; i++)
a[i] =k--;
a = randomSort(a,15);
for(int i=0; i<15; i++)
printf("%d ",a[i] );
return 0;
}
//NO ONE CAN FUCKING TOP THIS
2
Name:
Anonymous
2008-04-07 11:09
// Optimized version. Caution: randomness may be biased!
int** randomSort(int** list, int l)
{
return list;
}
3
Name:
Anonymous
2008-04-07 11:10
ROFL I GUESS THIS IS WHY THEY CALL THIS PLACE RANDOM xD
oh wait, this isn't random...
4
Name:
Anonymous
2008-04-07 11:11
>>2
UNREFERENCED_PARAMETER(l);
5
Name:
Anonymous
2008-04-07 11:13
listNull :: [a] -> [a]
listNull x = []
I just started learning Haskell yesterday, so don't be harsh.
(it removes all elements from the list, if you don't know Haskell).
6
Name:
Anonymous
2008-04-07 11:13
>>4
Be an EXPERT. Turn off compiler warnings.
7
Name:
Anonymous
2008-04-07 11:13
>>2
What are these fuzzy double *s after the function and variable names?
8
Name:
FAIL
2008-04-07 11:14
Good job reinventing bozo sort.
9
Name:
Anonymous
2008-04-07 11:14
>>1
I've been running that for a few minutes now and it still hasn't sorted the array...
10
Name:
Anonymous
2008-04-07 11:15
11
Name:
Anonymous
2008-04-07 11:16
>>8
fuck I didn't know that
I thought that this was so immensely stupid, that no one else would ever have done it
12
Name:
Anonymous
2008-04-07 11:17
>>7
It's the
power method.
13
Name:
Anonymous
2008-04-07 11:25
Seems to run in O(n) time for me.
14
Name:
Anonymous
2008-04-07 11:47
>>13
>>9 here
still not done
15
Name:
Anonymous
2008-04-07 12:03
Nice O (0) algorithm.
16
Name:
Anonymous
2008-04-07 13:03
<a href="donations.html"><blink>Donate to stop<br>the blinking!</blink></a>
17
Name:
Anonymous
2008-04-07 13:05
18
Name:
Anonymous
2008-04-07 14:06
bozo?
19
Name:
Anonymous
2008-04-07 14:25
I use bogosorts in my enterprise applications all the time. It's only natural...
20
Name:
Anonymous
2008-04-07 14:28
>>19
Are you serious? Your a fucking retard if your serious.
21
Name:
Anonymous
2008-04-07 14:29
>>20
What about
my fucking retard?
What about
my serious?
22
Name:
Anonymous
2008-04-07 14:29
>>20
do you want your facepalm here or should I wrap it up for the road?
23
Name:
Anonymous
2008-04-07 14:30
24
Name:
Anonymous
2008-04-07 14:32
In before YHBT Haskell Beginners Tutorial
25
Name:
oy vey
2008-04-07 14:45
oy gevalt
26
Name:
Anonymous
2008-04-07 14:47
27
Name:
Anonymous
2008-04-07 15:15
>>18 here
>>26
whoa, and to think I was
joking about the bozo sort.
28
Name:
Anonymous
2008-04-07 17:16
What about this one?
while (!isSorted(list,l))
{
i=rand()%l;
j=rand()%l;
if(i<j && list[i]>list[j]) {
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
where the elements to compare are picked at random, and swapped only if they aren't out of order.
29
Name:
Anonymous
2008-04-07 21:30
import System.Random
import Control.Arrow
import Control.Monad
import Test.QuickCheck
permutations (x:xs) = [zs | ys <- permutations xs, zs <- interleave x ys]
where interleave x [] = [[x]]
interleave x (y:ys) = (x : y : ys) : map (y:) (interleave x ys)
permutations [] = [[]]
sorted [] = True
sorted [_] = True
sorted (x:x':xs) = x < x' && sorted (x' : xs)
randomChoice xs g = first (xs!!) $ randomR (0, length xs - 1) g
randomSort :: (Ord a, RandomGen g) => [a] -> g -> ([a], g)
randomSort xs g = case randomChoice (permutations xs) g of
(xs', g) | sorted xs' -> (xs', g)
| otherwise -> randomSort xs' g
runRandomSort xs = do (xs', g') <- liftM (randomSort xs) $ getStdGen
setStdGen g'; return xs'
prop_randomSort :: [Int] -> Int -> Bool
prop_randomSort xs g_seed = sorted $ fst $ randomSort xs $ mkStdGen g_seed
main :: IO ()
main = quickCheck prop_randomSort
30
Name:
Anonymous
2008-04-07 22:56
31
Name:
Anonymous
2008-04-11 8:03
>>28
But that way, the algorithm would eventually sort, it's guaranteed to - not very russian roulette-like
That isn't what randomSort is about, indeed, there should be the possibility of your list never, ever getting sorted...
32
Name:
Anonymous
2008-04-11 9:48
I use applesort as a sorting algorithm.
The procedure is basically, you transform the list into an appleformed matrix and then use bubblesort to sort it.
33
Name:
Anonymous
2008-04-11 10:04
34
Name:
Anonymous
2008-04-11 17:23
I'm going to show this to my CS professor
in b4 expulsion
35
Name:
Anonymous
2008-04-12 1:21
I use a printersort.
1) Print out all the numbers, one number per sheet of paper. Use 200 point arial.
2) Hand out numbers to people who are good at math
3) Have them sort the numbers
4) Use OCR software to scan the numbers back in order
You now have a sorted list.
Bonus: It's the only sort in the world with an unpredictable error rate.
36
Name:
Anonymous
2008-04-12 3:47
>>5
No it doesn't, you're just returning an empty list.
listNull [] = []
listNull [x:xs] = listNull xs
37
Name:
Anonymous
2008-04-12 6:21
38
Name:
Anonymous
2008-04-13 8:33
I use penisSort
yeah... let's not go in there
39
Name:
Anonymous
2008-04-13 11:00
>>35
LOL. But you can optimize it by using nigger slaves.
>>38
Explain how that works.
40
Name:
Anonymous
2008-04-13 12:41
>>39
I use azn slaves. Some said it wasn't worth the difference, but I paid it anyway, and I'm happy with the performance.
...though I'm pretty sure nigger slaves will be better for penisSort.
Newer Posts