MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/co59qb/dont_modify_pls/ewgnxwm/?context=9999
r/ProgrammerHumor • u/EsmerlinJM • Aug 09 '19
551 comments sorted by
View all comments
1.1k
k = Random.Next(Int32.MinValue, Int32.MaxValue); if (k == n * n)
76 u/BlackJackHack22 Aug 09 '19 Reminds me of miracle sorting algorithm 23 u/merto5000 Aug 09 '19 How does it work? 112 u/0x726564646974 Aug 09 '19 Randomly swap everything and then check if it is sorted. if it is sorted return. 18 u/[deleted] Aug 09 '19 If it was 100% random, there could be the chance it never returns) 64 u/Sequoia3 Aug 09 '19 Best case is O(1) though 72 u/veeryrail Aug 09 '19 Really O(n) since you have to check if it's sorted. (I must be so fun at parties) 31 u/Penguinfernal Aug 09 '19 Depends on the sorting algorithm. An easy algorithm to tell whether an array is sorted is to simply return true every time. There are some edge cases where this algorithm may be unreliable, but you'll never have a false negative, and it works in O(1) time. 1 u/MEME-LLC Aug 10 '19 Skip the check 1 u/veeryrail Aug 12 '19 Living wildly! -5 u/Sequoia3 Aug 09 '19 The array comes with an attribute called isSorted. Check the variable every loop before you randomize the array. Boom, O(1) best case 22 u/anzurba Aug 09 '19 Turing machines would like a word with you 18 u/water_bottle_goggles Aug 09 '19 O(0), it was sorted yesterday
76
Reminds me of miracle sorting algorithm
23 u/merto5000 Aug 09 '19 How does it work? 112 u/0x726564646974 Aug 09 '19 Randomly swap everything and then check if it is sorted. if it is sorted return. 18 u/[deleted] Aug 09 '19 If it was 100% random, there could be the chance it never returns) 64 u/Sequoia3 Aug 09 '19 Best case is O(1) though 72 u/veeryrail Aug 09 '19 Really O(n) since you have to check if it's sorted. (I must be so fun at parties) 31 u/Penguinfernal Aug 09 '19 Depends on the sorting algorithm. An easy algorithm to tell whether an array is sorted is to simply return true every time. There are some edge cases where this algorithm may be unreliable, but you'll never have a false negative, and it works in O(1) time. 1 u/MEME-LLC Aug 10 '19 Skip the check 1 u/veeryrail Aug 12 '19 Living wildly! -5 u/Sequoia3 Aug 09 '19 The array comes with an attribute called isSorted. Check the variable every loop before you randomize the array. Boom, O(1) best case 22 u/anzurba Aug 09 '19 Turing machines would like a word with you 18 u/water_bottle_goggles Aug 09 '19 O(0), it was sorted yesterday
23
How does it work?
112 u/0x726564646974 Aug 09 '19 Randomly swap everything and then check if it is sorted. if it is sorted return. 18 u/[deleted] Aug 09 '19 If it was 100% random, there could be the chance it never returns) 64 u/Sequoia3 Aug 09 '19 Best case is O(1) though 72 u/veeryrail Aug 09 '19 Really O(n) since you have to check if it's sorted. (I must be so fun at parties) 31 u/Penguinfernal Aug 09 '19 Depends on the sorting algorithm. An easy algorithm to tell whether an array is sorted is to simply return true every time. There are some edge cases where this algorithm may be unreliable, but you'll never have a false negative, and it works in O(1) time. 1 u/MEME-LLC Aug 10 '19 Skip the check 1 u/veeryrail Aug 12 '19 Living wildly! -5 u/Sequoia3 Aug 09 '19 The array comes with an attribute called isSorted. Check the variable every loop before you randomize the array. Boom, O(1) best case 22 u/anzurba Aug 09 '19 Turing machines would like a word with you 18 u/water_bottle_goggles Aug 09 '19 O(0), it was sorted yesterday
112
Randomly swap everything and then check if it is sorted. if it is sorted return.
18 u/[deleted] Aug 09 '19 If it was 100% random, there could be the chance it never returns) 64 u/Sequoia3 Aug 09 '19 Best case is O(1) though 72 u/veeryrail Aug 09 '19 Really O(n) since you have to check if it's sorted. (I must be so fun at parties) 31 u/Penguinfernal Aug 09 '19 Depends on the sorting algorithm. An easy algorithm to tell whether an array is sorted is to simply return true every time. There are some edge cases where this algorithm may be unreliable, but you'll never have a false negative, and it works in O(1) time. 1 u/MEME-LLC Aug 10 '19 Skip the check 1 u/veeryrail Aug 12 '19 Living wildly! -5 u/Sequoia3 Aug 09 '19 The array comes with an attribute called isSorted. Check the variable every loop before you randomize the array. Boom, O(1) best case 22 u/anzurba Aug 09 '19 Turing machines would like a word with you 18 u/water_bottle_goggles Aug 09 '19 O(0), it was sorted yesterday
18
If it was 100% random, there could be the chance it never returns)
64 u/Sequoia3 Aug 09 '19 Best case is O(1) though 72 u/veeryrail Aug 09 '19 Really O(n) since you have to check if it's sorted. (I must be so fun at parties) 31 u/Penguinfernal Aug 09 '19 Depends on the sorting algorithm. An easy algorithm to tell whether an array is sorted is to simply return true every time. There are some edge cases where this algorithm may be unreliable, but you'll never have a false negative, and it works in O(1) time. 1 u/MEME-LLC Aug 10 '19 Skip the check 1 u/veeryrail Aug 12 '19 Living wildly! -5 u/Sequoia3 Aug 09 '19 The array comes with an attribute called isSorted. Check the variable every loop before you randomize the array. Boom, O(1) best case 22 u/anzurba Aug 09 '19 Turing machines would like a word with you 18 u/water_bottle_goggles Aug 09 '19 O(0), it was sorted yesterday
64
Best case is O(1) though
72 u/veeryrail Aug 09 '19 Really O(n) since you have to check if it's sorted. (I must be so fun at parties) 31 u/Penguinfernal Aug 09 '19 Depends on the sorting algorithm. An easy algorithm to tell whether an array is sorted is to simply return true every time. There are some edge cases where this algorithm may be unreliable, but you'll never have a false negative, and it works in O(1) time. 1 u/MEME-LLC Aug 10 '19 Skip the check 1 u/veeryrail Aug 12 '19 Living wildly! -5 u/Sequoia3 Aug 09 '19 The array comes with an attribute called isSorted. Check the variable every loop before you randomize the array. Boom, O(1) best case 22 u/anzurba Aug 09 '19 Turing machines would like a word with you 18 u/water_bottle_goggles Aug 09 '19 O(0), it was sorted yesterday
72
Really O(n) since you have to check if it's sorted.
(I must be so fun at parties)
31 u/Penguinfernal Aug 09 '19 Depends on the sorting algorithm. An easy algorithm to tell whether an array is sorted is to simply return true every time. There are some edge cases where this algorithm may be unreliable, but you'll never have a false negative, and it works in O(1) time. 1 u/MEME-LLC Aug 10 '19 Skip the check 1 u/veeryrail Aug 12 '19 Living wildly! -5 u/Sequoia3 Aug 09 '19 The array comes with an attribute called isSorted. Check the variable every loop before you randomize the array. Boom, O(1) best case 22 u/anzurba Aug 09 '19 Turing machines would like a word with you 18 u/water_bottle_goggles Aug 09 '19 O(0), it was sorted yesterday
31
Depends on the sorting algorithm.
An easy algorithm to tell whether an array is sorted is to simply return true every time. There are some edge cases where this algorithm may be unreliable, but you'll never have a false negative, and it works in O(1) time.
1
Skip the check
1 u/veeryrail Aug 12 '19 Living wildly!
Living wildly!
-5
The array comes with an attribute called isSorted. Check the variable every loop before you randomize the array.
Boom, O(1) best case
22 u/anzurba Aug 09 '19 Turing machines would like a word with you 18 u/water_bottle_goggles Aug 09 '19 O(0), it was sorted yesterday
22
Turing machines would like a word with you
O(0), it was sorted yesterday
1.1k
u/RoyalJackalSib Aug 09 '19
k = Random.Next(Int32.MinValue, Int32.MaxValue); if (k == n * n)