Заполните массив цифрами:
[
,
,
,
,
,
,
,
,
,
]
Скорость итерации: 0.10.20.40.60.81
//Сортировка пузырьковым методом
const arraySorting = () => {
let array = [37, 2, 89, 65, 1, 28, 41];
for(out = array.length - 1; out > 0; out--){
for (let i = 0; i < out; i++) {
if (array[i] > array[i + 1]) {
temp = array[i + 1];
array[i + 1] = array[i];
array[i] = temp;
}
}
}
return array;
}
console.log(arraySorting());
//Сортировка перемешиванием
const arraySorting = () => {
let array = [37, 2, 89, 65, 1, 28, 41];
let start = 0;
let stop = array.length - 1;
do{
swap = false;
for (i = start; i < stop; i++) {
if (array[i] > array[i + 1]) {
temp = array[i + 1];
array[i + 1] = array[i];
array[i] = temp;
swap = true;
}
}
for (l = stop - 1; l >= start; l--) {
if (array[l] > array[l + 1]) {
temp = array[l + 1];
array[l + 1] = array[l];
array[l] = temp;
swap = true;
}
}
start++;
stop--;
} while (swap);
return array;
}
console.log(arraySorting());
//Гномья сортировка
const arraySorting = () => {
let array = [37, 2, 89, 65, 1, 28, 41];
let index = 1;
let nextIndex = index + 1;
while (index < array.length) {
if (array[index - 1] < array[index]) {
index = nextIndex;
nextIndex++;
} else {
temp = array[index - 1];
array[index - 1] = array[index];
array[index] = temp;
index--;
if (index === 0) {
index = nextIndex;
nextIndex++;
}
}
}
return array;
}
console.log(arraySorting());
//Сортировка расчёской
const arraySorting = () => {
let array = [37, 2, 89, 65, 1, 28, 41];
const l = array.length;
const constant = 1.247;
let step = l / constant;
while (step > 1) {
const stepRound = Math.floor(step);
for (let i = 0, j = stepRound; j < l; i++, j++) {
if (array[i] > array[j]) {
[array[i], array[j]] = [array[j], array[i]];
}
}
step = stepRound / constant;
}
return array;
}
console.log(arraySorting());
//Сортировка выбором
const arraySorting = () => {
let array = [37, 2, 89, 65, 1, 28, 41];
const l = array.length;
for (let i = 0; i < l; i++) {
let min = i;
for (let j = i; j < l; j++) {
if (array[j] < array[min]) {
min = j;
}
}
if (min != i) {
let temp = array[i];
array[i] = array[min];
array[min] = temp;
}
}
return array;
}
console.log(arraySorting());
//Двухсторонняя сортировка выбором
const arraySorting = () => {
let array = [37, 2, 89, 65, 1, 28, 41];
const l = array.length;
for(i = 0, end = l-1; i < end; i++, end--){
let min = i;
let max = i;
for (let j = i; j <= end; j++) {
if (array[j] < array[min]) {
min = j;
}
if (array[j] > array[max]) {
max = j;
}
}
if (min !== i) {
let temp = array[i];
array[i] = array[min];
array[min] = temp;
if (max === i) {
max = min;
}
}
if (max !== end) {
let temp = array[end];
array[end] = array[max];
array[max] = temp;
}
}
return array;
}
console.log(arraySorting());
//Двухсторонняя сортировка выбором
const arraySorting = () => {
let array = [37, 2, 89, 65, 1, 28, 41];
const l = array.length;
for(i = 0, end = l-1; i < end; i++, end--){
let min = i;
let max = i;
for (let j = i; j <= end; j++) {
if (array[j] < array[min]) {
min = j;
}
if (array[j] > array[max]) {
max = j;
}
}
if (min !== i) {
let temp = array[i];
array[i] = array[min];
array[min] = temp;
if (max === i) {
max = min;
}
}
if (max !== end) {
let temp = array[end];
array[end] = array[max];
array[max] = temp;
}
}
return array;
}
console.log(arraySorting());