Инструменты пользователя

Инструменты сайта


basics_of_algorithms:sorting_algorithms

Различия

Здесь показаны различия между двумя версиями данной страницы.

Ссылка на это сравнение

Предыдущая версия справа и слева Предыдущая версия
basics_of_algorithms:sorting_algorithms [2023/10/04 22:12]
werwolf [Как реализовать быструю сортировку]
basics_of_algorithms:sorting_algorithms [2023/10/04 22:18] (текущий)
werwolf [Универсальная функция сортировки]
Строка 1165: Строка 1165:
 Сам массив выглядит так: Сам массив выглядит так:
  
-<​code>​+<​code ​javascript>
 const products = [ const products = [
   { name: "​Телевизор",​ price: 100000, rating: 9.1 },   { name: "​Телевизор",​ price: 100000, rating: 9.1 },
Строка 1179: Строка 1179:
 </​code>​ </​code>​
  
-Python+<​details>​ 
 +<​summary>​Python</​summary>​
  
-<​code>​+<​code ​python>
 products = [ products = [
   {"​name":​ "​Телевизор",​ "​price":​ 100000, "​rating":​ 9.1},   {"​name":​ "​Телевизор",​ "​price":​ 100000, "​rating":​ 9.1},
Строка 1194: Строка 1195:
 ] ]
 </​code>​ </​code>​
 +</​details>​
  
-PHP+<​details>​ 
 +<​summary>​PHP</​summary>​
  
-<​code>​+<​code ​php>
 <?php <?php
  
Строка 1212: Строка 1215:
 ]; ];
 </​code>​ </​code>​
 +</​details>​
  
-Java+<​details>​ 
 +<​summary>​Java</​summary>​
  
-<​code>​+<​code ​java>
 Map[] products = { Map[] products = {
   Map.of ("​name",​ "​Телевизор",​ "​price",​ 100000, "​rating",​ 9.1),   Map.of ("​name",​ "​Телевизор",​ "​price",​ 100000, "​rating",​ 9.1),
Строка 1228: Строка 1233:
 }; };
 </​code>​ </​code>​
 +</​details>​
  
 Можно реализовать несколько функций сортировки,​ но есть и более эффективный способ. Интернет-магазину подойдет **универсальная функция сортировки**. Можно реализовать несколько функций сортировки,​ но есть и более эффективный способ. Интернет-магазину подойдет **универсальная функция сортировки**.
Строка 1237: Строка 1243:
 Вот так будет выглядеть компаратор,​ сравнивающий элементы по цене: Вот так будет выглядеть компаратор,​ сравнивающий элементы по цене:
  
-<​code>​+<​code ​javascript
 const compareByPrice = (item1, item2) => { const compareByPrice = (item1, item2) => {
   if (item1.price < item2.price) {   if (item1.price < item2.price) {
Строка 1249: Строка 1255:
 </​code>​ </​code>​
  
-Python+<​details>​ 
 +<​summary>​Python</​summary> ​
  
-<​code>​+<​code ​python>
 def compare_by_price(item1,​ item2): def compare_by_price(item1,​ item2):
     if item1[price] < item2[price]:​     if item1[price] < item2[price]:​
Строка 1260: Строка 1267:
         return 1         return 1
 </​code>​ </​code>​
 +</​details>​
  
-PHP+<​details>​ 
 +<​summary>​PHP</​summary>​
  
-<​code>​+<​code ​php>
 <?php <?php
  
Строка 1277: Строка 1286:
 } }
 </​code>​ </​code>​
 +</​details>​
  
-Java+<​details>​ 
 +<​summary>​Java</​summary>​
  
-<​code>​+<​code ​java>
 public static ToIntBiFunction<​Map<​String,​ Object>, Map<​String,​ Object>>​ compareByPrice = (item1, item2) -> { public static ToIntBiFunction<​Map<​String,​ Object>, Map<​String,​ Object>>​ compareByPrice = (item1, item2) -> {
         var price1 = (int) item1.get("​price"​);​         var price1 = (int) item1.get("​price"​);​
Строка 1294: Строка 1305:
 }; };
 </​code>​ </​code>​
 +</​details>​
  
 А вот так — компаратор,​ сравнивающий элементы по рейтингу:​ А вот так — компаратор,​ сравнивающий элементы по рейтингу:​
  
-<​code>​+<​code ​javascript>
 const compareByRating = (item1, item2) => { const compareByRating = (item1, item2) => {
   if (item1.rating < item2.rating) {   if (item1.rating < item2.rating) {
Строка 1309: Строка 1321:
 </​code>​ </​code>​
  
-Python+<​details>​ 
 +<​summary>​Python</​summary>​
  
-<​code>​+<​code ​python>
 def compare_by_rating(item1,​ item2): def compare_by_rating(item1,​ item2):
     if item1[rating] < item2[rating]:​     if item1[rating] < item2[rating]:​
Строка 1320: Строка 1333:
         return 1         return 1
 </​code>​ </​code>​
 +</​details>​
  
-PHP+<​details>​ 
 +<​summary>​PHP</​summary>​
  
-<​code>​+<​code ​php>
 <?php <?php
  
Строка 1337: Строка 1352:
 } }
 </​code>​ </​code>​
 +</​details>​
  
-Java+<​details>​ 
 +<​summary>​Java</​summary>​
  
-<​code>​+<​code ​java>
 public static ToIntBiFunction<​Map<​String,​ Object>, Map<​String,​ Object>>​ compareByRating = (item1, item2) -> { public static ToIntBiFunction<​Map<​String,​ Object>, Map<​String,​ Object>>​ compareByRating = (item1, item2) -> {
         var rating1 = (double) item1.get("​rating"​);​         var rating1 = (double) item1.get("​rating"​);​
Строка 1354: Строка 1371:
 }; };
 </​code>​ </​code>​
 +</​details>​
  
 Универсальная функция сравнивает два элемента,​ но не использует операторы «больше» или «меньше». Вместо этого она вызывает компаратор и проверяет результат. Так выглядит универсальная пузырьковая сортировка:​ Универсальная функция сравнивает два элемента,​ но не использует операторы «больше» или «меньше». Вместо этого она вызывает компаратор и проверяет результат. Так выглядит универсальная пузырьковая сортировка:​
  
-<​code>​+<​code ​javascript>
 const bubbleSort = (items, comparator) => { const bubbleSort = (items, comparator) => {
   for (let limit = items.length - 1; limit > 0; limit -= 1) {   for (let limit = items.length - 1; limit > 0; limit -= 1) {
Строка 1370: Строка 1388:
 }; };
 </​code>​ </​code>​
 +</​details>​
  
-Python+<​details>​ 
 +<​summary>​Python</​summary>​
  
-<​code>​+<​code ​python>
 def bubble_sort(items,​ comparator):​ def bubble_sort(items,​ comparator):​
     for limit in range(len(items) - 1, 0, -1):     for limit in range(len(items) - 1, 0, -1):
Строка 1380: Строка 1400:
                 items[i], items[i + 1] = items[i + 1], items[i]                 items[i], items[i + 1] = items[i + 1], items[i]
 </​code>​ </​code>​
 +</​details>​
  
-PHP+<​details>​ 
 +<​summary>​PHP</​summary>​
  
-<​code>​+<​code ​php>
 <?php <?php
  
Строка 1399: Строка 1421:
 } }
 </​code>​ </​code>​
 +</​details>​
  
-Java+<​details>​ 
 +<​summary>​Java</​summary>​
  
-<​code>​+<​code ​java>
 class App { class App {
     public static void bubbleSort(Map[] items, ToIntBiFunction comparator) {     public static void bubbleSort(Map[] items, ToIntBiFunction comparator) {
basics_of_algorithms/sorting_algorithms.1696446771.txt.gz · Последние изменения: 2023/10/04 22:12 — werwolf