{"id":453971,"date":"2025-04-01T15:12:07","date_gmt":"2025-04-01T15:12:07","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=453971"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=453971","title":{"rendered":"<span>JavaScript: \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b. \u0427\u0430\u0441\u0442\u044c 8<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/webt\/ma\/po\/lv\/mapolvqq4uunxfqoaviv3g9km9y.jpeg\" data-src=\"https:\/\/habrastorage.org\/webt\/ma\/po\/lv\/mapolvqq4uunxfqoaviv3g9km9y.jpeg\" data-blurred=\"true\"\/> <\/p>\n<p> \u041f\u0440\u0438\u0432\u0435\u0442, \u0434\u0440\u0443\u0437\u044c\u044f!<\/p>\n<p> <\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0442\u0435\u0439 \u043c\u044b \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0432 <a href=\"https:\/\/github.com\/trekhleb\/javascript-algorithms\">\u044d\u0442\u043e\u043c \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u043c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438<\/a>. \u042d\u0442\u043e \u0432\u043e\u0441\u044c\u043c\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0441\u0435\u0440\u0438\u0438.<\/p>\n<p> <\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e\u0431 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u0445 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438.<\/p>\n<p> <\/p>\n<p>\u041a\u043e\u0434, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0432 \u044d\u0442\u043e\u0439 \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445 \u0441\u0435\u0440\u0438\u0438, \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 <a href=\"https:\/\/github.com\/harryheman\/algorithms-data-structures\">\u044d\u0442\u043e\u043c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438<\/a>.<\/p>\n<p> <\/p>\n<p>\u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e? \u0422\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0448\u0443 \u043f\u043e\u0434 \u043a\u0430\u0442.<\/p>\n<p><a name=\"habracut\"><\/a> <\/p>\n<ul>\n<li><a href=\"https:\/\/habr.com\/ru\/companies\/timeweb\/articles\/826424\/\">\u041f\u0435\u0440\u0432\u0430\u044f \u0447\u0430\u0441\u0442\u044c<\/a><\/li>\n<li><a href=\"https:\/\/habr.com\/ru\/companies\/timeweb\/articles\/828068\/\">\u0412\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c<\/a><\/li>\n<li><a href=\"https:\/\/habr.com\/ru\/companies\/timeweb\/articles\/832402\/\">\u0422\u0440\u0435\u0442\u044c\u044f \u0447\u0430\u0441\u0442\u044c<\/a><\/li>\n<li><a href=\"https:\/\/habr.com\/ru\/companies\/timeweb\/articles\/836782\/\">\u0427\u0435\u0442\u0432\u0435\u0440\u0442\u0430\u044f \u0447\u0430\u0441\u0442\u044c<\/a><\/li>\n<li><a href=\"https:\/\/habr.com\/ru\/companies\/timeweb\/articles\/838794\/\">\u041f\u044f\u0442\u0430\u044f \u0447\u0430\u0441\u0442\u044c<\/a><\/li>\n<li><a href=\"https:\/\/habr.com\/ru\/companies\/timeweb\/articles\/845544\/\">\u0428\u0435\u0441\u0442\u0430\u044f \u0447\u0430\u0441\u0442\u044c<\/a><\/li>\n<li><a href=\"https:\/\/habr.com\/ru\/companies\/timeweb\/articles\/856046\/\">\u0421\u0435\u0434\u044c\u043c\u0430\u044f \u0447\u0430\u0441\u0442\u044c<\/a><\/li>\n<\/ul>\n<p> <\/p>\n<h1 id=\"-sortirovka\">\u276f \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430<\/h1>\n<p> <\/p>\n<ul>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=PF7AqefS4MU\">\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438<\/a><\/li>\n<\/ul>\n<p> <\/p>\n<h2 id=\"-sortirovka-puzyrkom\">\u276f \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c<\/h2>\n<p> <\/p>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<ul>\n<li><a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A1%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0_%D0%BF%D1%83%D0%B7%D1%8B%D1%80%D1%8C%D0%BA%D0%BE%D0%BC\">\u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u044f<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=XXHFjKaIlHI\">YouTube<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=nmhjrI-aW5o\">\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/a><\/li>\n<\/ul>\n<p> <\/p>\n<p>\u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (bubble sort) \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u043f\u0430\u0440\u044b \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043c\u0435\u043d\u044f\u0435\u0442 \u0438\u0445 \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438. \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u044b \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435.<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/eg\/6g\/vc\/eg6gvcp2nf27et5sszaqm5hqnyo.gif\" data-src=\"https:\/\/habrastorage.org\/webt\/eg\/6g\/vc\/eg6gvcp2nf27et5sszaqm5hqnyo.gif\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<p><em>\u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c<\/em><\/p>\n<p> <\/p>\n<div class=\"scrollable-table\">\n<table>\n<thead>\n<tr>\n<th>\u041b\u0443\u0447\u0448\u0435\u0435<\/th>\n<th>\u0421\u0440\u0435\u0434\u043d\u0435\u0435<\/th>\n<th>\u0425\u0443\u0434\u0448\u0435\u0435<\/th>\n<th>\u041f\u0430\u043c\u044f\u0442\u044c<\/th>\n<th>\u0421\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>n<\/td>\n<td>n^2<\/td>\n<td>n^2<\/td>\n<td>1<\/td>\n<td>\u0414\u0430<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p> <\/p>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong><\/p>\n<p> <\/p>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0443\u043f\u0435\u0440\u043a\u043b\u0430\u0441\u0441\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438:<\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/sort.js import Comparator from '..\/..\/utils\/comparator'  export default class Sort {   constructor(originalCallbacks) {     \/\/ \u041a\u043e\u043b\u043b\u0431\u044d\u043a\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438     this.callbacks = Sort.initSortingCallbacks(originalCallbacks)     \/\/ \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432     this.comparator = new Comparator(this.callbacks.compareCallback)   }    \/\/ \u041a\u043e\u043b\u043b\u0431\u044d\u043a\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438   static initSortingCallbacks(originalCallbacks) {     const callbacks = originalCallbacks || {}     const stubCallback = () =&gt; {}      \/\/ \u0412\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432     callbacks.compareCallback = callbacks.compareCallback || undefined     \/\/ \u0412\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0438\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430     callbacks.visitingCallback = callbacks.visitingCallback || stubCallback      return callbacks   }    \/\/ \u041c\u0435\u0442\u043e\u0434 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u043a\u043b\u0430\u0441\u0441\u043e\u043c   sort() {     throw new Error('\u041c\u0435\u0442\u043e\u0434 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d')   } }<\/code><\/pre>\n<p> <\/p>\n<p>\u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043a\u043b\u0430\u0441\u0441 <code>Comparator<\/code>:<\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ utils\/comparator.js export default class Comparator {   constructor(fn) {     this.compare = fn || Comparator.defaultCompare   }    \/\/ \u0414\u0435\u0444\u043e\u043b\u0442\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0443\u0437\u043b\u043e\u0432   static defaultCompare(a, b) {     if (a === b) {       return 0     }     return a &lt; b ? -1 : 1   }    \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u043e   equal(a, b) {     return this.compare(a, b) === 0   }    \/\/ \u041c\u0435\u043d\u044c\u0448\u0435 \u0447\u0435\u043c   lessThan(a, b) {     return this.compare(a, b) &lt; 0   }    \/\/ \u0411\u043e\u043b\u044c\u0448\u0435 \u0447\u0435\u043c   greaterThan(a, b) {     return this.compare(a, b) &gt; 0   }    \/\/ \u041c\u0435\u043d\u044c\u0448\u0435 \u0438\u043b\u0438 \u0440\u0430\u0432\u043d\u043e   lessThanOrEqual(a, b) {     return this.lessThan(a, b) || this.equal(a, b)   }    \/\/ \u0411\u043e\u043b\u044c\u0448\u0435 \u0438\u043b\u0438 \u0440\u0430\u0432\u043d\u043e   greaterThanOrEqual(a, b) {     return this.greaterThan(a, b) || this.equal(a, b)   }    \/\/ \u0418\u043d\u0432\u0435\u0440\u0441\u0438\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f   reverse() {     const original = this.compare     this.compare = (a, b) =&gt; original(b, a)   } }<\/code><\/pre>\n<p> <\/p>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043f\u043e\u0434\u043a\u043b\u0430\u0441\u0441 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c:<\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/bubble-sort.js import Sort from '.\/sort'  export default class BubbleSort extends Sort {   sort(arr) {     \/\/ \u0418\u043d\u0434\u0438\u043a\u0430\u0442\u043e\u0440 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430     let swapped = false     \/\/ \u041a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0432\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u0435 \u0435\u0433\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438     \/\/ https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/structuredClone     const _arr = structuredClone(arr)      \/\/ \u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441\u043e \u0432\u0442\u043e\u0440\u043e\u0433\u043e     for (let i = 1; i &lt; _arr.length; i++) {       swapped = false        this.callbacks.visitingCallback(_arr[i])        \/\/ \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u0434\u0432\u0438\u0433\u0430\u0435\u043c\u0441\u044f \u0434\u043e `i`       for (let j = 0; j &lt; _arr.length - i; j++) {         this.callbacks.visitingCallback(_arr[j])          \/\/ \u041c\u0435\u043d\u044f\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0435\u0441\u0442\u0430\u043c\u0438, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u044b \u0432 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435         if (this.comparator.lessThan(_arr[j + 1], _arr[j])) {           ;[_arr[j], _arr[j + 1]] = [_arr[j + 1], _arr[j]]            \/\/ \u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0438\u043d\u0434\u0438\u043a\u0430\u0442\u043e\u0440           swapped = true         }       }        \/\/ \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d       if (!swapped) {         return _arr       }     }      return _arr   } }<\/code><\/pre>\n<p> <\/p>\n<p><strong>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f<\/strong><\/p>\n<p> <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0438 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/sort-tester.js export const sortedArr = [   1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ] export const reverseArr = [   20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, ] export const notSortedArr = [   15, 8, 5, 12, 10, 1, 16, 9, 11, 7, 20, 3, 2, 6, 17, 18, 4, 13, 14, 19, ] export const equalArr = [   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ] export const negativeArr = [-1, 0, 5, -10, 20, 13, -7, 3, 2, -3] export const negativeArrSorted = [-10, -7, -3, -1, 0, 2, 3, 5, 13, 20]  export class SortTester {   \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438   \/\/ \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 (\u043a\u043b\u0430\u0441\u0441\u0430) \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438   static testSort(SortingClass) {     const sorter = new SortingClass()      expect(sorter.sort([])).toEqual([])     expect(sorter.sort([1])).toEqual([1])     expect(sorter.sort([1, 2])).toEqual([1, 2])     expect(sorter.sort([2, 1])).toEqual([1, 2])     expect(sorter.sort([3, 4, 2, 1, 0, 0, 4, 3, 4, 2])).toEqual([       0, 0, 1, 2, 2, 3, 3, 4, 4, 4,     ])     expect(sorter.sort(sortedArr)).toEqual(sortedArr)     expect(sorter.sort(reverseArr)).toEqual(sortedArr)     expect(sorter.sort(notSortedArr)).toEqual(sortedArr)     expect(sorter.sort(equalArr)).toEqual(equalArr)   }    \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b   static testNegativeNumbersSort(SortingClass) {     const sorter = new SortingClass()     expect(sorter.sort(negativeArr)).toEqual(negativeArrSorted)   }    \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438   \/\/ \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432   static testSortWithCustomComparator(SortingClass) {     const callbacks = {       compareCallback: (a, b) =&gt; {         if (a.length === b.length) {           return 0         }         return a.length &lt; b.length ? -1 : 1       },     }      const sorter = new SortingClass(callbacks)      expect(sorter.sort([''])).toEqual([''])     expect(sorter.sort(['a'])).toEqual(['a'])     expect(sorter.sort(['aa', 'a'])).toEqual(['a', 'aa'])     expect(sorter.sort(['aa', 'q', 'bbbb', 'ccc'])).toEqual([       'q',       'aa',       'ccc',       'bbbb',     ])     expect(sorter.sort(['aa', 'aa'])).toEqual(['aa', 'aa'])   }    \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438   static testSortStability(SortingClass) {     const callbacks = {       compareCallback: (a, b) =&gt; {         if (a.length === b.length) {           return 0         }         return a.length &lt; b.length ? -1 : 1       },     }      const sorter = new SortingClass(callbacks)      expect(sorter.sort(['bb', 'aa', 'c'])).toEqual(['c', 'bb', 'aa'])     expect(sorter.sort(['aa', 'q', 'a', 'bbbb', 'ccc'])).toEqual([       'q',       'a',       'aa',       'ccc',       'bbbb',     ])   }    \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438   static testAlgorithmTimeComplexity(     SortingClass,     arrayToBeSorted,     numberOfVisits,   ) {     const visitingCallback = jest.fn()     const callbacks = { visitingCallback }     const sorter = new SortingClass(callbacks)      sorter.sort(arrayToBeSorted)      expect(visitingCallback).toHaveBeenCalledTimes(numberOfVisits)   } }<\/code><\/pre>\n<p> <\/p>\n<p>\u0422\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443:<\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/__tests__\/bubble-sort.test.js import BubbleSort from '..\/bubble-sort' import {   equalArr,   notSortedArr,   reverseArr,   sortedArr,   SortTester, } from '..\/sort-tester'  \/\/ \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 const SORTED_ARRAY_VISITING_COUNT = 20 const NOT_SORTED_ARRAY_VISITING_COUNT = 189 const REVERSE_SORTED_ARRAY_VISITING_COUNT = 209 const EQUAL_ARRAY_VISITING_COUNT = 20  describe('BubbleSort', () =&gt; {   it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432', () =&gt; {     SortTester.testSort(BubbleSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432', () =&gt; {     SortTester.testSortWithCustomComparator(BubbleSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443', () =&gt; {     SortTester.testSortStability(BubbleSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430', () =&gt; {     SortTester.testNegativeNumbersSort(BubbleSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       BubbleSort,       equalArr,       EQUAL_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       BubbleSort,       sortedArr,       SORTED_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       BubbleSort,       notSortedArr,       NOT_SORTED_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       BubbleSort,       reverseArr,       REVERSE_SORTED_ARRAY_VISITING_COUNT,     )   }) })<\/code><\/pre>\n<p> <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u044b:<\/p>\n<p> <\/p>\n<pre><code class=\"bash\">npm run test .\/algorithms\/sorting\/__tests__\/bubble-sort<\/code><\/pre>\n<p> <\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/82\/wy\/b8\/82wyb8ki_-dajodgnzgsigef3fi.png\" data-src=\"https:\/\/habrastorage.org\/webt\/82\/wy\/b8\/82wyb8ki_-dajodgnzgsigef3fi.png\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<h2 id=\"-sortirovka-vyborom\">\u276f \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c<\/h2>\n<p> <\/p>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<ul>\n<li><a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A1%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0_%D0%B2%D1%8B%D0%B1%D0%BE%D1%80%D0%BE%D0%BC\">\u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u044f<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=uCbV2xHxalk\">YouTube<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=xWBP4lzkoyM\">\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/a><\/li>\n<\/ul>\n<p> <\/p>\n<p>\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c (selection sort) \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0448\u0430\u0433\u043e\u0432:<\/p>\n<p> <\/p>\n<ol>\n<li>\u041d\u0430\u0445\u043e\u0434\u0438\u043c \u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 (\u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442).<\/li>\n<li>\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u043c \u0437\u0430\u043c\u0435\u043d\u0443 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 (\u0437\u0430\u043c\u0435\u043d\u0430 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0443\u0436\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0443\u0436\u043d\u043e\u0439 \u043f\u043e\u0437\u0438\u0446\u0438\u0438).<\/li>\n<li>\u0421\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u043e\u0441\u0442\u0430\u0432\u0448\u0443\u044e\u0441\u044f \u0447\u0430\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430, \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0432 \u0438\u0437 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b.<\/li>\n<\/ol>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/ie\/f1\/cv\/ief1cv7ux3lj0m4rbyylkqwuois.gif\" data-src=\"https:\/\/habrastorage.org\/webt\/ie\/f1\/cv\/ief1cv7ux3lj0m4rbyylkqwuois.gif\"\/> <\/p>\n<p> <\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/fv\/ri\/dn\/fvridngvmo1me6dgbdvu3l1gi0i.gif\" data-src=\"https:\/\/habrastorage.org\/webt\/fv\/ri\/dn\/fvridngvmo1me6dgbdvu3l1gi0i.gif\"\/> <\/p>\n<p> <\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/fv\/ri\/dn\/fvridngvmo1me6dgbdvu3l1gi0i.gif\" data-src=\"https:\/\/habrastorage.org\/webt\/fv\/ri\/dn\/fvridngvmo1me6dgbdvu3l1gi0i.gif\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<p><em>\u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c<\/em><\/p>\n<p> <\/p>\n<div class=\"scrollable-table\">\n<table>\n<thead>\n<tr>\n<th>\u041b\u0443\u0447\u0448\u0435\u0435<\/th>\n<th>\u0421\u0440\u0435\u0434\u043d\u0435\u0435<\/th>\n<th>\u0425\u0443\u0434\u0448\u0435\u0435<\/th>\n<th>\u041f\u0430\u043c\u044f\u0442\u044c<\/th>\n<th>\u0421\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>n^2<\/td>\n<td>n^2<\/td>\n<td>n^2<\/td>\n<td>1<\/td>\n<td>\u041d\u0435\u0442<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p> <\/p>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong><\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/selection-sort.js import Sort from '.\/sort'  export default class SelectionSort extends Sort {   sort(arr) {     \/\/ \u041a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0432\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u0435 \u0435\u0433\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438     \/\/ https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/structuredClone     const _arr = structuredClone(arr)      \/\/ \u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430     for (let i = 0; i &lt; _arr.length - 1; i++) {       \/\/ \u0418\u043d\u0434\u0435\u043a\u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430       let minIndex = i        this.callbacks.visitingCallback(_arr[i])        \/\/ \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u0434\u0432\u0438\u0433\u0430\u0435\u043c\u0441\u044f \u043e\u0442 `i + 1`       for (let j = i + 1; j &lt; _arr.length; j++) {         this.callbacks.visitingCallback(_arr[j])          if (this.comparator.lessThan(_arr[j], _arr[minIndex])) {           minIndex = j         }       }        \/\/ \u0415\u0441\u043b\u0438 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d \u043d\u043e\u0432\u044b\u0439 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442,       \/\/ \u043c\u0435\u043d\u044f\u0435\u043c \u043d\u0430 \u043d\u0435\u0433\u043e \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442       if (minIndex !== i) {         ;[_arr[i], _arr[minIndex]] = [_arr[minIndex], _arr[i]]       }     }      return _arr   } }<\/code><\/pre>\n<p> <\/p>\n<p><strong>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/__tests__\/selection-sort.test.js import SelectionSort from '..\/selection-sort' import {   equalArr,   notSortedArr,   reverseArr,   sortedArr,   SortTester, } from '..\/sort-tester'  \/\/ \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 const SORTED_ARRAY_VISITING_COUNT = 209 const NOT_SORTED_ARRAY_VISITING_COUNT = 209 const REVERSE_SORTED_ARRAY_VISITING_COUNT = 209 const EQUAL_ARRAY_VISITING_COUNT = 209  describe('SelectionSort', () =&gt; {   it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432', () =&gt; {     SortTester.testSort(SelectionSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f', () =&gt; {     SortTester.testSortWithCustomComparator(SelectionSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430', () =&gt; {     SortTester.testNegativeNumbersSort(SelectionSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       SelectionSort,       equalArr,       EQUAL_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       SelectionSort,       sortedArr,       SORTED_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       SelectionSort,       notSortedArr,       NOT_SORTED_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       SelectionSort,       reverseArr,       REVERSE_SORTED_ARRAY_VISITING_COUNT,     )   }) })<\/code><\/pre>\n<p> <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u044b:<\/p>\n<p> <\/p>\n<pre><code class=\"bash\">npm run test .\/algorithms\/sorting\/__tests__\/selection-sort<\/code><\/pre>\n<p> <\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/fv\/ap\/z7\/fvapz7fqotblssdgzgxqq7g2ou8.png\" data-src=\"https:\/\/habrastorage.org\/webt\/fv\/ap\/z7\/fvapz7fqotblssdgzgxqq7g2ou8.png\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<h2 id=\"-sortirovka-vstavkami\">\u276f \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438<\/h2>\n<p> <\/p>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<ul>\n<li><a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A1%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0_%D0%B2%D1%81%D1%82%D0%B0%D0%B2%D0%BA%D0%B0%D0%BC%D0%B8\">\u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u044f<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=SIrdTFF8-4s\">YouTube<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=OGzPmgsI-pQ\">\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/a><\/li>\n<\/ul>\n<p> <\/p>\n<p>\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 (insertion sort) \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u0440\u043e\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443, \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u0432\u0448\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0437\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0435\u0435 \u043c\u0435\u0441\u0442\u043e \u0441\u0440\u0435\u0434\u0438 \u0440\u0430\u043d\u0435\u0435 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432.<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/wa\/em\/oi\/waemoiprtiiev3wxqap9qdgpsmy.gif\" data-src=\"https:\/\/habrastorage.org\/webt\/wa\/em\/oi\/waemoiprtiiev3wxqap9qdgpsmy.gif\"\/> <\/p>\n<p> <\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/1r\/7_\/ep\/1r7_ep_plmt7m-d3l25ooj1q-le.gif\" data-src=\"https:\/\/habrastorage.org\/webt\/1r\/7_\/ep\/1r7_ep_plmt7m-d3l25ooj1q-le.gif\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<p><em>\u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c<\/em><\/p>\n<p> <\/p>\n<div class=\"scrollable-table\">\n<table>\n<thead>\n<tr>\n<th>\u041b\u0443\u0447\u0448\u0435\u0435<\/th>\n<th>\u0421\u0440\u0435\u0434\u043d\u0435\u0435<\/th>\n<th>\u0425\u0443\u0434\u0448\u0435\u0435<\/th>\n<th>\u041f\u0430\u043c\u044f\u0442\u044c<\/th>\n<th>\u0421\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>n<\/td>\n<td>n^2<\/td>\n<td>n^2<\/td>\n<td>1<\/td>\n<td>\u0414\u0430<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p> <\/p>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong><\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/insertion-sort.js import Sort from '.\/sort'  export default class InsertionSort extends Sort {   sort(arr) {     \/\/ \u041a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0432\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u0435 \u0435\u0433\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438     \/\/ https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/structuredClone     const _arr = structuredClone(arr)      \/\/ \u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441\u043e \u0432\u0442\u043e\u0440\u043e\u0433\u043e     for (let i = 1; i &lt; _arr.length; i++) {       this.callbacks.visitingCallback(_arr[i])        let currentIndex = i        \/\/ \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440,       \/\/ \u043f\u043e\u043a\u0430 \u0443 \u043d\u0430\u0441 \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438       \/\/ \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e       \/\/ (\u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u0440\u0430\u0432\u043e\u0433\u043e)       while (         _arr[currentIndex - 1] !== undefined &amp;&amp;         this.comparator.lessThan(_arr[currentIndex], _arr[currentIndex - 1])       ) {         this.callbacks.visitingCallback(_arr[currentIndex - 1])         \/\/ \u041c\u0435\u043d\u044f\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0435\u0441\u0442\u0430\u043c\u0438         ;[_arr[currentIndex - 1], _arr[currentIndex]] = [           _arr[currentIndex],           _arr[currentIndex - 1],         ]          \/\/ \u0414\u0432\u0438\u0433\u0430\u0435\u043c\u0441\u044f \u0432\u043b\u0435\u0432\u043e         currentIndex--       }     }      return _arr   } }<\/code><\/pre>\n<p> <\/p>\n<p><strong>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/__tests__\/insertion-sort.test.js import InsertionSort from '..\/insertion-sort' import {   equalArr,   notSortedArr,   reverseArr,   sortedArr,   SortTester, } from '..\/sort-tester'  \/\/ \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 const SORTED_ARRAY_VISITING_COUNT = 19 const NOT_SORTED_ARRAY_VISITING_COUNT = 100 const REVERSE_SORTED_ARRAY_VISITING_COUNT = 209 const EQUAL_ARRAY_VISITING_COUNT = 19  describe('InsertionSort', () =&gt; {   it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432', () =&gt; {     SortTester.testSort(InsertionSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f', () =&gt; {     SortTester.testSortWithCustomComparator(InsertionSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443', () =&gt; {     SortTester.testSortStability(InsertionSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430', () =&gt; {     SortTester.testNegativeNumbersSort(InsertionSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       InsertionSort,       equalArr,       EQUAL_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       InsertionSort,       sortedArr,       SORTED_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       InsertionSort,       notSortedArr,       NOT_SORTED_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       InsertionSort,       reverseArr,       REVERSE_SORTED_ARRAY_VISITING_COUNT,     )   }) })<\/code><\/pre>\n<p> <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u044b:<\/p>\n<p> <\/p>\n<pre><code class=\"bash\">npm run test .\/algorithms\/sorting\/__tests__\/insertion-sort<\/code><\/pre>\n<p> <\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/mm\/_h\/2j\/mm_h2jhqmh1afuaonxctt7xopdq.png\" data-src=\"https:\/\/habrastorage.org\/webt\/mm\/_h\/2j\/mm_h2jhqmh1afuaonxctt7xopdq.png\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<h2 id=\"-sortirovka-kuchey\">\u276f \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u0443\u0447\u0435\u0439<\/h2>\n<p> <\/p>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<ul>\n<li><a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%9F%D0%B8%D1%80%D0%B0%D0%BC%D0%B8%D0%B4%D0%B0%D0%BB%D1%8C%D0%BD%D0%B0%D1%8F_%D1%81%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0\">\u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u044f<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=FjtQhZGf5SI\">YouTube<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=MtQL_ll5KhQ\">\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/a><\/li>\n<\/ul>\n<p> <\/p>\n<p>\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u0443\u0447\u0435\u0439 (\u043f\u0438\u0440\u0430\u043c\u0438\u0434\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) (heap sort) \u2014 \u044d\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0432\u043e\u0435\u0433\u043e \u0440\u043e\u0434\u0430 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u0435\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0432\u044b\u0431\u043e\u0440\u043e\u043c. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u0438 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u0438. \u0417\u0430\u0442\u0435\u043c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f \u0437\u0430 \u0441\u0447\u0435\u0442 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438 \u0435\u0433\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0432 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c. \u0423\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u0435\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e, \u0447\u0442\u043e \u0434\u043b\u044f \u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0435 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a, \u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 &#171;\u041a\u0443\u0447\u0430&#187; (\u0441\u043c. \u0447\u0430\u0441\u0442\u044c 2, \u0440\u0430\u0437\u0434\u0435\u043b 6).<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/qd\/w0\/5p\/qdw05pto9jpnic4ac_7bi7nxhl4.gif\" data-src=\"https:\/\/habrastorage.org\/webt\/qd\/w0\/5p\/qdw05pto9jpnic4ac_7bi7nxhl4.gif\"\/> <\/p>\n<p> <\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/c_\/14\/qm\/c_14qm7m00fjyl7bbroqtdcimmy.gif\" data-src=\"https:\/\/habrastorage.org\/webt\/c_\/14\/qm\/c_14qm7m00fjyl7bbroqtdcimmy.gif\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<p><em>\u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c<\/em><\/p>\n<p> <\/p>\n<div class=\"scrollable-table\">\n<table>\n<thead>\n<tr>\n<th>\u041b\u0443\u0447\u0448\u0435\u0435<\/th>\n<th>\u0421\u0440\u0435\u0434\u043d\u0435\u0435<\/th>\n<th>\u0425\u0443\u0434\u0448\u0435\u0435<\/th>\n<th>\u041f\u0430\u043c\u044f\u0442\u044c<\/th>\n<th>\u0421\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>n log(n)<\/td>\n<td>n log(n)<\/td>\n<td>n log(n)<\/td>\n<td>1<\/td>\n<td>\u041d\u0435\u0442<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p> <\/p>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong><\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/heap-sort.js import Sort from '.\/sort' import MinHeap from '..\/..\/data-structures\/heap\/min-heap'  export default class HeapSort extends Sort {   sort(arr) {     const _arr = []     \/\/ \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043a\u0443\u0447\u0443     const minHeap = new MinHeap(this.callbacks.compareCallback)      \/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043a\u0443\u0447\u0443     for (const item of arr) {       this.callbacks.visitingCallback(item)        minHeap.add(item)     }      \/\/ \u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u0443\u0447\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432\u0441\u0435\u0433\u0434\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0432\u0435\u0440\u0445\u0443.     \/\/ \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0434\u043b\u044f \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430     while (!minHeap.isEmpty()) {       const item = minHeap.poll()        this.callbacks.visitingCallback(item)        _arr.push(item)     }      return _arr   } }<\/code><\/pre>\n<p> <\/p>\n<p><strong>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/__tests__\/heap-sort.test.js import HeapSort from '..\/heap-sort' import {   equalArr,   notSortedArr,   reverseArr,   sortedArr,   SortTester, } from '..\/sort-tester'  \/\/ \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438. \/\/ \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0432\u0440\u0435\u043c\u044f \u0440\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u0443\u0447\u0438, \/\/ \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0447\u0438\u0441\u043b\u0430 \u0431\u0443\u0434\u0443\u0442 \u0431\u041e\u043b\u044c\u0448\u0438\u043c\u0438 const SORTED_ARRAY_VISITING_COUNT = 40 const NOT_SORTED_ARRAY_VISITING_COUNT = 40 const REVERSE_SORTED_ARRAY_VISITING_COUNT = 40 const EQUAL_ARRAY_VISITING_COUNT = 40  describe('HeapSort', () =&gt; {   it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432', () =&gt; {     SortTester.testSort(HeapSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f', () =&gt; {     SortTester.testSortWithCustomComparator(HeapSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430', () =&gt; {     SortTester.testNegativeNumbersSort(HeapSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       HeapSort,       equalArr,       EQUAL_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       HeapSort,       sortedArr,       SORTED_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       HeapSort,       notSortedArr,       NOT_SORTED_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       HeapSort,       reverseArr,       REVERSE_SORTED_ARRAY_VISITING_COUNT,     )   }) })<\/code><\/pre>\n<p> <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u044b:<\/p>\n<p> <\/p>\n<pre><code class=\"bash\">npm run test .\/algorithms\/sorting\/__tests__\/heap-sort<\/code><\/pre>\n<p> <\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/pg\/qb\/ji\/pgqbjifnbmijg3axs6zldctn0uy.png\" data-src=\"https:\/\/habrastorage.org\/webt\/pg\/qb\/ji\/pgqbjifnbmijg3axs6zldctn0uy.png\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<h2 id=\"-sortirovka-sliyaniem\">\u276f \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c<\/h2>\n<p> <\/p>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<ul>\n<li><a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A1%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0_%D1%81%D0%BB%D0%B8%D1%8F%D0%BD%D0%B8%D0%B5%D0%BC\">\u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u044f<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=Qs3l8_wd_34\">YouTube<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=JSceec-wEyw\">\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/a><\/li>\n<\/ul>\n<p> <\/p>\n<p>\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c (merge sort) \u2014 \u044d\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0445\u043e\u0440\u043e\u0448\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0430 &#171;\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439&#187;. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u043c\u0435\u043d\u044c\u0448\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430. \u0417\u0430\u0442\u0435\u043c \u044d\u0442\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u0440\u0435\u0448\u0430\u044e\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u0438\u043b\u0438 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0435\u0441\u043b\u0438 \u0438\u0445 \u0440\u0430\u0437\u043c\u0435\u0440 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043c\u0430\u043b. \u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043a\u043e\u043c\u0431\u0438\u043d\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0432 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442.<\/p>\n<p> <\/p>\n<p>\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u0442\u0440\u0438 \u044d\u0442\u0430\u043f\u0430:<\/p>\n<p> <\/p>\n<ol>\n<li>\u0421\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0434\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0435 \u0447\u0430\u0441\u0442\u0438.<\/li>\n<li>\u041a\u0430\u0436\u0434\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0435\u043c \u0436\u0435 \u0441\u0430\u043c\u044b\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c.<\/li>\n<li>\u0414\u0432\u0430 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u043e\u0434\u0438\u043d.<\/li>\n<\/ol>\n<p> <\/p>\n<p>\u0415\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u044e\u0430\u043d\u0441\u043e\u0432:<\/p>\n<p> <\/p>\n<ol>\n<li>\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 \u043c\u0435\u043d\u044c\u0448\u0438\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u0440\u0430\u0437\u043c\u0435\u0440 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0435 \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0435\u0442 \u0435\u0434\u0438\u043d\u0438\u0446\u044b (\u043b\u044e\u0431\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0435\u0434\u0438\u043d\u0438\u0447\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b \u043c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c).<\/li>\n<li>\u041e\u0441\u043d\u043e\u0432\u043d\u0443\u044e \u0438\u0434\u0435\u044e \u0441\u043b\u0438\u044f\u043d\u0438\u044f \u0434\u0432\u0443\u0445 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u0442\u044c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435. \u041f\u0443\u0441\u0442\u044c \u043c\u044b \u0438\u043c\u0435\u0435\u043c \u0434\u0432\u0430 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u0422\u043e\u0433\u0434\u0430:<br \/> \n<ul>\n<li>\u0441\u043b\u0438\u044f\u043d\u0438\u0435 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432. \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043c\u044b \u0431\u0435\u0440\u0435\u043c \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u0435\u0440\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0435\u0433\u043e \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432. \u0421\u0447\u0435\u0442\u0447\u0438\u043a\u0438 \u043d\u043e\u043c\u0435\u0440\u043e\u0432 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u044b\u043b \u0432\u0437\u044f\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043d\u0430 1<\/li>\n<li>\u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0441\u0442\u0430\u0442\u043a\u0430. \u041a\u043e\u0433\u0434\u0430 \u043e\u0434\u0438\u043d \u0438\u0437 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0441\u044f, \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/zl\/dy\/1p\/zldy1p4lm5cyxmff6apc8vilyt0.gif\" data-src=\"https:\/\/habrastorage.org\/webt\/zl\/dy\/1p\/zldy1p4lm5cyxmff6apc8vilyt0.gif\"\/> <\/p>\n<p> <\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/n_\/za\/qz\/n_zaqzcw9bvv25txhyuuqba9gcg.png\" data-src=\"https:\/\/habrastorage.org\/webt\/n_\/za\/qz\/n_zaqzcw9bvv25txhyuuqba9gcg.png\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<p><em>\u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c<\/em><\/p>\n<p> <\/p>\n<div class=\"scrollable-table\">\n<table>\n<thead>\n<tr>\n<th>\u041b\u0443\u0447\u0448\u0435\u0435<\/th>\n<th>\u0421\u0440\u0435\u0434\u043d\u0435\u0435<\/th>\n<th>\u0425\u0443\u0434\u0448\u0435\u0435<\/th>\n<th>\u041f\u0430\u043c\u044f\u0442\u044c<\/th>\n<th>\u0421\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>n log(n)<\/td>\n<td>n log(n)<\/td>\n<td>n log(n)<\/td>\n<td>n<\/td>\n<td>\u0414\u0430<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p> <\/p>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong><\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/merge-sort.js import Sort from '.\/sort'  export default class MergeSort extends Sort {   \/\/ \u0421\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u0441\u043b\u0438\u044f\u043d\u0438\u044f   sort(arr) {     this.callbacks.visitingCallback(null)      \/\/ \u0415\u0441\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u0443\u0441\u0442\u043e\u0439 \u0438\u043b\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442,     \/\/ \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0435\u0433\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043d \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d     if (arr.length &lt;= 1) {       return arr     }      \/\/ \u0414\u0435\u043b\u0438\u043c \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u043f\u043e\u043b\u0430\u043c     const middleIndex = Math.floor(arr.length \/ 2)     const leftArray = arr.slice(0, middleIndex)     const rightArray = arr.slice(middleIndex, arr.length)      \/\/ \u0421\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u044b     const leftSortedArray = this.sort(leftArray)     const rightSortedArray = this.sort(rightArray)      \/\/ \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u043c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u044b \u0432 \u043e\u0434\u0438\u043d \u043c\u0430\u0441\u0441\u0438\u0432     return this.mergeSortedArrays(leftSortedArray, rightSortedArray)   }    \/\/ \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u0442 \u0434\u0432\u0430 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u0430   mergeSortedArrays(leftArray, rightArray) {     const _arr = []      \/\/ \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u0434\u043b\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432     let leftIndex = 0     let rightIndex = 0      while (leftIndex &lt; leftArray.length &amp;&amp; rightIndex &lt; rightArray.length) {       let minItem = null        \/\/ \u041d\u0430\u0445\u043e\u0434\u0438\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432       if (         this.comparator.lessThanOrEqual(           leftArray[leftIndex],           rightArray[rightIndex],         )       ) {         minItem = leftArray[leftIndex]         \/\/ \u0414\u0432\u0438\u0433\u0430\u0435\u043c\u0441\u044f \u0432\u043f\u0440\u0430\u0432\u043e         leftIndex += 1       } else {         minItem = rightArray[rightIndex]         \/\/ \u0414\u0432\u0438\u0433\u0430\u0435\u043c\u0441\u044f \u0432\u043b\u0435\u0432\u043e         rightIndex += 1       }        \/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432       _arr.push(minItem)        this.callbacks.visitingCallback(minItem)     }      \/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432     return _arr       .concat(leftArray.slice(leftIndex))       .concat(rightArray.slice(rightIndex))   } }<\/code><\/pre>\n<p> <\/p>\n<p><strong>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/__tests__\/merge-sort.test.js import MergeSort from '..\/merge-sort' import {   equalArr,   notSortedArr,   reverseArr,   sortedArr,   SortTester, } from '..\/sort-tester'  \/\/ \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 const SORTED_ARRAY_VISITING_COUNT = 79 const NOT_SORTED_ARRAY_VISITING_COUNT = 102 const REVERSE_SORTED_ARRAY_VISITING_COUNT = 87 const EQUAL_ARRAY_VISITING_COUNT = 79  describe('MergeSort', () =&gt; {   it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432', () =&gt; {     SortTester.testSort(MergeSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f', () =&gt; {     SortTester.testSortWithCustomComparator(MergeSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443', () =&gt; {     SortTester.testSortStability(MergeSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430', () =&gt; {     SortTester.testNegativeNumbersSort(MergeSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       MergeSort,       equalArr,       EQUAL_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       MergeSort,       sortedArr,       SORTED_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       MergeSort,       notSortedArr,       NOT_SORTED_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       MergeSort,       reverseArr,       REVERSE_SORTED_ARRAY_VISITING_COUNT,     )   }) })<\/code><\/pre>\n<p> <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u044b:<\/p>\n<p> <\/p>\n<pre><code class=\"bash\">npm run test .\/algorithms\/sorting\/__tests__\/merge-sort<\/code><\/pre>\n<p> <\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/uf\/iv\/qv\/ufivqv_e-sqfalvwry1wrnesf-u.png\" data-src=\"https:\/\/habrastorage.org\/webt\/uf\/iv\/qv\/ufivqv_e-sqfalvwry1wrnesf-u.png\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<h2 id=\"-bystraya-sortirovka\">\u276f \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430<\/h2>\n<p> <\/p>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<ul>\n<li><a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%91%D1%8B%D1%81%D1%82%D1%80%D0%B0%D1%8F_%D1%81%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0\">\u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u044f<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=4s-aG6yGGLU\">YouTube<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=hW6ct2ufHvs\">\u0413\u0440\u043e\u043a\u0430\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=PgBzjlCcFvc\">\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/a><\/li>\n<\/ul>\n<p> <\/p>\n<p>\u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0425\u043e\u0430\u0440\u0430) (quick sort) \u2014 \u044d\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d\u0430 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c). \u041f\u0440\u0438\u043d\u0446\u0438\u043f\u0438\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u043d\u0430 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u043c \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0438 \u043f\u043e\u0441\u043b\u0435 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 \u0434\u0432\u0435 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0435 \u0433\u0440\u0443\u043f\u043f\u044b.<\/p>\n<p> <\/p>\n<p>\u041e\u0431\u0449\u0430\u044f \u0438\u0434\u0435\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c:<\/p>\n<p> <\/p>\n<ol>\n<li>\u0412\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0438\u0437 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0442\u0430\u043a \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0439 \u043e\u043f\u043e\u0440\u043d\u044b\u0439 (pivot) \u044d\u043b\u0435\u043c\u0435\u043d\u0442. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u041e\u0442 \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430, \u043d\u043e \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043c\u043e\u0436\u0435\u0442 \u0441\u0438\u043b\u044c\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0435\u0442\u044c \u0435\u0433\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c.<\/li>\n<li>\u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c \u0438 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0438\u0445 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0430 \u0442\u0440\u0438 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u0445 \u043e\u0442\u0440\u0435\u0437\u043a\u0430, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0434\u0440\u0443\u0433 \u0437\u0430 \u0434\u0440\u0443\u0433\u043e\u043c: &#171;\u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0435\u043d\u044c\u0448\u0438\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e&#187;, &#171;\u0440\u0430\u0432\u043d\u044b\u0435&#187; \u0438 &#171;\u0431\u043e\u043b\u044c\u0448\u0438\u0435&#187;.<\/li>\n<li>\u0414\u043b\u044f \u043e\u0442\u0440\u0435\u0437\u043a\u043e\u0432 \u043c\u0435\u043d\u044c\u0448\u0435\u0433\u043e \u0438 \u0431\u043e\u043b\u044c\u0448\u0435\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0442\u0443 \u0436\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u0435\u0441\u043b\u0438 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 1.<\/li>\n<\/ol>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/3x\/5i\/wc\/3x5iwcrpdbdergnajwujv_ynrto.gif\" data-src=\"https:\/\/habrastorage.org\/webt\/3x\/5i\/wc\/3x5iwcrpdbdergnajwujv_ynrto.gif\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<p><em>\u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c<\/em><\/p>\n<p> <\/p>\n<div class=\"scrollable-table\">\n<table>\n<thead>\n<tr>\n<th>\u041b\u0443\u0447\u0448\u0435\u0435<\/th>\n<th>\u0421\u0440\u0435\u0434\u043d\u0435\u0435<\/th>\n<th>\u0425\u0443\u0434\u0448\u0435\u0435<\/th>\n<th>\u041f\u0430\u043c\u044f\u0442\u044c<\/th>\n<th>\u0421\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>n log(n)<\/td>\n<td>n log(n)<\/td>\n<td>n^2<\/td>\n<td>log(n)<\/td>\n<td>\u041d\u0435\u0442<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p> <\/p>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong><\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/quick-sort.js import Sort from '.\/sort'  export default class QuickSort extends Sort {   sort(arr) {     \/\/ \u041a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0432\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u0435 \u0435\u0433\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438     \/\/ https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/structuredClone     const _arr = structuredClone(arr)      \/\/ \u0415\u0441\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u0443\u0441\u0442\u043e\u0439 \u0438\u043b\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442,     \/\/ \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0435\u0433\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043d \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d     if (_arr.length &lt;= 1) {       return _arr     }      const leftArr = []     const rightArr = []      \/\/ \u0411\u0435\u0440\u0435\u043c \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e     const pivot = _arr.shift()     const centerArr = [pivot]      \/\/ \u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043c\u0435\u0436\u0434\u0443 \u043b\u0435\u0432\u044b\u043c, \u0446\u0435\u043d\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u043c \u0438 \u043f\u0440\u0430\u0432\u044b\u043c \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438     while (_arr.length) {       const currentItem = _arr.shift()        this.callbacks.visitingCallback(currentItem)        if (this.comparator.equal(currentItem, pivot)) {         centerArr.push(currentItem)       } else if (this.comparator.lessThan(currentItem, pivot)) {         leftArr.push(currentItem)       } else {         rightArr.push(currentItem)       }     }      \/\/ \u0421\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b     const leftArraySorted = this.sort(leftArr)     const rightArraySorted = this.sort(rightArr)      \/\/ \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e     return leftArraySorted.concat(centerArr, rightArraySorted)   } }<\/code><\/pre>\n<p> <\/p>\n<p><strong>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/__tests__\/quick-sort.test.js import QuickSort from '..\/quick-sort' import {   equalArr,   notSortedArr,   reverseArr,   sortedArr,   SortTester, } from '..\/sort-tester'  \/\/ \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 const SORTED_ARRAY_VISITING_COUNT = 190 const NOT_SORTED_ARRAY_VISITING_COUNT = 62 const REVERSE_SORTED_ARRAY_VISITING_COUNT = 190 const EQUAL_ARRAY_VISITING_COUNT = 19  describe('QuickSort', () =&gt; {   it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432', () =&gt; {     SortTester.testSort(QuickSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f', () =&gt; {     SortTester.testSortWithCustomComparator(QuickSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443', () =&gt; {     SortTester.testSortStability(QuickSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430', () =&gt; {     SortTester.testNegativeNumbersSort(QuickSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       QuickSort,       equalArr,       EQUAL_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       QuickSort,       sortedArr,       SORTED_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       QuickSort,       notSortedArr,       NOT_SORTED_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       QuickSort,       reverseArr,       REVERSE_SORTED_ARRAY_VISITING_COUNT,     )   }) })<\/code><\/pre>\n<p> <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u044b:<\/p>\n<p> <\/p>\n<pre><code class=\"bash\">npm run test .\/algorithms\/sorting\/__tests__\/quick-sort<\/code><\/pre>\n<p> <\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/jo\/j6\/ho\/joj6ho-isd40xjlrruzidljcayw.png\" data-src=\"https:\/\/habrastorage.org\/webt\/jo\/j6\/ho\/joj6ho-isd40xjlrruzidljcayw.png\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<h2 id=\"-sortirovka-shella\">\u276f \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0428\u0435\u043b\u043b\u0430<\/h2>\n<p> <\/p>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<ul>\n<li><a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A1%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0_%D0%A8%D0%B5%D0%BB%D0%BB%D0%B0\">\u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u044f<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=ddeLSDsYVp8\">YouTube<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=SHcPqUe2GZM\">\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=lvts84Qfo8o\">\u0414\u043b\u044f \u0441\u043c\u0435\u0445\u0430<\/a><\/li>\n<\/ul>\n<p> <\/p>\n<p>\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0428\u0435\u043b\u043b\u0430 (Shell sort) \u2014 \u044d\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u044f\u0432\u043b\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438. \u0418\u0434\u0435\u044f \u043c\u0435\u0442\u043e\u0434\u0430 \u0428\u0435\u043b\u043b\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0441\u0442\u043e\u044f\u0449\u0438\u0445 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u044f\u0434\u043e\u043c, \u043d\u043e \u0438 \u043d\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0434\u0440\u0443\u0433 \u043e\u0442 \u0434\u0440\u0443\u0433\u0430.<\/p>\n<p> <\/p>\n<p>\u041f\u0440\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u0428\u0435\u043b\u043b\u0430 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0441\u0442\u043e\u044f\u0449\u0438\u0435 \u043e\u0434\u0438\u043d \u043e\u0442 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0438 <code>d<\/code>. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 <code>d<\/code>, \u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0428\u0435\u043b\u043b\u0430 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u0440\u0438 <code>d = 1<\/code> (\u0442\u043e \u0435\u0441\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438).<\/p>\n<p> <\/p>\n<p>\u0425\u043e\u0442\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0428\u0435\u043b\u043b\u0430 \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435, \u0447\u0435\u043c \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430, \u043e\u043d\u0430 \u0438\u043c\u0435\u0435\u0442 \u0440\u044f\u0434 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432:<\/p>\n<p> <\/p>\n<ul>\n<li>\u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u0438 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u043e\u0434 \u0441\u0442\u0435\u043a<\/li>\n<li>\u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0434\u0435\u0433\u0440\u0430\u0434\u0430\u0446\u0438\u0438 \u043f\u0440\u0438 \u043d\u0435\u0443\u0434\u0430\u0447\u043d\u044b\u0445 \u043d\u0430\u0431\u043e\u0440\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445: \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043b\u0435\u0433\u043a\u043e \u0434\u0435\u0433\u0440\u0430\u0434\u0438\u0440\u0443\u0435\u0442 \u0434\u043e <code>O(n^2)<\/code>, \u0447\u0442\u043e \u0445\u0443\u0436\u0435, \u0447\u0435\u043c \u0445\u0443\u0434\u0448\u0435\u0435 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0428\u0435\u043b\u043b\u0430<\/li>\n<\/ul>\n<p> <\/p>\n<p><em>\u041f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b<\/em><\/p>\n<p> <\/p>\n<p>\u041d\u0443\u0436\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a <code>[ 35, 33, 42, 10, 14, 19, 27, 44 ]<\/code>. \u0411\u0435\u0440\u0435\u043c <code>d = 4<\/code> (<code>d<\/code> \u2014 \u044d\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b). \u0420\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0430 \u043f\u0430\u0440\u044b \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 <code>{35, 14}<\/code>, <code>{33, 19}<\/code>, <code>{42, 27}<\/code> \u0438 <code>{10, 44}<\/code>.<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/webt\/qp\/tr\/el\/qptrelklvpydyum8dnbi2jd4eho.jpeg\" data-src=\"https:\/\/habrastorage.org\/webt\/qp\/tr\/el\/qptrelklvpydyum8dnbi2jd4eho.jpeg\" data-blurred=\"true\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<p>\u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u043f\u0430\u0440\u044b \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u0438\u0445 \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438. \u041d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/webt\/_b\/mf\/y0\/_bmfy0x-yzyeaay6vjpkzhyinx4.jpeg\" data-src=\"https:\/\/habrastorage.org\/webt\/_b\/mf\/y0\/_bmfy0x-yzyeaay6vjpkzhyinx4.jpeg\" data-blurred=\"true\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<p>\u0411\u0435\u0440\u0435\u043c <code>d = 2<\/code>, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u0430\u0440\u044b <code>{14, 27, 35, 42}<\/code> \u0438 <code>{19, 10, 33, 44}<\/code>.<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/webt\/px\/kp\/ec\/pxkpecy3xklrt8i50dzc_-yx3yq.jpeg\" data-src=\"https:\/\/habrastorage.org\/webt\/px\/kp\/ec\/pxkpecy3xklrt8i50dzc_-yx3yq.jpeg\" data-blurred=\"true\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<p>\u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u043f\u0430\u0440\u044b \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u0438\u0445 \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438. \u041d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/webt\/0d\/wy\/pt\/0dwyptarmm-avfrzozdmlhotyr4.jpeg\" data-src=\"https:\/\/habrastorage.org\/webt\/0d\/wy\/pt\/0dwyptarmm-avfrzozdmlhotyr4.jpeg\" data-blurred=\"true\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<p>\u0411\u0435\u0440\u0435\u043c <code>d = 1<\/code> \u0438 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438:<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/webt\/im\/42\/od\/im42odfvn70hawaz6xw8fcwz1e8.jpeg\" data-src=\"https:\/\/habrastorage.org\/webt\/im\/42\/od\/im42odfvn70hawaz6xw8fcwz1e8.jpeg\" data-blurred=\"true\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<p><em>\u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c<\/em><\/p>\n<p> <\/p>\n<div class=\"scrollable-table\">\n<table>\n<thead>\n<tr>\n<th>\u041b\u0443\u0447\u0448\u0435\u0435<\/th>\n<th>\u0421\u0440\u0435\u0434\u043d\u0435\u0435<\/th>\n<th>\u0425\u0443\u0434\u0448\u0435\u0435<\/th>\n<th>\u041f\u0430\u043c\u044f\u0442\u044c<\/th>\n<th>\u0421\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>n log(n)<\/td>\n<td>\u0417\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0448\u0430\u0433\u0430<\/td>\n<td>n (log(n))^2<\/td>\n<td>1<\/td>\n<td>\u041d\u0435\u0442<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p> <\/p>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong><\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/shell-sort.js import Sort from '.\/sort'  export default class ShellSort extends Sort {   sort(arr) {     \/\/ \u041a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0432\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u0435 \u0435\u0433\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438     \/\/ https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/structuredClone     const _arr = structuredClone(arr)      \/\/ \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0448\u0430\u0433 - \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430     let step = Math.floor(_arr.length \/ 2)      \/\/ \u0414\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u0448\u0430\u0433 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0443\u043b\u044f     while (step &gt; 0) {       \/\/ \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0432\u0441\u0435 \u043f\u0430\u0440\u044b \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432       for (let i = 0; i &lt; _arr.length - step; i++) {         let currentIndex = i         let gapShiftedIndex = i + step          while (currentIndex &gt;= 0) {           this.callbacks.visitingCallback(_arr[currentIndex])            \/\/ \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438           if (             this.comparator.lessThan(_arr[gapShiftedIndex], _arr[currentIndex])           ) {             const tmp = _arr[currentIndex]              _arr[currentIndex] = _arr[gapShiftedIndex]              _arr[gapShiftedIndex] = tmp           }            gapShiftedIndex = currentIndex           currentIndex -= step         }       }        \/\/ \u0423\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0448\u0430\u0433 \u0432 2 \u0440\u0430\u0437\u0430       step = Math.floor(step \/ 2)     }      return _arr   } }<\/code><\/pre>\n<p> <\/p>\n<p><strong>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/__tests__\/shell-sort.test.js import ShellSort from '..\/shell-sort' import {   equalArr,   notSortedArr,   reverseArr,   sortedArr,   SortTester, } from '..\/sort-tester'  \/\/ \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 const SORTED_ARRAY_VISITING_COUNT = 320 const NOT_SORTED_ARRAY_VISITING_COUNT = 320 const REVERSE_SORTED_ARRAY_VISITING_COUNT = 320 const EQUAL_ARRAY_VISITING_COUNT = 320  describe('ShellSort', () =&gt; {   it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432', () =&gt; {     SortTester.testSort(ShellSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f', () =&gt; {     SortTester.testSortWithCustomComparator(ShellSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430', () =&gt; {     SortTester.testNegativeNumbersSort(ShellSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       ShellSort,       equalArr,       EQUAL_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       ShellSort,       sortedArr,       SORTED_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       ShellSort,       notSortedArr,       NOT_SORTED_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       ShellSort,       reverseArr,       REVERSE_SORTED_ARRAY_VISITING_COUNT,     )   }) })<\/code><\/pre>\n<p> <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u044b:<\/p>\n<p> <\/p>\n<pre><code class=\"bash\">npm run test .\/algorithms\/sorting\/__tests__\/shell-sort<\/code><\/pre>\n<p> <\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/qp\/rk\/zu\/qprkzu3knjsqxnduwxmiuzf8a1c.png\" data-src=\"https:\/\/habrastorage.org\/webt\/qp\/rk\/zu\/qprkzu3knjsqxnduwxmiuzf8a1c.png\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<h2 id=\"-sortirovka-podschetom\">\u276f \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c<\/h2>\n<p> <\/p>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<ul>\n<li><a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A1%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0_%D0%BF%D0%BE%D0%B4%D1%81%D1%87%D1%91%D1%82%D0%BE%D0%BC\">\u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u044f<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=eXIjpUq7yc4\">YouTube<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=7zuGmKfUt7s\">\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/a><\/li>\n<\/ul>\n<p> <\/p>\n<p>\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c (counting sort) \u2014 \u044d\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0447\u0438\u0441\u0435\u043b \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 (\u0441\u043f\u0438\u0441\u043a\u0430) \u0434\u043b\u044f \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u0430 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u0446\u0435\u043b\u0435\u0441\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u043e \u043b\u0438\u0448\u044c \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 \u0438\u043c\u0435\u044e\u0442 (\u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u044c \u0432) \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043c\u0430\u043b \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c\u044b\u043c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u0438\u043b\u043b\u0438\u043e\u043d \u043d\u0430\u0442\u0443\u0440\u0430\u043b\u044c\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u043c\u0435\u043d\u044c\u0448\u0438\u0445 1000. \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u2014 \u044d\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b. \u041e\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0447\u0438\u0441\u0435\u043b \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043e\u0447\u0435\u043d\u044c \u043c\u0430\u043b.<\/p>\n<p> <\/p>\n<p><em>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c<\/em><\/p>\n<p> <\/p>\n<ol>\n<li>\u041d\u0430 \u043f\u0435\u0440\u0432\u043e\u043c \u0448\u0430\u0433\u0435 \u043c\u044b \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0435\u0433\u043e\u0441\u044f \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 <code>A<\/code>. \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 <code>C<\/code>:<\/li>\n<\/ol>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/60\/to\/cv\/60tocvzrtjeb1hhec39fj1q__sm.gif\" data-src=\"https:\/\/habrastorage.org\/webt\/60\/to\/cv\/60tocvzrtjeb1hhec39fj1q__sm.gif\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<ol>\n<li>\u041d\u0430 \u0432\u0442\u043e\u0440\u043e\u043c \u0448\u0430\u0433\u0435 \u043c\u044b \u0441\u0447\u0438\u0442\u0430\u0435\u043c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 <code>A<\/code> \u043c\u0435\u043d\u044c\u0448\u0435 \u0438\u043b\u0438 \u0440\u0430\u0432\u043d\u044b \u0442\u0435\u043a\u0443\u0449\u0435\u043c\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u0443. <code>Ci<\/code> \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u0438\u043b\u0438 \u0440\u0430\u0432\u043d\u044b\u0445 <code>i<\/code>:<\/li>\n<\/ol>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/zz\/jw\/da\/zzjwda2sfrja5jfhmzauitm75xe.png\" data-src=\"https:\/\/habrastorage.org\/webt\/zz\/jw\/da\/zzjwda2sfrja5jfhmzauitm75xe.png\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<ol>\n<li>\u041d\u0430 \u0442\u0440\u0435\u0442\u044c\u0435\u043c \u0448\u0430\u0433\u0435 \u043c\u044b \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 <code>A<\/code> \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 <code>C<\/code>. \u0414\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043c\u0430\u0441\u0441\u0438\u0432 <code>B<\/code>:<\/li>\n<\/ol>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/21\/5b\/hd\/215bhdz2mwkgn6p3js4ojqdsckw.gif\" data-src=\"https:\/\/habrastorage.org\/webt\/21\/5b\/hd\/215bhdz2mwkgn6p3js4ojqdsckw.gif\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<p><em>\u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c<\/em><\/p>\n<p> <\/p>\n<div class=\"scrollable-table\">\n<table>\n<thead>\n<tr>\n<th>\u041b\u0443\u0447\u0448\u0435\u0435<\/th>\n<th>\u0421\u0440\u0435\u0434\u043d\u0435\u0435<\/th>\n<th>\u0425\u0443\u0434\u0448\u0435\u0435<\/th>\n<th>\u041f\u0430\u043c\u044f\u0442\u044c<\/th>\n<th>\u0421\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/th>\n<th>\u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>n + r<\/td>\n<td>n + r<\/td>\n<td>n + r<\/td>\n<td>n + r<\/td>\n<td>\u0414\u0430<\/td>\n<td>r \u2014 \u044d\u0442\u043e \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p> <\/p>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong><\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/counting-sort.js import Sort from '.\/sort'  export default class CountingSort extends Sort {   sort(arr, smallestItem, biggestItem) {     \/\/ \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0435 \u0447\u0438\u0441\u043b\u0430     \/\/ \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u043e\u0432 (buckets) \u043f\u043e\u0437\u0436\u0435     let _smallestItem = smallestItem || 0     let _biggestItem = biggestItem || 0      if (!smallestItem || !biggestItem) {       arr.forEach((item) =&gt; {         this.callbacks.visitingCallback(item)          \/\/ \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442         if (this.comparator.greaterThan(item, _biggestItem)) {           _biggestItem = item         }          \/\/ \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442         if (this.comparator.lessThan(item, _smallestItem)) {           _smallestItem = item         }       })     }      \/\/ \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c     \/\/ \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0439 (\u0447\u0430\u0441\u0442\u043e\u0442\u0443) \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 `arr`     const buckets = new Array(_biggestItem - _smallestItem + 1).fill(0)     arr.forEach((item) =&gt; {       this.callbacks.visitingCallback(item)        buckets[item - _smallestItem]++     })      \/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u0447\u0430\u0441\u0442\u043e\u0442\u044b \u043a \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u0432 \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0435,     \/\/ \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0447\u0438\u0441\u0435\u043b \u043c\u0435\u043d\u044c\u0448\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u0441\u0442\u043e\u044f\u0442\u044c     \/\/ \u0441\u043b\u0435\u0432\u0430 \u043e\u0442 \u043d\u0435\u0433\u043e     for (let i = 1; i &lt; buckets.length; i++) {       buckets[i] += buckets[i - 1]     }      \/\/ \u0421\u0434\u0432\u0438\u0433\u0430\u0435\u043c \u0447\u0430\u0441\u0442\u043e\u0442\u044b \u0432\u043f\u0440\u0430\u0432\u043e, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430.     \/\/ \u0415\u0441\u043b\u0438 \u043c\u044b \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c, \u0442\u043e `buckets[5]`, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e\u043a\u0430\u0436\u0435\u0442, \u0441\u043a\u043e\u043b\u044c\u043a\u043e     \/\/ \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043c\u0435\u043d\u044c\u0448\u0438\u0445 5, \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0441\u043b\u0435\u0432\u0430 \u043e\u0442 5 \u0432 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435,     \/\/ \u0412\u041a\u041b\u042e\u0427\u0410\u042f 5. \u041f\u043e\u0441\u043b\u0435 \u0441\u0434\u0432\u0438\u0433\u0430 5 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u043e     buckets.pop()     buckets.unshift(0)      \/\/ \u0424\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432     const sortedArr = new Array(arr.length).fill(null)     arr.forEach((item) =&gt; {       this.callbacks.visitingCallback(item)        \/\/ \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435       const sortedPosition = buckets[item - _smallestItem]       \/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435       sortedArr[sortedPosition] = item       \/\/ \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0435 \u0434\u043b\u044f \u0431\u0443\u0434\u0443\u0449\u0438\u0445 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0445 \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u0439       buckets[item - _smallestItem]++     })      return sortedArr   } }<\/code><\/pre>\n<p> <\/p>\n<p><strong>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/__tests__\/counting-sort.test.js import CountingSort from '..\/counting-sort' import {   equalArr,   notSortedArr,   reverseArr,   sortedArr,   SortTester, } from '..\/sort-tester'  \/\/ \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 const SORTED_ARRAY_VISITING_COUNT = 60 const NOT_SORTED_ARRAY_VISITING_COUNT = 60 const REVERSE_SORTED_ARRAY_VISITING_COUNT = 60 const EQUAL_ARRAY_VISITING_COUNT = 60  describe('CountingSort', () =&gt; {   it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432', () =&gt; {     SortTester.testSort(CountingSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430', () =&gt; {     SortTester.testNegativeNumbersSort(CountingSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435\/\u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0446\u0435\u043b\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 \u0434\u043b\u044f \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438', () =&gt; {     const visitingCallback = jest.fn()     const sorter = new CountingSort({ visitingCallback })      \/\/ \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0435 \u0447\u0438\u0441\u043b\u043e     const biggestElement = Math.max(...notSortedArr)      \/\/ \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0435\u0435 \u0447\u0438\u0441\u043b\u043e     const smallestElement = Math.min(...notSortedArr)      const sortedArray = sorter.sort(       notSortedArr,       smallestElement,       biggestElement,     )      expect(sortedArray).toEqual(sortedArr)     \/\/ \u041e\u0431\u044b\u0447\u043d\u043e `visitingCallback()` \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f 60 \u0440\u0430\u0437, \u043d\u043e \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435     \/\/ \u043e\u043d \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0432\u044b\u0437\u0432\u0430\u043d \u0442\u043e\u043b\u044c\u043a\u043e 40 \u0440\u0430\u0437     expect(visitingCallback).toHaveBeenCalledTimes(40)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       CountingSort,       equalArr,       EQUAL_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       CountingSort,       sortedArr,       SORTED_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       CountingSort,       notSortedArr,       NOT_SORTED_ARRAY_VISITING_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       CountingSort,       reverseArr,       REVERSE_SORTED_ARRAY_VISITING_COUNT,     )   }) })<\/code><\/pre>\n<p> <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u044b:<\/p>\n<p> <\/p>\n<pre><code class=\"bash\">npm run test .\/algorithms\/sorting\/__tests__\/counting-sort<\/code><\/pre>\n<p> <\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/p6\/1-\/6p\/p61-6pdccmnjznczzx-5kglq9nu.png\" data-src=\"https:\/\/habrastorage.org\/webt\/p6\/1-\/6p\/p61-6pdccmnjznczzx-5kglq9nu.png\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<h2 id=\"-porazryadnaya-sortirovka\">\u276f \u041f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430<\/h2>\n<p> <\/p>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<ul>\n<li><a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%9F%D0%BE%D1%80%D0%B0%D0%B7%D1%80%D1%8F%D0%B4%D0%BD%D0%B0%D1%8F_%D1%81%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0\">\u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u044f<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=DOM5ZHnf3Gw\">YouTube<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=nu4gDuFabIM\">\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/a><\/li>\n<\/ul>\n<p> <\/p>\n<p>\u041f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (radix sort) \u2014 \u044d\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0437\u0430 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0438 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b, \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0445 \u0446\u0438\u0444\u0440\u0430\u043c\u0438. \u041e\u0434\u043d\u0430\u043a\u043e \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430 \u043b\u044e\u0431\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430 \u0446\u0435\u043b\u044b\u043c\u0438 \u0447\u0438\u0441\u043b\u0430\u043c\u0438, \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u0440\u0438\u0433\u043e\u0434\u0435\u043d \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043b\u044e\u0431\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u044c \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 &#171;\u0440\u0430\u0437\u0440\u044f\u0434\u044b&#187;, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0430\u0431\u043e\u0440 \u0431\u0430\u0439\u0442\u043e\u0432.<\/p>\n<p> <\/p>\n<p>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u043e: \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043a\u0440\u0430\u0439\u043d\u0435\u0433\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u0430, \u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043f\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c \u044d\u0442\u043e\u0433\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f, \u0437\u0430\u0442\u0435\u043c \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u0430, \u0441\u043e\u0441\u0435\u0434\u043d\u0435\u0433\u043e, \u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043b\u0438\u0431\u043e \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u044d\u0442\u043e\u0433\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u0430 \u0432\u043d\u0443\u0442\u0440\u0438 \u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u043f\u0440\u043e\u0445\u043e\u0434\u0435 \u0433\u0440\u0443\u043f\u043f, \u043b\u0438\u0431\u043e \u043f\u0435\u0440\u0435\u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u0446\u0435\u043b\u043e\u043c, \u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044f \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a, \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0443\u0442\u044b\u0439 \u043f\u0440\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435. \u0417\u0430\u0442\u0435\u043c \u0442\u043e\u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u0430 \u0438 \u0442.\u0434.<\/p>\n<p> <\/p>\n<p>\u0412\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c\u044b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0430 \u043c\u043e\u0436\u043d\u043e \u0432 \u0440\u0430\u0437\u043d\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0434\u0432\u0430 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u044d\u0442\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438. \u0414\u043b\u044f \u0447\u0438\u0441\u0435\u043b \u043e\u043d\u0438 \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u0445 \u0437\u043d\u0430\u0447\u0438\u043c\u043e\u0441\u0442\u0438 \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432 \u0447\u0438\u0441\u043b\u0430, \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0442\u0430\u043a: \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0440\u043e\u0432\u043d\u044f\u0442\u044c \u0437\u0430\u043f\u0438\u0441\u0438 \u0447\u0438\u0441\u0435\u043b \u0432 \u0441\u0442\u043e\u0440\u043e\u043d\u0443 \u043c\u0435\u043d\u0435\u0435 \u0437\u043d\u0430\u0447\u0430\u0449\u0438\u0445 \u0446\u0438\u0444\u0440 (\u043f\u043e \u043f\u0440\u0430\u0432\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u0435, \u0432 \u0441\u0442\u043e\u0440\u043e\u043d\u0443 \u0435\u0434\u0438\u043d\u0438\u0446 \u2014 LSD \u043e\u0442 least significant digit) \u0438\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u0437\u043d\u0430\u0447\u0430\u0449\u0438\u0445 \u0446\u0438\u0444\u0440 (\u043f\u043e \u043b\u0435\u0432\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u0435, \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0431\u043e\u043b\u0435\u0435 \u0437\u043d\u0430\u0447\u0430\u0449\u0438\u0445 \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432 \u2014 MSD \u043e\u0442 most significant digit).<\/p>\n<p> <\/p>\n<p>\u041f\u0440\u0438 LSD-\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0440\u044f\u0434\u043e\u043a, \u0443\u043c\u0435\u0441\u0442\u043d\u044b\u0439 \u0434\u043b\u044f \u0447\u0438\u0441\u0435\u043b. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: 1, 2, 9, 10, 21, 100, 200, 201, 202, 210. \u0417\u0434\u0435\u0441\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043f\u043e \u0435\u0434\u0438\u043d\u0438\u0446\u0430\u043c, \u0437\u0430\u0442\u0435\u043c \u043f\u043e \u0434\u0435\u0441\u044f\u0442\u043a\u0430\u043c, \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044f \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043f\u043e \u0435\u0434\u0438\u043d\u0438\u0446\u0430\u043c \u0432\u043d\u0443\u0442\u0440\u0438 \u0434\u0435\u0441\u044f\u0442\u043a\u043e\u0432, \u0437\u0430\u0442\u0435\u043c \u043f\u043e \u0441\u043e\u0442\u043d\u044f\u043c, \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044f \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043f\u043e \u0434\u0435\u0441\u044f\u0442\u043a\u0430\u043c \u0438 \u0435\u0434\u0438\u043d\u0438\u0446\u0430\u043c \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u043e\u0442\u0435\u043d, \u0438 \u0442.\u0434.<\/p>\n<p> <\/p>\n<p>\u041f\u0440\u0438 MSD-\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0430\u043b\u0444\u0430\u0432\u0438\u0442\u043d\u044b\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043c\u0435\u0441\u0442\u0435\u043d \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u0442\u0440\u043e\u043a. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 &#171;b, c, d, e, f, g, h, i, j, ba&#187; \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043a\u0430\u043a &#171;b, ba, c, d, e, f, g, h, i, j&#187;. \u0415\u0441\u043b\u0438 MSD \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u043a \u0447\u0438\u0441\u043b\u0430\u043c, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0430\u043b\u0444\u0430\u0432\u0438\u0442\u043d\u044b\u0439, \u0430 \u043d\u0435 \u0447\u0438\u0441\u043b\u043e\u0432\u043e\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a: 1, 10, 100, 2, 200, 201, 202, 21, 210, 9.<\/p>\n<p> <\/p>\n<p><em>\u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c<\/em><\/p>\n<p> <\/p>\n<div class=\"scrollable-table\">\n<table>\n<thead>\n<tr>\n<th>\u041b\u0443\u0447\u0448\u0435\u0435<\/th>\n<th>\u0421\u0440\u0435\u0434\u043d\u0435\u0435<\/th>\n<th>\u0425\u0443\u0434\u0448\u0435\u0435<\/th>\n<th>\u041f\u0430\u043c\u044f\u0442\u044c<\/th>\n<th>\u0421\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/th>\n<th>\u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>n * k<\/td>\n<td>n * k<\/td>\n<td>n * k<\/td>\n<td>n + k<\/td>\n<td>\u0414\u0430<\/td>\n<td>k \u2014 \u044d\u0442\u043e \u0434\u043b\u0438\u043d\u0430 \u0441\u0430\u043c\u043e\u0433\u043e \u0434\u043b\u0438\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u044e\u0447\u0430<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/er\/sz\/rs\/erszrszsth66hywpbjub6ocbn_e.png\" data-src=\"https:\/\/habrastorage.org\/webt\/er\/sz\/rs\/erszrszsth66hywpbjub6ocbn_e.png\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong><\/p>\n<p> <\/p>\n<p>\u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0430\u0435\u043c \u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430:<\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/radix-sort.js import Sort from '.\/sort'  \/\/ `charCode` (\u043a\u043e\u0434\u043e\u0432\u0430\u044f \u0435\u0434\u0438\u043d\u0438\u0446\u0430 UTF-16) (a = 97, b = 98 \u0438 \u0442.\u0434.) \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \/\/ \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0442\u044c \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u043a \u0433\u0440\u0443\u043f\u043f\u0430\u043c \u043e\u0442 0 \u0434\u043e 25 (\u0432 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u043e\u043c \u0430\u043b\u0444\u0430\u0432\u0438\u0442\u0435 26 \u0431\u0443\u043a\u0432) const BASE_CHAR_CODE = 97 \/\/ 0-9 const NUMBER_OF_POSSIBLE_DIGITS = 10 \/\/ a-z const ENGLISH_ALPHABET_LENGTH = 26  export default class RadixSort extends Sort {   sort(arr) {     \/\/ \u0412\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u043c\u0435\u0442\u044c \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0439 \u0442\u0438\u043f     const isArrayOfNumbers = this.isArrayOfNumbers(arr)      \/\/ \u041a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0432\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u0435 \u0435\u0433\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438     \/\/ https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/structuredClone     let sortedArr = structuredClone(arr)     \/\/ \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u043d\u0443\u0436\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439     const numPasses = this.determineNumPasses(sortedArr)      \/\/ \u0424\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u044b     for (let i = 0; i &lt; numPasses; i++) {       const buckets = isArrayOfNumbers         ? this.placeItemsInNumBucket(sortedArr, i)         : this.placeItemsInCharBucket(sortedArr, i, numPasses)        \/\/ \u0420\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u044b\u0432\u0430\u0435\u043c \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u044b       sortedArr = buckets.flat()     }      return sortedArr   } }<\/code><\/pre>\n<p> <\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>isArrayOfNumbers<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0442\u0438\u043f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 (\u0447\u0438\u0441\u043b\u0430 \u0438\u043b\u0438 \u0441\u0442\u0440\u043e\u043a\u0438), \u0430 \u043c\u0435\u0442\u043e\u0434 <code>determineNumPasses<\/code> \u2014 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439:<\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043b\u0438\u043d\u043e\u0439 \u0441\u0430\u043c\u043e\u0433\u043e \u0434\u043b\u0438\u043d\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \/\/ \u0414\u043b\u044f \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b - \u044d\u0442\u043e `log10(num)`, \u0434\u043b\u044f \u0441\u0442\u0440\u043e\u043a - \u0434\u043b\u0438\u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0438 determineNumPasses(arr) {   if (this.isArrayOfNumbers(arr)) {     return Math.floor(Math.log10(Math.max(...arr))) + 1     \/\/ return Math.max(...arr.map((i) =&gt; i.toString().length))   }    return Math.max(...arr.map((i) =&gt; i.length)) }  isArrayOfNumbers(arr) {   return Number.isInteger(arr[0]) }<\/code><\/pre>\n<p> <\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u043e\u0432\u043e\u0433\u043e \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430:<\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">placeItemsInNumBucket(arr, index) {   \/\/ \u042d\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0438\u0436\u0435 \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0443\u043f\u043f\u044b,   \/\/ \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0438\u0442 \u0447\u0438\u0441\u043b\u043e   const modded = 10 ** (index + 1)   const divided = 10 ** index   const buckets = this.createBuckets(NUMBER_OF_POSSIBLE_DIGITS)    arr.forEach((item) =&gt; {     this.callbacks.visitingCallback(item)      if (item &lt; divided) {       buckets[0].push(item)     } else {       \/\/ \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 `1052` \u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 `1` (\u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 `0`). \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442,       \/\/ \u0447\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c `5` \u043a\u0430\u043a \u0433\u0440\u0443\u043f\u043f\u0443. `modded` \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u043d\u044f\u0442\u044c\u0441\u044f `10 ** (1 + 1)`       \/\/ \u0438\u043b\u0438 `100`. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0431\u0435\u0440\u0435\u043c `1052 % 100 (52)`, \u0434\u0435\u043b\u0438\u043c \u043d\u0430 `10 (5.2)` \u0438 \u043e\u043a\u0440\u0443\u0433\u043b\u044f\u0435\u043c \u0434\u043e `5`       const currentDigit = Math.floor((item % modded) \/ divided)       buckets[currentDigit].push(item)     }   })    return buckets }<\/code><\/pre>\n<p> <\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430:<\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">createBuckets(size) {   return new Array(size).fill().map(() =&gt; []) }<\/code><\/pre>\n<p> <\/p>\n<p>\u041c\u0435\u0442\u043e\u0434\u044b \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u0433\u043e \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u043e\u0432\u043e\u0439 \u0435\u0434\u0438\u043d\u0438\u0446\u044b \u0441\u0438\u043c\u0432\u043e\u043b\u0430:<\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">placeItemsInCharBucket(arr, index, numPasses) {   const buckets = this.createBuckets(ENGLISH_ALPHABET_LENGTH)    arr.forEach((item) =&gt; {     this.callbacks.visitingCallback(item)     const currentBucket = this.getCharCodeOfItemAtIndex(       item,       index,       numPasses,     )     buckets[currentBucket].push(item)   })    return buckets }  getCharCodeOfItemAtIndex(item, index, numPasses) {   \/\/ \u041f\u043e\u043c\u0435\u0449\u0430\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0433\u0440\u0443\u043f\u043f\u0443,   \/\/ \u0435\u0441\u043b\u0438 \u043e\u043d \u043d\u0435 \u0433\u043e\u0442\u043e\u0432 \u043a \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u044e   if (numPasses - index &gt; item.length) {     return ENGLISH_ALPHABET_LENGTH - 1   }    \/\/ \u0415\u0441\u043b\u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0443\u043f\u043f\u044b,   \/\/ \u0438\u043d\u0430\u0447\u0435, \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435   const charPos = index &gt; item.length - 1 ? 0 : item.length - index - 1    \/\/ https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/String\/charCodeAt   return item.toLowerCase().charCodeAt(charPos) - BASE_CHAR_CODE }<\/code><\/pre>\n<p> <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430:<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"javascript\">import Sort from '.\/sort'  \/\/ `charCode` (\u043a\u043e\u0434\u043e\u0432\u0430\u044f \u0435\u0434\u0438\u043d\u0438\u0446\u0430 UTF-16) (a = 97, b = 98 \u0438 \u0442.\u0434.) \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \/\/ \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0442\u044c \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u043a \u0433\u0440\u0443\u043f\u043f\u0430\u043c \u043e\u0442 0 \u0434\u043e 25 (\u0432 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u043e\u043c \u0430\u043b\u0444\u0430\u0432\u0438\u0442\u0435 26 \u0431\u0443\u043a\u0432) const BASE_CHAR_CODE = 97 \/\/ 0-9 const NUMBER_OF_POSSIBLE_DIGITS = 10 \/\/ a-z const ENGLISH_ALPHABET_LENGTH = 26  export default class RadixSort extends Sort {   sort(arr) {     \/\/ \u0412\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u043c\u0435\u0442\u044c \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0439 \u0442\u0438\u043f     const isArrayOfNumbers = this.isArrayOfNumbers(arr)      \/\/ \u041a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0432\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u0435 \u0435\u0433\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438     \/\/ https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/structuredClone     let sortedArr = structuredClone(arr)     \/\/ \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u043d\u0443\u0436\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439     const numPasses = this.determineNumPasses(sortedArr)      \/\/ \u0424\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u044b     for (let i = 0; i &lt; numPasses; i++) {       const buckets = isArrayOfNumbers         ? this.placeItemsInNumBucket(sortedArr, i)         : this.placeItemsInCharBucket(sortedArr, i, numPasses)        \/\/ \u0420\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u044b\u0432\u0430\u0435\u043c \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u044b       sortedArr = buckets.flat()     }      return sortedArr   }    placeItemsInNumBucket(arr, index) {     \/\/ \u042d\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0438\u0436\u0435 \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0443\u043f\u043f\u044b,     \/\/ \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0438\u0442 \u0447\u0438\u0441\u043b\u043e     const modded = 10 ** (index + 1)     const divided = 10 ** index     const buckets = this.createBuckets(NUMBER_OF_POSSIBLE_DIGITS)      arr.forEach((item) =&gt; {       this.callbacks.visitingCallback(item)        if (item &lt; divided) {         buckets[0].push(item)       } else {         \/\/ \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 `1052` \u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 `1` (\u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 `0`). \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442,         \/\/ \u0447\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c `5` \u043a\u0430\u043a \u0433\u0440\u0443\u043f\u043f\u0443. `modded` \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u043d\u044f\u0442\u044c\u0441\u044f `10 ** (1 + 1)`         \/\/ \u0438\u043b\u0438 `100`. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0431\u0435\u0440\u0435\u043c `1052 % 100 (52)`, \u0434\u0435\u043b\u0438\u043c \u043d\u0430 `10 (5.2)` \u0438 \u043e\u043a\u0440\u0443\u0433\u043b\u044f\u0435\u043c \u0434\u043e `5`         const currentDigit = Math.floor((item % modded) \/ divided)         buckets[currentDigit].push(item)       }     })      return buckets   }    placeItemsInCharBucket(arr, index, numPasses) {     const buckets = this.createBuckets(ENGLISH_ALPHABET_LENGTH)      arr.forEach((item) =&gt; {       this.callbacks.visitingCallback(item)       const currentBucket = this.getCharCodeOfItemAtIndex(         item,         index,         numPasses,       )       buckets[currentBucket].push(item)     })      return buckets   }    getCharCodeOfItemAtIndex(item, index, numPasses) {     \/\/ \u041f\u043e\u043c\u0435\u0449\u0430\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0433\u0440\u0443\u043f\u043f\u0443,     \/\/ \u0435\u0441\u043b\u0438 \u043e\u043d \u043d\u0435 \u0433\u043e\u0442\u043e\u0432 \u043a \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u044e     if (numPasses - index &gt; item.length) {       return ENGLISH_ALPHABET_LENGTH - 1     }      \/\/ \u0415\u0441\u043b\u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0443\u043f\u043f\u044b,     \/\/ \u0438\u043d\u0430\u0447\u0435, \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435     const charPos = index &gt; item.length - 1 ? 0 : item.length - index - 1      \/\/ https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/String\/charCodeAt     return item.toLowerCase().charCodeAt(charPos) - BASE_CHAR_CODE   }    \/\/ \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043b\u0438\u043d\u043e\u0439 \u0441\u0430\u043c\u043e\u0433\u043e \u0434\u043b\u0438\u043d\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430.   \/\/ \u0414\u043b\u044f \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b - \u044d\u0442\u043e `log10(num)`, \u0434\u043b\u044f \u0441\u0442\u0440\u043e\u043a - \u0434\u043b\u0438\u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0438   determineNumPasses(arr) {     if (this.isArrayOfNumbers(arr)) {       return Math.floor(Math.log10(Math.max(...arr))) + 1       \/\/ return Math.max(...arr.map((i) =&gt; i.toString().length))     }      return Math.max(...arr.map((i) =&gt; i.length))   }    isArrayOfNumbers(arr) {     return Number.isInteger(arr[0])   }    createBuckets(size) {     return new Array(size).fill().map(() =&gt; [])   } }<\/code><\/pre>\n<\/div><\/div>\n<p> <\/p>\n<p><strong>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/__tests__\/radix-sort.test.js import RadixSort from '..\/radix-sort' import { SortTester } from '..\/sort-tester'  \/\/ \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 const ARRAY_OF_STRINGS_VISIT_COUNT = 24 const ARRAY_OF_INTEGERS_VISIT_COUNT = 77  describe('RadixSort', () =&gt; {   it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u044b', () =&gt; {     SortTester.testSort(RadixSort)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u0442\u0440\u043e\u043a `n (\u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a) x m (\u0434\u043b\u0438\u043d\u0430 \u0441\u0430\u043c\u043e\u0439 \u0434\u043b\u0438\u043d\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438)` \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       RadixSort,       ['zzz', 'bb', 'a', 'rr', 'rrb', 'rrba'],       ARRAY_OF_STRINGS_VISIT_COUNT,     )   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b `n (\u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0447\u0438\u0441\u0435\u043b) x m (\u0434\u043b\u0438\u043d\u0430 \u0441\u0430\u043c\u043e\u0433\u043e \u0434\u043b\u0438\u043d\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430)` \u0440\u0430\u0437', () =&gt; {     SortTester.testAlgorithmTimeComplexity(       RadixSort,       [3, 1, 75, 32, 884, 523, 4343456, 232, 123, 656, 343],       ARRAY_OF_INTEGERS_VISIT_COUNT,     )   }) })<\/code><\/pre>\n<p> <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u044b:<\/p>\n<p> <\/p>\n<pre><code class=\"bash\">npm run test .\/algorithms\/sorting\/__tests__\/radix-sort<\/code><\/pre>\n<p> <\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/uv\/p5\/wl\/uvp5wledcyci0u7bmct9g6n0lnw.png\" data-src=\"https:\/\/habrastorage.org\/webt\/uv\/p5\/wl\/uvp5wledcyci0u7bmct9g6n0lnw.png\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<h2 id=\"-blochnaya-sortirovka\">\u276f \u0411\u043b\u043e\u0447\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430<\/h2>\n<p> <\/p>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<ul>\n<li><a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%91%D0%BB%D0%BE%D1%87%D0%BD%D0%B0%D1%8F_%D1%81%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0\">\u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u044f<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=LPrF9yEKTks\">YouTube<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=VuXbEb5ywrU\">\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/a><\/li>\n<\/ul>\n<p> <\/p>\n<p>\u0411\u043b\u043e\u0447\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (bucket sort) \u2014 \u044d\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u043c \u0447\u0438\u0441\u043b\u043e\u043c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0431\u043b\u043e\u043a\u043e\u0432 (\u043a\u0430\u0440\u043c\u0430\u043d\u043e\u0432, \u043a\u043e\u0440\u0437\u0438\u043d) \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443 \u0431\u043b\u043e\u043a\u0435 \u0431\u044b\u043b\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 (\u0438\u043b\u0438 \u043c\u0435\u043d\u044c\u0448\u0435), \u0447\u0435\u043c \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c. \u041a\u0430\u0436\u0434\u044b\u0439 \u0431\u043b\u043e\u043a \u0437\u0430\u0442\u0435\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e, \u043b\u0438\u0431\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0442\u0435\u043c \u0436\u0435 \u043c\u0435\u0442\u043e\u0434\u043e\u043c, \u043b\u0438\u0431\u043e \u0434\u0440\u0443\u0433\u0438\u043c. \u0417\u0430\u0442\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u043c\u0435\u0449\u0430\u044e\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u043c\u0430\u0441\u0441\u0438\u0432. \u042d\u0442\u043e\u0442 \u0442\u0438\u043f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043c\u043e\u0436\u0435\u0442 \u043e\u0431\u043b\u0430\u0434\u0430\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.<\/p>\n<p> <\/p>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0437\u043d\u0430\u043d\u0438\u0439 \u043e \u043f\u0440\u0438\u0440\u043e\u0434\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u044b\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0437\u0430 \u0440\u0430\u043c\u043a\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 &#171;\u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c&#187; \u0438 &#171;\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438&#187;, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u044b\u0445 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043a\u0443\u0447\u0435\u0439, \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0428\u0435\u043b\u043b\u0430 \u0438\u043b\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438.<\/p>\n<p> <\/p>\n<p>\u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430: \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043a \u043a\u043b\u0430\u0441\u0441\u0443 \u0431\u044b\u0441\u0442\u0440\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0441 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f <code>O(n)<\/code> (\u043d\u0430 \u0443\u0434\u0430\u0447\u043d\u044b\u0445 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445).<\/p>\n<p> <\/p>\n<p>\u041d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438: \u0441\u0438\u043b\u044c\u043d\u043e \u0434\u0435\u0433\u0440\u0430\u0434\u0438\u0440\u0443\u0435\u0442 \u043f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435 \u043c\u0430\u043b\u043e \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0438\u043b\u0438 \u0436\u0435 \u043d\u0430 \u043d\u0435\u0443\u0434\u0430\u0447\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u043e\u043c\u0435\u0440\u0430 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u043f\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430.<\/p>\n<p> <\/p>\n<p><em>\u041f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b<\/em><\/p>\n<p> <\/p>\n<p>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<p> <\/p>\n<ol>\n<li>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u0443\u0441\u0442\u044b\u0445 <code>buckets<\/code>.<\/li>\n<li>\u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0432 \u0435\u0433\u043e <code>bucket<\/code>.<\/li>\n<li>\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0435\u043f\u0443\u0441\u0442\u044b\u0445 \u0431\u043b\u043e\u043a\u043e\u0432.<\/li>\n<li>\u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u043e\u0432 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e.<\/li>\n<\/ol>\n<p> <\/p>\n<p>\u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u0431\u043b\u043e\u043a\u0430\u043c:<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/ic\/w7\/r3\/icw7r33zmfghol-wtrtfve8r5k0.png\" data-src=\"https:\/\/habrastorage.org\/webt\/ic\/w7\/r3\/icw7r33zmfghol-wtrtfve8r5k0.png\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<p>\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043d\u0443\u0442\u0440\u0438 \u0431\u043b\u043e\u043a\u043e\u0432:<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/gz\/-1\/hi\/gz-1himknd65lajuc6fhoobq5jy.png\" data-src=\"https:\/\/habrastorage.org\/webt\/gz\/-1\/hi\/gz-1himknd65lajuc6fhoobq5jy.png\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<p><em>\u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c<\/em><\/p>\n<p> <\/p>\n<p>\u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0433\u043e \u043a \u0431\u043b\u043e\u043a\u0430\u043c, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0445 \u0431\u043b\u043e\u043a\u043e\u0432 \u0438 \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435.<\/p>\n<p> <\/p>\n<p>\u0412 \u0445\u0443\u0434\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 <code>O(n^2)<\/code>, \u0435\u0441\u043b\u0438 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0431\u043b\u043e\u043a\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u0436\u0438\u0434\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0431\u043b\u043e\u043a \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043c\u0430\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u0412 \u0445\u0443\u0434\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e\u043c\u0435\u0449\u0430\u044e\u0442\u0441\u044f \u0432 \u043e\u0434\u0438\u043d \u0431\u043b\u043e\u043a.<\/p>\n<p> <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0445\u0443\u0434\u0448\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 <code>O(n * log(n))<\/code>, \u0442\u043e \u0445\u0443\u0434\u0448\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u0431\u043b\u043e\u0447\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0442\u0430\u043a\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c <code>O(n * log(n))<\/code>.<\/p>\n<p> <\/p>\n<p>\u0412 \u0441\u0440\u0435\u0434\u043d\u0435\u043c, \u043a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u044b, \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0434\u043e\u043a\u0430\u0437\u0430\u043d\u043e, \u0447\u0442\u043e \u0431\u043b\u043e\u0447\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432 \u0441\u0440\u0435\u0434\u043d\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0437\u0430 <code>O(n + k)<\/code>, \u0433\u0434\u0435 <code>k<\/code> \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0431\u043b\u043e\u043a\u043e\u0432.<\/p>\n<p> <\/p>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong><\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/bucket-sort.js import RadixSort from '.\/radix-sort'  const sorter = new RadixSort()  export default function bucketSort(arr, bucketSize = 1) {   \/\/ \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0431\u043b\u043e\u043a\u0438   const buckets = new Array(bucketSize).fill().map(() =&gt; [])    \/\/ \u041d\u0430\u0445\u043e\u0434\u0438\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435   const minValue = Math.min(...arr)   \/\/ \u041d\u0430\u0441\u0442\u0440\u043e\u0439 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435   const maxValue = Math.max(...arr)    \/\/ \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440 \u0431\u043b\u043e\u043a\u0430   const _bucketSize = Math.ceil(Math.max(1, (maxValue - minValue) \/ bucketSize))    \/\/ \u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u0433\u0440\u0443\u043f\u043f\u0430\u043c   for (const item of arr) {     const index = Math.floor((item - minValue) \/ _bucketSize)      \/\/ \u041a\u0440\u0430\u0439\u043d\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u0434\u043b\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f     if (index === bucketSize) {       buckets[bucketSize - 1].push(item)     } else {       buckets[index].push(item)     }   }    \/\/ \u0421\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0431\u043b\u043e\u043a\u0438   for (let i = 0; i &lt; buckets.length; i += 1) {     \/\/ \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0434\u0430\u0442\u044c \u0441\u0440\u0435\u0434\u043d\u044e\u044e     \/\/ \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c `O(n + k)` \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0430     \/\/ (\u0433\u0434\u0435 `k` - \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0446\u0438\u0444\u0440 \u0441\u0430\u043c\u043e\u0433\u043e \u0434\u043b\u0438\u043d\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430)     buckets[i] = sorter.sort(buckets[i])   }    \/\/ \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u043c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0431\u043b\u043e\u043a\u0438 \u0432 \u043e\u0434\u0438\u043d \u043c\u0430\u0441\u0441\u0438\u0432   const sortedArr = buckets.flat()    return sortedArr }<\/code><\/pre>\n<p> <\/p>\n<p><strong>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p> <\/p>\n<pre><code class=\"javascript\">\/\/ algorithms\/sorting\/__tests__\/bucket-sort.test.js import BucketSort from '..\/bucket-sort' import { equalArr, notSortedArr, reverseArr, sortedArr } from '..\/sort-tester'  describe('BucketSort', () =&gt; {   it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0447\u0438\u0441\u0435\u043b \u0441 \u0440\u0430\u0437\u043d\u044b\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0431\u043b\u043e\u043a\u043e\u0432', () =&gt; {     expect(BucketSort(notSortedArr, 4)).toEqual(sortedArr)     expect(BucketSort(equalArr, 4)).toEqual(equalArr)     expect(BucketSort(reverseArr, 4)).toEqual(sortedArr)     expect(BucketSort(sortedArr, 4)).toEqual(sortedArr)      expect(BucketSort(notSortedArr, 10)).toEqual(sortedArr)     expect(BucketSort(equalArr, 10)).toEqual(equalArr)     expect(BucketSort(reverseArr, 10)).toEqual(sortedArr)     expect(BucketSort(sortedArr, 10)).toEqual(sortedArr)      expect(BucketSort(notSortedArr, 50)).toEqual(sortedArr)     expect(BucketSort(equalArr, 50)).toEqual(equalArr)     expect(BucketSort(reverseArr, 50)).toEqual(sortedArr)     expect(BucketSort(sortedArr, 50)).toEqual(sortedArr)   })    it('\u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0447\u0438\u0441\u0435\u043b \u0441 \u043e\u0434\u043d\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u043e\u0439', () =&gt; {     expect(BucketSort(notSortedArr)).toEqual(sortedArr)     expect(BucketSort(equalArr)).toEqual(equalArr)     expect(BucketSort(reverseArr)).toEqual(sortedArr)     expect(BucketSort(sortedArr)).toEqual(sortedArr)   }) })<\/code><\/pre>\n<p> <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u044b:<\/p>\n<p> <\/p>\n<pre><code class=\"bash\">npm run test .\/algorithms\/sorting\/__tests__\/bucket-sort<\/code><\/pre>\n<p> <\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/82\/wy\/b8\/82wyb8ki_-dajodgnzgsigef3fi.png\" data-src=\"https:\/\/habrastorage.org\/webt\/82\/wy\/b8\/82wyb8ki_-dajodgnzgsigef3fi.png\"\/> <\/p>\n<p> <\/p>\n<p> <\/p>\n<p>\u041d\u0430 \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u044d\u0442\u043e \u0432\u0441\u0435, \u0434\u0440\u0443\u0437\u044c\u044f. \u0423\u0432\u0438\u0434\u0438\u043c\u0441\u044f \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438.<\/p>\n<p> <\/p>\n<hr\/>\n<p> <\/p>\n<blockquote><p><a href=\"https:\/\/t.me\/timewebru\"><b>\u041d\u043e\u0432\u043e\u0441\u0442\u0438, \u043e\u0431\u0437\u043e\u0440\u044b \u043f\u0440\u043e\u0434\u0443\u043a\u0442\u043e\u0432 \u0438 \u043a\u043e\u043d\u043a\u0443\u0440\u0441\u044b \u043e\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u044b Timeweb.Cloud \u2014 \u0432 \u043d\u0430\u0448\u0435\u043c Telegram-\u043a\u0430\u043d\u0430\u043b\u0435<\/b><\/a> <b>\u21a9<\/b><\/p><\/blockquote>\n<p><a href=\"https:\/\/timeweb.cloud\/?i=106909\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/u9\/vg\/io\/u9vgio3hxj12h5u7j3un0wx_zpk.png\" data-src=\"https:\/\/habrastorage.org\/webt\/u9\/vg\/io\/u9vgio3hxj12h5u7j3un0wx_zpk.png\"\/><\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/872326\/\"> https:\/\/habr.com\/ru\/articles\/872326\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/webt\/ma\/po\/lv\/mapolvqq4uunxfqoaviv3g9km9y.jpeg\" data-src=\"https:\/\/habrastorage.org\/webt\/ma\/po\/lv\/mapolvqq4uunxfqoaviv3g9km9y.jpeg\" data-blurred=\"true\"\/> <\/p>\n<p> \u041f\u0440\u0438\u0432\u0435\u0442, \u0434\u0440\u0443\u0437\u044c\u044f!<\/p>\n<p> <\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0442\u0435\u0439 \u043c\u044b \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0432 <a href=\"https:\/\/github.com\/trekhleb\/javascript-algorithms\">\u044d\u0442\u043e\u043c \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u043c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438<\/a>. \u042d\u0442\u043e \u0432\u043e\u0441\u044c\u043c\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0441\u0435\u0440\u0438\u0438.<\/p>\n<p> <\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e\u0431 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u0445 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438.<\/p>\n<p> <\/p>\n<p>\u041a\u043e\u0434, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0432 \u044d\u0442\u043e\u0439 \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445 \u0441\u0435\u0440\u0438\u0438, \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 <a href=\"https:\/\/github.com\/harryheman\/algorithms-data-structures\">\u044d\u0442\u043e\u043c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438<\/a>.<\/p>\n<p> <\/p>\n<p>\u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e? \u0422\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0448\u0443 \u043f\u043e\u0434 \u043a\u0430\u0442.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-453971","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/453971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=453971"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/453971\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=453971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=453971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=453971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}