@@ -64,6 +64,25 @@ function binarySearch(arr, target) {
6464 else hi = mid - 1;
6565 }
6666 return -1;
67+ }
68+
69+
70+ // O(n log n) — Linearithmic time
71+ function mergeSort(arr) {
72+ if (arr.length <= 1) return arr;
73+ const mid = Math.floor(arr.length / 2);
74+ const left = mergeSort(arr.slice(0, mid));
75+ const right = mergeSort(arr.slice(mid));
76+ return merge(left, right);
77+ }
78+
79+ function merge(left, right) {
80+ const result = [];
81+ while (left.length && right.length) {
82+ if (left[0] < right[0]) result.push(left.shift());
83+ else result.push(right.shift());
84+ }
85+ return [...result, ...left, ...right];
6786}` ,
6887 description : `Big O Notation
6988
@@ -95,6 +114,8 @@ The chart shows how each complexity's curve grows as the input size increases.`,
95114 // 7: function findMax(arr) { 25: // O(log n) — Logarithmic time
96115 // 9: for (let i = 1; ...) { 26: function binarySearch(arr, target) {
97116 // 28: while (lo <= hi) {
117+ // 36: // O(n log n) — Linearithmic time
118+ // 37: function mergeSort(arr) {
98119
99120 // Step 1: Introduction — no curves yet
100121 steps . push ( {
@@ -175,7 +196,7 @@ The chart shows how each complexity's curve grows as the input size increases.`,
175196 'O(n log n) — Linearithmic. At small n it looks close to O(n). Let\'s see it diverge...' ,
176197 'O(n log n) — Linearítmico. Con n pequeño se parece a O(n). Veamos cómo diverge...' ,
177198 ) ,
178- codeLine : 9 ,
199+ codeLine : 41 ,
179200 variables : { complexity : 'O(n log n)' , 'ops(2)' : 2 , 'ops(4)' : 8 } ,
180201 } )
181202
@@ -186,7 +207,7 @@ The chart shows how each complexity's curve grows as the input size increases.`,
186207 'O(n log n) at n=10: ~33 operations. Typical of Merge Sort and Quick Sort. It bends away from O(n) as n grows.' ,
187208 'O(n log n) con n=10: ~33 operaciones. Típico de Merge Sort y Quick Sort. Se curva alejándose de O(n) conforme n crece.' ,
188209 ) ,
189- codeLine : 9 ,
210+ codeLine : 43 ,
190211 variables : { complexity : 'O(n log n)' , 'ops(5)' : '11.6' , 'ops(10)' : '33.2' } ,
191212 } )
192213
0 commit comments