Created
January 21, 2015 18:02
-
-
Save Aleksey-Danchin/5d6154d648d85604d056 to your computer and use it in GitHub Desktop.
Функция рандомной сортировки массива.
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 randomSort (array) { | |
var length, isSorted, i; | |
length = array.length | |
// Проверка на возрастание. | |
isSorted = true; | |
for (i = 1; i < length && isSorted; i++) | |
isSorted = (array[i - 1] <= array[i]); | |
// Если массив уже отсортирован, то и делать больше нечего. | |
if (isSorted) return array; | |
while (!isSorted) { | |
// Сама рандомная сортировка. | |
array.sort(function () { | |
return Math.random() > Math.random(); | |
}); | |
// Проверка на возрастание. | |
isSorted = true; | |
for (i = 1; i < length && isSorted; i++) | |
isSorted = (array[i - 1] <= array[i]); | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment