|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <title>Sort an Array Without Using sort()</title> |
| 7 | + <style> |
| 8 | + body { |
| 9 | + line-height: 1.3; |
| 10 | + font-size: 16px; |
| 11 | + margin: 20px; |
| 12 | + background-color: #eaedf0; |
| 13 | + } |
| 14 | + pre { |
| 15 | + background-color: #dbdfe3; |
| 16 | + padding: 15px; |
| 17 | + border-radius: 5px; |
| 18 | + overflow-x: auto; |
| 19 | + } |
| 20 | + code { |
| 21 | + color: #000000; |
| 22 | + } |
| 23 | + h1, h2 { |
| 24 | + color: #343a40; |
| 25 | + } |
| 26 | + h3 { |
| 27 | + color: #495057; |
| 28 | + } |
| 29 | + .example { |
| 30 | + color: #f52222; |
| 31 | + } |
| 32 | + </style> |
| 33 | +</head> |
| 34 | +<body> |
| 35 | + <h1>Sort an Array Without Using <code>sort()</code></h1> |
| 36 | + <p> |
| 37 | + Sorting an array without using JavaScript's built-in <code>sort()</code> method can be done using simple techniques like the bubble sort algorithm. |
| 38 | + </p> |
| 39 | + |
| 40 | + <h2>Using Bubble Sort</h2> |
| 41 | + <p> |
| 42 | + Below is an example of sorting an array in ascending order using the bubble sort algorithm: |
| 43 | + </p> |
| 44 | + <pre><code> |
| 45 | +function bubbleSort(arr) { |
| 46 | + let n = arr.length; |
| 47 | + for (let i = 0; i < n - 1; i++) |
| 48 | + { |
| 49 | + for (let j = 0; j < n - i - 1; j++) |
| 50 | + { |
| 51 | + if (arr[j] > arr[j + 1]) |
| 52 | + { |
| 53 | + let temp = arr[j]; |
| 54 | + arr[j] = arr[j + 1]; |
| 55 | + arr[j + 1] = temp; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + return arr; |
| 60 | +} |
| 61 | +const numbers = [5, 3, 8, 1, 2]; |
| 62 | +const sortedArray = bubbleSort(numbers); |
| 63 | +console.log(sortedArray); |
| 64 | + </code></pre> |
| 65 | + <p class="example">Output: [1, 2, 3, 5, 8]</p> |
| 66 | + |
| 67 | + <h2>How It Works</h2> |
| 68 | + <p> |
| 69 | + - Bubble sort works by repeatedly comparing adjacent elements and swapping them if they are in the wrong order.<br> |
| 70 | + - This process is repeated until the entire array is sorted.<br> |
| 71 | + - For the array <code>[5, 3, 8, 1, 2]</code>: |
| 72 | + <ul> |
| 73 | + <li>In the first pass, the largest number is moved to the last position.</li> |
| 74 | + <li>This continues until all elements are in ascending order.</li> |
| 75 | + </ul> |
| 76 | + </p> |
| 77 | + |
| 78 | + <h2>Why Bubble Sort?</h2> |
| 79 | + <p> |
| 80 | + While bubble sort is not the most efficient sorting algorithm, it is easy to understand and implement, making it a great choice for learning purposes. |
| 81 | + </p> |
| 82 | + |
| 83 | + <h2>Conclusion</h2> |
| 84 | + <p> |
| 85 | + Sorting an array without <code>sort()</code> can be achieved using algorithms like bubble sort. This method is simple but may not be suitable for very large datasets. |
| 86 | + </p> |
| 87 | +</body> |
| 88 | +</html> |
0 commit comments