-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontent.jsx
More file actions
306 lines (286 loc) · 11.1 KB
/
Copy pathcontent.jsx
File metadata and controls
306 lines (286 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"use client";
import ComplexityGraph from "@/app/components/ui/graph";
import { useEffect, useState } from "react";
const content = () => {
const [theme, setTheme] = useState('light');
const [mounted, setMounted] = useState(false);
useEffect(() => {
const updateTheme = () => {
const savedTheme = localStorage.getItem('theme') || 'light';
setTheme(savedTheme);
};
updateTheme();
setMounted(true);
window.addEventListener('storage', updateTheme);
window.addEventListener('themeChange', updateTheme);
return () => {
window.removeEventListener('storage', updateTheme);
window.removeEventListener('themeChange', updateTheme);
};
}, []);
const paragraph = [
`Insertion Sort is a simple sorting algorithm that builds the final sorted array one item at a time. It works similarly to how you might sort playing cards in your hands - you take each new card and insert it into its proper position among the already sorted cards.`,
`The algorithm maintains a "sorted sublist" that grows with each iteration.`,
`Insertion Sort is often used when the data is nearly sorted (where it approaches O(n) time) or when the dataset is small. Some hybrid algorithms like TimSort use Insertion Sort for small subarrays due to its low overhead.`,
];
const working = [
{
points: "First Element (7):",
subpoints: ['Already "sorted" as the first item', "→ [7, 3, 5, 2, 1]"],
},
{
points: "Second Element (3):",
subpoints: ["Insert before 7", "→ [3, 7, 5, 2, 1]"],
},
{
points: "Third Element (5):",
subpoints: ["Insert between 3 and 7", "→ [3, 5, 7, 2, 1]"],
},
{
points: "Fourth Element (2):",
subpoints: ["Insert at beginning", "→ [2, 3, 5, 7, 1]"],
},
{
points: "Fifth Element (1):",
subpoints: ["Insert at beginning", "→ [1, 2, 3, 5, 7]"],
},
];
const algorithm = [
{
steps: "Start with the second element (consider first element as sorted)",
},
{ steps: "Pick the next element (key) from the unsorted portion" },
{
steps: "Compare the key with elements in the sorted portion:",
points: [
"Shift elements greater than the key one position right",
"Stop when you find an element ≤ the key",
],
},
{ steps: "Insert the key in its correct position" },
{ steps: "Repeat until all elements are processed" },
];
const timeComplexity = [
{
points:
"Best Case: Already sorted array → O(n) (only comparisons, no shifts).",
},
{ points: "Average Case: Randomly ordered array → O(n²)." },
{
points:
"Worst Case: Reverse sorted array → O(n²) (maximum comparisons and shifts).",
},
];
const advantages = [
{
points:
"Efficient for small datasets (often faster than more complex algorithms for n ≤ 10)",
},
{ points: "Stable (doesn't change relative order of equal elements)" },
{ points: "Adaptive (performs well with partially sorted data)" },
{ points: "Online (can sort as it receives input)" },
];
return (
<main className="max-w-7xl mx-auto grid grid-cols-1 md:grid-cols-5 md:gap-4">
<div className="col-span-1">
<div className="hidden md:block">
{mounted && (
<iframe
key={theme}
src={
theme === "dark"
? "https://hw.glich.co/resources/embed/daily/dsa?theme=dark"
: "https://hw.glich.co/resources/embed/daily/dsa?theme=light"
}
width="100%"
height="400"
title="Daily DSA Challenge"
></iframe>
)}
</div>
<div className="flex justify-center">
<span className="text-xs hidden md:block">
Powered by{" "}
<a
href="https://hw.glich.co/resources/daily"
target="_blank"
className="underline hover:text-blue-500 duration-300"
>
Hello World
</a>
</span>
</div>
</div>
<article className="col-span-4 max-w-4xl bg-white dark:bg-neutral-950 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700 overflow-hidden mb-8">
{/* What is Insertion Sort */}
<section className="p-6 border-b border-gray-100 dark:border-gray-700">
<h1 className="text-2xl font-bold text-gray-900 dark:text-white mb-4 flex items-center">
<span className="w-1 h-6 bg-blue-500 mr-3 rounded-full"></span>
What is Insertion Sort?
</h1>
<div className="prose dark:prose-invert max-w-none">
<p className="text-gray-700 dark:text-gray-300 leading-relaxed">
{paragraph[0]}
</p>
</div>
</section>
{/* How Does It Work */}
<section className="p-6 border-b border-gray-100 dark:border-gray-700">
<h1 className="text-2xl font-bold text-gray-900 dark:text-white mb-4 flex items-center">
<span className="w-1 h-6 bg-blue-500 mr-3 rounded-full"></span>
How Does It Work?
</h1>
<div className="prose dark:prose-invert max-w-none">
<p className="text-gray-700 dark:text-gray-300 mb-4 leading-relaxed">
Consider this unsorted array: [7, 3, 5, 2, 1]
</p>
<ol className="space-y-3 list-decimal pl-5 marker:text-gray-500 dark:marker:text-gray-400">
{working.map((items, index) => (
<li
key={index}
className="text-gray-700 dark:text-gray-300 pl-2"
>
<span className="font-semibold">{items.points}</span>
{items.subpoints && (
<ul className="mt-2 space-y-2 list-disc pl-5 marker:text-gray-400 dark:marker:text-gray-500 font-normal">
{items.subpoints.map((subitems, subindex) => (
<li
key={subindex}
className="text-gray-600 dark:text-gray-400"
>
{subitems}
</li>
))}
</ul>
)}
</li>
))}
</ol>
<p className="text-gray-700 dark:text-gray-300 mt-4 leading-relaxed">
{paragraph[1]}
</p>
</div>
</section>
{/* Algorithm Steps */}
<section className="p-6 border-b border-gray-100 dark:border-gray-700">
<h1 className="text-2xl font-bold text-gray-900 dark:text-white mb-4 flex items-center">
<span className="w-1 h-6 bg-blue-500 mr-3 rounded-full"></span>
Algorithm Steps
</h1>
<div className="prose dark:prose-invert max-w-none">
<ol className="space-y-3 list-decimal pl-5 marker:text-gray-500 dark:marker:text-gray-400">
{algorithm.map((item, index) => (
<li
key={index}
className="text-gray-700 dark:text-gray-300 pl-2"
>
{item.steps}
{item.points && (
<ul className="mt-2 space-y-2 list-disc pl-5 marker:text-gray-400 dark:marker:text-gray-500">
{item.points.map((subitem, subindex) => (
<li
key={subindex}
className="text-gray-600 dark:text-gray-400"
>
{subitem}
</li>
))}
</ul>
)}
</li>
))}
</ol>
</div>
</section>
{/* Time Complexity */}
<section className="p-6 border-b border-gray-100 dark:border-gray-700">
<h1 className="text-2xl font-bold text-gray-900 dark:text-white mb-4 flex items-center">
<span className="w-1 h-6 bg-blue-500 mr-3 rounded-full"></span>
Time Complexity
</h1>
<div className="prose dark:prose-invert max-w-none">
<ul className="space-y-3 list-disc pl-5 marker:text-gray-500 dark:marker:text-gray-400">
{timeComplexity.map((item, index) => (
<li
key={index}
className="text-gray-700 dark:text-gray-300 pl-2"
>
<span className="font-mono bg-gray-100 dark:bg-gray-700 px-2 py-1 rounded text-sm">
{item.points.split(":")[0]}:
</span>
<span className="ml-2">{item.points.split(":")[1]}</span>
</li>
))}
</ul>
</div>
<div className="mt-8">
<ComplexityGraph
bestCase={(n) => n}
averageCase={(n) => n * n}
worstCase={(n) => n * n}
maxN={25}
/>
</div>
</section>
{/* Advantages */}
<section className="p-6 border-b border-gray-100 dark:border-gray-700">
<h1 className="text-2xl font-bold text-gray-900 dark:text-white mb-4 flex items-center">
<span className="w-1 h-6 bg-blue-500 mr-3 rounded-full"></span>
Advantages
</h1>
<div className="prose dark:prose-invert max-w-none">
<ul className="space-y-3 list-disc pl-5 marker:text-gray-500 dark:marker:text-gray-400">
{advantages.map((item, index) => (
<li
key={index}
className="text-gray-700 dark:text-gray-300 pl-2"
>
{item.points}
</li>
))}
</ul>
</div>
</section>
{/* Additional Info */}
<section className="p-6">
<div className="prose dark:prose-invert max-w-none">
<div className="px-4 bg-blue-50 dark:bg-blue-900/20 rounded-lg border border-blue-200 dark:border-blue-800">
<p className="text-gray-700 dark:text-gray-300 leading-relaxed">
{paragraph[2]}
</p>
</div>
</div>
</section>
</article>
{/* Mobile iframe at bottom */}
<div className="block md:hidden w-full">
{mounted && (
<iframe
key={theme}
src={
theme === "dark"
? "https://hw.glich.co/resources/embed/daily/dsa?theme=dark"
: "https://hw.glich.co/resources/embed/daily/dsa?theme=light"
}
width="100%"
height="320"
title="Daily DSA Challenge"
></iframe>
)}
<div className="flex justify-center pb-8">
<span className="text-xs">
Powered by{" "}
<a
href="https://hw.glich.co/resources/daily"
target="_blank"
className="underline hover:text-blue-500 duration-300"
>
Hello World
</a>
</span>
</div>
</div>
</main>
);
};
export default content;