gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
напиши js код, котрый визуализирует в браузере алгоритм бин поика
- User
напиши js код, котрый визуализирует в браузере алгоритм бин поика
- Assistant
Конечно! Вот пример JavaScript кода, который визуализирует алгоритм бинарного поиска в браузере:
<!DOCTYPE html> <html> <head> <title>Binary Search Visualization</title> <style> #container { display: flex; flex-direction: column; align-items: center; margin-top: 50px; } #target { margin-bottom: 20px; } #result { font-weight: bold; } </style> </head> <body> <div id="container"> <input type="number" id="target" placeholder="Enter a number" /> <button onclick="binarySearch()">Search</button> <p id="result"></p> </div> <script> const array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]; // sorted array for binary search function binarySearch() { const target = parseInt(document.getElementById("target").value); let start = 0; let end = array.length - 1; let middle, found = false; while (start <= end) { middle = Math.floor((start + end) / 2); if (target === array[middle]) { found = true; break; } else if (target < array[middle]) { end = middle - 1; } else { start = middle + 1; } } const result = document.getElementById("result"); result.innerText = found ? `${target} found at index ${middle}` : `${target} not found in the array`; } </script> </body> </html>
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.