1import{j as e,B as p,S as E}from"./index-DxzsxK2X.js";import{r as h,L as y}from"./vendor-DlcqtHFq.js";import{M as D}from"./MarketingLayout-BqcyWV3e.js";import{C as F}from"./circle-check-ChkjSNco.js";import{C as V}from"./circle-x-BdP0D6z-.js";import{R as k}from"./rotate-ccw-DjIsYZ50.js";import{S as H,P as z,a as R}from"./skip-forward-BH9xgb1F.js";import{P as W}from"./play-CCLcSZsW.js";import{u as U,A as K,M as G}from"./useScrollSpy-5czGbz9L.js";import{a as Y,b as Z,f as J,q as X}from"./schemas-LlmsxjhX.js";import{C as A}from"./chevron-right-CeoH38AI.js";import{L as m}from"./link-ClbJ78HG.js";import{B as M}from"./book-open-CsF8txL6.js";import{T as u}from"./triangle-alert-CwGrVLZL.js";import{C as P}from"./code-BPHEj6WV.js";import{L as N}from"./lightbulb-BY8cMslo.js";const x=[{id:1,question:"What is the theoretical lower bound for comparison-based sorting algorithms?",options:["O(n)","O(n log n)","O(n²)","O(log n)"],correctIndex:1,explanation:"Any comparison-based sorting algorithm requires at least O(n log n) comparisons in the worst case. This is a fundamental result proven using decision trees."},{id:2,question:"Which of the following sorting algorithms is stable?",options:["Quick Sort","Merge Sort","Selection Sort","Heap Sort"],correctIndex:1,explanation:"Merge Sort is stable â it preserves the relative order of equal elements during the merge step. Quick Sort, Selection Sort, and Heap Sort are all unstable."},{id:3,question:"What is the worst-case time complexity of Quick Sort?",options:["O(n log n)","O(n)","O(n²)","O(1)"],correctIndex:2,explanation:"Quick Sort degrades to O(n²) when the pivot selection consistently produces highly unbalanced partitions, such as always picking the smallest or largest element."},{id:4,question:"Which sorting algorithm is NOT considered in-place?",options:["Bubble Sort","Selection Sort","Merge Sort","Heap Sort"],correctIndex:2,explanation:"Merge Sort requires O(n) auxiliary space for the temporary array used during merging, making it an out-of-place algorithm. The others sort within the original array."},{id:5,question:"Which algorithmic paradigm does Merge Sort use?",options:["Divide and Conquer","Greedy approach","Dynamic programming","Brute force"],correctIndex:0,explanation:"Merge Sort is a classic divide-and-conquer algorithm: it divides the array in half, recursively sorts each half, then merges the sorted halves back together."},{id:6,question:'What does it mean for a sorting algorithm to be "stable"?',options:["It preserves the relative order of equal elements","It always runs in O(n log n) time","It uses O(1) auxiliary space","It never crashes on edge cases"],correctIndex:0,explanation:"A stable sort preserves the original relative ordering of elements with equal keys. This matters when sorting by multiple criteria sequentially."},{id:7,question:"Which sorting algorithm performs well on nearly sorted arrays with O(n) best-case time?",options:["Selection Sort","Merge Sort","Insertion Sort","Heap Sort"],correctIndex:2,explanation:"Insertion Sort achieves O(n) on already-sorted or nearly-sorted arrays because each element requires minimal or no shifting. Selection Sort is always O(n²)."},{id:8,question:"What is the space complexity of Heap Sort?",options:["O(n)","O(n log n)","O(log n)","O(1)"],correctIndex:3,explanation:"Heap Sort is an in-place algorithm that sorts elements within the original array. It requires only O(1) auxiliary space beyond the input."},{id:9,question:"In Quick Sort, what is the purpose of the pivot element?",options:["It is always the smallest element in the array","It is the element that is swapped in every comparison","It partitions the array so smaller elements are left and larger elements are right","It marks the end of the sorted portion of the array"],correctIndex:2,explanation:"The pivot is used to partition the array into two subarrays: elements less than the pivot go to its left, and elements greater go to its right. The pivot ends up in its final sorted position."},{id:10,question:"Which sorting algorithm guarantees O(n log n) time in ALL cases while using only O(1) extra space?",options:["Quick Sort","Merge Sort","Hea
1p Sort","Insertion Sort"],correctIndex:2,explanation:"Heap Sort guarantees O(n log n) in best, average, and worst cases while being in-place with O(1) extra space. Merge Sort is O(n log n) but needs O(n) space. Quick Sort is in-place but has O(n²) worst case."}];function ee({headless:r=!1}={}){const[i,l]=h.useState({}),[t,s]=h.useState(!1),c=t?x.filter(d=>i[d.id]===d.correctIndex).length:0,a=(d,n)=>{t||l(o=>({...o,[d]:n}))},g=()=>{Object.keys(i).length<x.length||s(!0)},w=()=>{l({}),s(!1)},v=Object.keys(i).length===x.length;return e.jsxs("div",{children:[!r&&e.jsx("h2",{className:"text-2xl font-bold mb-2",children:"Practice Quiz"}),e.jsx("p",{className:"text-muted-foreground mb-6",children:"Test your understanding of sorting algorithms â select the correct answer for each question."}),t&&e.jsxs("div",{className:`mb-8 rounded-xl p-5 text-center ${c===x.length?"bg-green-50 border border-green-200":c>=x.length*.7?"bg-blue-50 border border-blue-200":"bg-amber-50 border border-amber-200"}`,children:[e.jsxs("p",{className:"text-2xl font-bold mb-1",children:[c," / ",x.length]}),e.jsx("p",{className:"text-sm text-muted-foreground",children:c===x.length?"Perfect score! You nailed it.":c>=x.length*.7?"Great job! Review the ones you missed.":"Keep studying â review the explanations below."})]}),e.jsx("div",{className:"space-y-8",children:x.map((d,n)=>{const o=i[d.id],S=t&&o===d.correctIndex,L=t&&o!==void 0&&o!==d.correctIndex;return e.jsxs("div",{className:`rounded-xl border p-5 transition-colors ${S?"border-green-300 bg-green-50/50":L?"border-red-300 bg-red-50/50":"border-border"}`,children:[e.jsxs("p",{className:"font-semibold mb-3",children:[e.jsxs("span",{className:"text-muted-foreground mr-2",children:[n+1,"."]}),d.question]}),e.jsx("div",{className:"space-y-2",children:d.options.map((_,j)=>{const C=o===j,q=t&&j===d.correctIndex,T=t&&C&&j!==d.correctIndex;let O="border-border hover:border-primary/40";return C&&!t&&(O="border-primary bg-primary/5"),q&&(O="border-green-400 bg-green-50"),T&&(O="border-red-400 bg-red-50"),e.jsxs("button",{type:"button",onClick:()=>a(d.id,j),disabled:t,className:`w-full text-left rounded-lg border px-4 py-3 text-sm transition-all flex items-center gap-3 ${O} ${t?"cursor-default":"cursor-pointer"}`,children:[e.jsx("span",{className:`flex-shrink-0 w-6 h-6 rounded-full border-2 flex items-center justify-center text-xs font-bold ${C&&!t?"border-primary bg-primary text-white":q?"border-green-500 bg-green-500 text-white":T?"border-red-500 bg-red-500 text-white":"border-gray-300"}`,children:String.fromCharCode(65+j)}),e.jsx("span",{className:"flex-1",children:_}),q&&e.jsx(F,{className:"h-5 w-5 text-green-600 flex-shrink-0"}),T&&e.jsx(V,{className:"h-5 w-5 text-red-500 flex-shrink-0"})]},j)})}),t&&e.jsxs("div",{className:"mt-3 rounded-lg bg-muted/40 p-3 text-sm text-muted-foreground",children:[e.jsx("strong",{children:"Explanation:"})," ",d.explanation]})]},d.id)})}),e.jsx("div",{className:"mt-8 flex gap-3",children:t?e.jsxs(p,{onClick:w,variant:"outline",className:"gap-2",children:[e.jsx(k,{className:"h-4 w-4"}),"Retake Quiz"]}):e.jsxs(p,{onClick:g,disabled:!v,className:"bg-primary text-white hover:bg-primary/90",children:["Submit Answers (",Object.keys(i).length,"/",x.length,")"]})})]})}function se(){const r=[5,3,8,1,4],i=[],l=[];i.push({array:[...r],comparing:null,swapped:!1,sorted:[],pass:0,annotation:"Starting array: [5, 3, 8, 1, 4]. We'll compare adjacent pairs and swap if out of order."});const t=r.length;for(let s=0;s<t-1;s++){let c=!1;for(let a=0;a<t-1-s;a++)i.push({array:[...r],comparing:[a,a+1],swapped:!1,sorted:[...l],pass:s+1,annotation:`Pass ${s+1}: Compare ${r[a]} and ${r[a+1]}.`}),r[a]>r[a+1]&&([r[a],r[a+1]]=[r[a+1],r[a]],c=!0,i.push({array:[...r],comparing:[a,a+1],swapped:!0,sorted:[...l],pass:s+1,annotation:`Swap! ${r[a+1]} > ${r[a]}, so they switch places.`}));if(l.push(t-1-s),i.push({array:[...r],comparing:null,swapped:!1,sorted:[...l],pass:s+1,annotation:`End of pass ${s+1}: ${r[t-1-s]} is now in its final position.`}),!c)break}for(let s=0;s<t;s++)l.includes(s)||l.push(s);return i.push({array:[...r],comparing:null,swapped:!1,sorted:[...l],pass:0,annotation:"Sorting complete! Array is now [1, 3, 4, 5, 8]."}),i}const b=se();function re(){const[r,i]=h.useState(0),[l,t]=h.useState(!1),s=b[r],c=()=>{r<b.length-1&&i(n=>n+1)},a=()=>{r>0&&i(n=>n-1)},g=()=>{i(0),t(!1)};h.useEffect(()=>{if(!l)return;const n=setInterval(()=>{i(o=>o>=b.length-1?(t(!1),o):o+1)},1200);return()=>clearInterval(n)},[l]);const w=n=>s.sorted.includes(n)?"bg-emerald-400 border-emerald-500":s.comparing&&(n===s.comparing[0]||n===s.comparing[1])?s.swapped?"bg-amber-400 border-amber-500":"bg-blue-400 border-blue-500":"bg-slate-200 border-slate-300",v=n=>s.sorted.includes(n)?"text-emerald-800":s.comparing&&(n===s.comparing[0]||n===s.comparing[1])?s.swapped?"text-amber-800":"text-blue-800":"text-slate-600",d=Math.max(...s.array);return e.jsxs("figure",{className:"my-6 flex flex-col items-center",children:[e.jsxs("div",{className:"w-full max-w-sm sm:max-w-lg",children:[e.jsxs("div",{className:"rounded-xl border border-blue-200/60 bg-gradient-to-br from-blue-50/80 via-white to-indigo-50/60 shadow-sm p-4 sm:p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-3",children:[e.jsx("p",{className:"text-xs font-bold uppercase tracking-widest text-blue-600",children:s.pass>0?`Pass ${s.pass}`:r===0?"Ready":"Done"}),e.jsxs("div",{className:"flex gap-1.5",children:[e.jsxs("span",{className:"inline-flex items-center gap-1 text-[10px] text-slate-500",children:[e.jsx("span",{className:"inline-block w-2.5 h-2.5 rounded-sm bg-blue-400 border border-blue-500"})," Comparing"]}),e.jsxs("span",{className:"inline-flex items-center gap-1 text-[10px] text-slate-500",children:[e.jsx("span",{className:"inline-block w-2.5 h-2.5 rounded-sm bg-amber-400 border border-amber-500"})," Swapped"]}),e.jsxs("span",{className:"inline-flex items-center gap-1 text-[10px] text-slate-500",children:[e.jsx("span",{className:"inline-block w-2.5 h-2.5 rounded-sm bg-emerald-400 border border-emerald-500"})," Sorted"]})]})]}),e.jsx("div",{className:"flex items-end justify-center gap-3 sm:gap-5 mb-4",style:{height:160},children:s.array.map((n,o)=>{const S=n/d*100;return e.jsxs("div",{className:"flex flex-col items-center gap-1",style:{width:48},children:[e.jsx("span",{className:`text-xs font-bold ${v(o)}`,children:n}),e.jsx("div",{className:`w-full rounded-t-md border-2 transition-all duration-300 ${w(o)}`,style:{height:`${S}%`,minHeight:16}}),e.jsxs("span",{className:"text-[10px] text-muted-foreground font-mono",children:["[",o,"]"]})]},o)})}),s.comparing!==null&&e.jsx("div",{className:"flex justify-center mb-3",children:e.jsxs("div",{className:`inline-flex items-center gap-2 rounded-lg border px-3 py-1.5 text-sm font-mono ${s.swapped?"border-amber-300 bg-amber-50 text-amber-800":"border-blue-300 bg-blue-50 text-blue-800"}`,children:[s.array[s.comparing[0]],e.jsx("span",{className:"text-xs font-bold",children:s.swapped?"â":s.array[s.comparing[0]]<=s.array[s.comparing[1]]?"â¤":">"}),s.array[s.comparing[1]],!s.swapped&&s.array[s.comparing[0]]<=s.array[s.comparing[1]]&&e.jsx("span",{className:"text-xs text-blue-500 ml-1",children:"No swap needed"})]})}),e.jsx("div",{className:"rounded-lg bg-amber-50 border border-amber-200 px-3 py-2 text-sm text-amber-800 min-h-[40px] flex items-center",children:s.annotation})]}),e.jsxs("div",{className:"flex items-center justify-center gap-2 mt-4",children:[e.jsx(p,{variant:"outline",size:"sm",onClick:g,children:e.jsx(k,{className:"h-3.5 w-3.5"})}),e.jsx(p,{variant:"outline",size:"sm",onClick:a,disabled:r===0,children:e.jsx(H,{className:"h-3.5 w-3.5"})}),e.jsx(p,{size:"sm",onClick:()=>{r>=b.length-1?g():t(!l)},className:"gap-1.5 min-w-[80px]",children:r>=b.length-1?e.jsxs(e.Fragment,{children:[e.jsx(k,{className:"h-3.5 w-3.5"}),"Restart"]}):l?e.jsxs(e.Fragment,{children:[e.jsx(z,{className:"h-3.5 w-3.5"}),"Pause"]}):e.jsxs(e.Fragment,{children:[e.jsx(W,{className:"h-3.5 w-3.5"}),"Play"]})}),e.jsx(p,{variant:"outline",size:"sm",onClick:()=>{t(!1),c()},disabled:r>=b.length-1,children:e.jsx(R,{className:"h-3.5 w-3.5"})}),e.jsxs("span",{className:"text-xs text-muted-foreground ml-2",children:["Step ",r," / ",b.length-1]})]})]}),e.jsx("figcaption",{className:"text-xs text-muted-foreground text-center mt-3 max-w-md",children:"Bubble Sort repeatedly compares adjacent elements and swaps them if out of order. After each pass, the largest unsorted element âbubblesâ to its final position."})]})}function te(){const r=[5,3,8,1,4],i=[];i.push({array:[...r],sortedBoundary:1,keyIndex:null,keyValue:null,comparingIndex:null,shiftedIndices:[],annotation:"Starting array: [5, 3, 8, 1, 4]. Index 0 is trivially sorted. We insert elements one at a time."});const l=r.length;for(let t=1;t<l;t++){const s=r[t];i.push({array:[...r],sortedBoundary:t,keyIndex:t,keyValue:s,comparingIndex:null,shiftedIndices:[],annotation:`Pick up key = ${s} at index ${t}. Find where it belongs in the sorted portion [0..${t-1}].`});const c=[];let a=t-1;for(;a>=0&&r[a]>s;)i.push({array:[...r],sortedBoundary:t,keyIndex:t,keyValue:s,comparingIndex:a,shiftedIndices:[...c],annotation:`Compare key ${s} with ${r[a]} at index ${a}. ${r[a]} > ${s}, so shift ${r[a]} right.`}),r[a+1]=r[a],c.push(a+1),i.push({array:[...r],sortedBoundary:t,keyIndex:t,keyValue:s,comparingIndex:null,shiftedIndices:[...c],annotation:`Shifted ${r[a+1]} from index ${a} to ${a+1}.`}),a--;a>=0&&i.push({array:[...r],sortedBoundary:t,keyIndex:t,keyValue:s,comparingIndex:a,shiftedIndices:[...c],annotation:`Compare key ${s} with ${r[a]} at index ${a}. ${r[a]} ⤠${s}, so stop here.`}
1),r[a+1]=s,i.push({array:[...r],sortedBoundary:t+1,keyIndex:a+1,keyValue:s,comparingIndex:null,shiftedIndices:[],annotation:`Insert key ${s} at index ${a+1}. Sorted portion is now [0..${t}].`})}return i.push({array:[...r],sortedBoundary:l,keyIndex:null,keyValue:null,comparingIndex:null,shiftedIndices:[],annotation:"Sorting complete! Array is now [1, 3, 4, 5, 8]."}),i}const f=te();function ae(){const[r,i]=h.useState(0),[l,t]=h.useState(!1),s=f[r],c=()=>{r<f.length-1&&i(n=>n+1)},a=()=>{r>0&&i(n=>n-1)},g=()=>{i(0),t(!1)};h.useEffect(()=>{if(!l)return;const n=setInterval(()=>{i(o=>o>=f.length-1?(t(!1),o):o+1)},1200);return()=>clearInterval(n)},[l]);const w=Math.max(...s.array),v=n=>s.keyIndex===n&&s.keyValue!==null&&s.shiftedIndices.length===0&&s.comparingIndex===null?"bg-emerald-400 border-emerald-500":s.comparingIndex===n?"bg-amber-400 border-amber-500":s.keyIndex===n&&s.keyValue!==null?"bg-blue-400 border-blue-500":s.shiftedIndices.includes(n)?"bg-orange-300 border-orange-400":n<s.sortedBoundary?"bg-emerald-200 border-emerald-300":"bg-slate-200 border-slate-300",d=n=>s.keyIndex===n&&s.keyValue!==null&&s.shiftedIndices.length===0&&s.comparingIndex===null?"text-emerald-800":s.comparingIndex===n?"text-amber-800":s.keyIndex===n&&s.keyValue!==null?"text-blue-800":s.shiftedIndices.includes(n)?"text-orange-800":n<s.sortedBoundary?"text-emerald-700":"text-slate-600";return e.jsxs("figure",{className:"my-6 flex flex-col items-center",children:[e.jsxs("div",{className:"w-full max-w-sm sm:max-w-lg",children:[e.jsxs("div",{className:"rounded-xl border border-blue-200/60 bg-gradient-to-br from-blue-50/80 via-white to-indigo-50/60 shadow-sm p-4 sm:p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-3",children:[e.jsx("p",{className:"text-xs font-bold uppercase tracking-widest text-blue-600",children:s.keyValue!==null?`Key: ${s.keyValue}`:r===0?"Ready":"Done"}),e.jsxs("div",{className:"flex gap-1.5 flex-wrap justify-end",children:[e.jsxs("span",{className:"inline-flex items-center gap-1 text-[10px] text-slate-500",children:[e.jsx("span",{className:"inline-block w-2.5 h-2.5 rounded-sm bg-emerald-200 border border-emerald-300"})," Sorted"]}),e.jsxs("span",{className:"inline-flex items-center gap-1 text-[10px] text-slate-500",children:[e.jsx("span",{className:"inline-block w-2.5 h-2.5 rounded-sm bg-blue-400 border border-blue-500"})," Key"]}),e.jsxs("span",{className:"inline-flex items-center gap-1 text-[10px] text-slate-500",children:[e.jsx("span",{className:"inline-block w-2.5 h-2.5 rounded-sm bg-amber-400 border border-amber-500"})," Comparing"]}),e.jsxs("span",{className:"inline-flex items-center gap-1 text-[10px] text-slate-500",children:[e.jsx("span",{className:"inline-block w-2.5 h-2.5 rounded-sm bg-orange-300 border border-orange-400"})," Shifted"]})]})]}),e.jsx("div",{className:"flex items-center gap-3 sm:gap-5 mb-1 px-0",style:{height:16},children:s.array.map((n,o)=>e.jsx("div",{className:"flex justify-center",style:{width:48},children:o<s.sortedBoundary&&e.jsx("span",{className:"text-[9px] font-bold text-emerald-500",children:"sorted"})},o))}),e.jsx("div",{className:"flex items-end justify-center gap-3 sm:gap-5 mb-4",style:{height:160},children:s.array.map((n,o)=>{const S=n/w*100;return e.jsxs("div",{className:"flex flex-col items-center gap-1",style:{width:48},children:[e.jsx("span",{className:`text-xs font-bold ${d(o)}`,children:n}),e.jsx("div",{className:`w-full rounded-t-md border-2 transition-all duration-300 ${v(o)}`,style:{height:`${S}%`,minHeight:16}}),e.jsxs("span",{className:"text-[10px] text-muted-foreground font-mono",children:["[",o,"]"]})]},o)})}),e.jsx("div",{className:"rounded-lg bg-amber-50 border border-amber-200 px-3 py-2 text-sm text-amber-800 min-h-[40px] flex items-center",children:s.annotation})]}),e.jsxs("div",{className:"flex items-center justify-center gap-2 mt-4",children:[e.jsx(p,{variant:"outline",size:"sm",onClick:g,children:e.jsx(k,{className:"h-3.5 w-3.5"})}),e.jsx(p,{variant:"outline",size:"sm",onClick:a,disabled:r===0,children:e.jsx(H,{className:"h-3.5 w-3.5"})}),e.jsx(p,{size:"sm",onClick:()=>{r>=f.length-1?g():t(!l)},className:"gap-1.5 min-w-[80px]",children:r>=f.length-1?e.jsxs(e.Fragment,{children:[e.jsx(k,{className:"h-3.5 w-3.5"}),"Restart"]}):l?e.jsxs(e.Fragment,{children:[e.jsx(z,{className:"h-3.5 w-3.5"}),"Pause"]}):e.jsxs(e.Fragment,{children:[e.jsx(W,{className:"h-3.5 w-3.5"}),"Play"]})}),e.jsx(p,{variant:"outline",size:"sm",onClick:()=>{t(!1),c()},disabled:r>=f.length-1,children:e.jsx(R,{className:"h-3.5 w-3.5"})}),e.jsxs("span",{className:"text-xs text-muted-foreground ml-2",children:["Step ",r," / ",f.length-1]})]})]}),e.jsx("figcaption",{className:"text-xs text-muted-foreground text-center mt-3 max-w-md",children:"Insertion Sort picks each element and inserts it into
1its correct position within the already-sorted portion by shifting larger elements right."})]})}const $="https://www.tutoremy.ai",B=`${$}/resources/computer-science/sorting-algorithms`,Q=[{question:"What is the fastest general-purpose sorting algorithm?",answer:'There is no single "fastest" algorithm for all cases. Quick Sort is often the fastest in practice due to good cache performance and low constant factors, with O(n log n) average time. However, its worst case is O(n²). Merge Sort guarantees O(n log n) in all cases but uses O(n) extra space. Heap Sort guarantees O(n log n) in-place. The best choice depends on your data characteristics, stability requirements, and memory constraints.'},{question:"Why is the O(n log n) lower bound important for comparison sorts?",answer:"The O(n log n) lower bound means no comparison-based sorting algorithm can do better than n log n comparisons in the worst case. This is proven using decision trees: sorting n elements requires distinguishing among n! permutations, and a binary decision tree with n! leaves needs at least logâ(n!) = O(n log n) height. This tells us Merge Sort and Heap Sort are asymptotically optimal."},{question:"When should I use Insertion Sort over Quick Sort or Merge Sort?",answer:"Insertion Sort excels on small arrays (typically n < 20-50) and nearly sorted data, achieving O(n) best-case time. Many optimized sorting implementations (like Timsort in Python and Java) use Insertion Sort for small subarrays within a larger divide-and-conquer framework. It is also ideal when elements arrive one at a time and must be kept sorted (online sorting)."},{question:"What makes a sorting algorithm stable, and why does it matter?",answer:"A stable sort preserves the relative order of elements with equal keys. For example, if you sort students by grade and two students both have an A, a stable sort keeps them in their original order. Stability matters when sorting by multiple keys sequentially: sort by last name, then by first name with a stable sort, and students with the same first name remain sorted by last name."},{question:"Can non-comparison sorts like Counting Sort beat O(n log n)?",answer:"Yes. Non-comparison sorts bypass the O(n log n) lower bound because they do not rely on pairwise comparisons. Counting Sort runs in O(n + k) where k is the value range. Radix Sort runs in O(d(n + k)) where d is the number of digits. However, they have constraints: Counting Sort needs a bounded integer range, and Radix Sort works on fixed-length keys. They cannot sort arbitrary objects."},{question:"What is the difference between in-place and out-of-place sorting?",answer:"An in-place algorithm sorts within the original array using O(1) extra space (e.g., Bubble Sort, Selection Sort, Heap Sort). An out-of-place algorithm requires additional memory proportional to input size, typically O(n), for temporary storage (e.g., Merge Sort). Quick Sort is considered in-place despite using O(log n) stack space for recursion. In-place algorithms are preferred when memory is constrained."}],I=[{id:"introduction",label:"1. Introduction"},{id:"definitions",label:"2. Key Definitions"},{id:"comparison-sorts",label:"3. Comparison-Based Sorts"},{id:"divide-conquer",label:"4. Divide-and-Conquer Sorts"},{id:"non-comparison",label:"5. Non-Comparison Sorts"},{id:"complexity",label:"6. Complexity Comparison"},{id:"code-walkthroughs",label:"7. Code Walkthroughs"},{id:"memory-aids",label:"8. Memory Aids"},{id:"common-mistakes",label:"9. Common Mistakes"},{id:"faq",label:"FAQ"},{id:"quiz",label:"Practice Quiz"}];function Ne(){const r=h.useMemo(()=>I.map(t=>t.id),[]),i=U(r),l=[Y({title:"Sorting Algorithms: College CS Study Guide â Bubble, Merge, Quick, Heap Sort and More",description:"Comprehensive college-level guide to sorting algorithms in computer science. Covers comparison sorts, divide-and-conquer, stability, complexity analysis, code walkthroughs, and a practice quiz.",url:B,datePublished:"2026-03-03T00:00:00Z",dateModified:"2026-03-03T00:00:00Z",teaches:["Sorting Algorithms","Bubble Sort","Selection Sort","Insertion Sort","Merge Sort","Quick Sort","Hea
1p Sort","Time Complexity","Stability"]}),Z([{name:"Resources",url:`${$}/resources`},{name:"Computer Science",url:`${$}/resources/computer-science`},{name:"Sorting Algorithms",url:B}]),J(Q),X({title:"Sorting Algorithms Practice Quiz",description:"Test your understanding of sorting algorithms, time complexity, stability, and divide-and-conquer techniques",questions:x})];return e.jsxs(D,{children:[e.jsx(E,{title:"Sorting Algorithms: College CS Study Guide â Bubble, Merge, Quick, Heap Sort and More",description:"Comprehensive college-level guide to sorting algorithms in computer science. Covers comparison sorts, divide-and-conquer, stability, complexity analysis, code walkthroughs, and a practice quiz.",canonicalUrl:B,ogType:"article",articlePublishedTime:"2026-03-03T00:00:00Z",articleModifiedTime:"2026-03-03T00:00:00Z",jsonLd:l}),e.jsx("div",{className:"border-b bg-muted/40",children:e.jsxs("div",{className:"container flex flex-wrap items-center gap-1.5 py-3 text-sm text-muted-foreground",children:[e.jsx(y,{to:"/resources",className:"hover:text-foreground transition-colors",children:"Resources"}),e.jsx(A,{className:"h-3.5 w-3.5"}),e.jsx(y,{to:"/resources/computer-science",className:"hover:text-foreground transition-colors",children:"Computer Science"}),e.jsx(A,{className:"h-3.5 w-3.5"}),e.jsx("span",{className:"text-foreground font-medium",children:"Sorting Algorithms"})]})}),e.jsx("article",{className:"container py-12 lg:py-16",children:e.jsxs("div",{className:"mx-auto max-w-3xl lg:max-w-5xl lg:flex lg:gap-12",children:[e.jsx(K,{sections:[...I],activeId:i}),e.jsx(G,{sections:[...I],activeId:i}),e.jsxs("div",{className:"min-w-0 max-w-3xl flex-1",children:[e.jsxs("header",{className:"mb-12",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-4",children:[e.jsx("span",{className:"rounded-full bg-primary/10 px-3 py-1 text-xs font-semibold text-primary",children:"Computer Science"}),e.jsx("span",{className:"rounded-full bg-muted px-3 py-1 text-xs font-medium text-muted-foreground",children:"College"})]}),e.jsx("h1",{className:"text-4xl font-bold mb-4 sm:text-5xl",children:"Sorting Algorithms"}),e.jsxs("p",{className:"text-lg text-muted-foreground mb-4",children:[e.jsx("strong",{children:"Sorting"})," is the process of arranging elements of a collection in a particular order â numerical, lexicographical, or by any defined comparison. Sorting algorithms are the bedrock upon which many complex data structures and algorithms are built, making their understanding essential for any computer scientist."]}),e.jsx("p",{className:"text-lg text-muted-foreground",children:"This guide covers comparison-based sorts (Bubble, Selection, Insertion), divide-and-conquer sorts (Merge, Quick, Heap), non-comparison sorts, complexity analysis, code walkthroughs in Python, and a 10-question practice quiz."})]}),e.jsxs("nav",{className:"mb-12 rounded-xl border border-border bg-muted/30 p-6 lg:hidden",children:[e.jsx("h2",{className:"text-sm font-semibold uppercase tracking-wider text-muted-foreground mb-3",children:"In this guide"}),e.jsx("ol",{className:"space-y-1.5 text-sm",children:I.map(t=>e.jsx("li",{children:e.jsx("a",{href:`#${t.id}`,className:"text-primary hover:underline",children:t.label})},t.id))})]}),e.jsxs("section",{id:"introduction",className:"mb-14 scroll-mt-20",children:[e.jsx("a",{href:"#introduction",className:"group no-underline",children:e.jsxs("h2",{className:"text-2xl font-bold mb-4 flex items-center gap-2",children:[e.jsx("span",{className:"flex items-center justify-center w-8 h-8 rounded-full bg-primary text-white text-sm font-bold",children:"1"}),"Introduction",e.jsx(m,{className:"h-4 w-4 text-muted-foreground opacity-0 group-hover:opacity-100 transition-opacity"})]})}),e.jsxs("p",{className:"text-muted-foreground mb-3",children:["In the vast landscape of computer science, efficiency is paramount. Whether you're searching a database, rendering graphics, or processing financial transactions, the ability to organize data quickly and effectively is fundamental. This organization is achieved through ",e.jsx("strong",{children:"sorting algorithms"})," â systematic procedures that rearrange elements of a list into a defined order."]}),e.jsxs("p",{className:"text-muted-foreground mb-4",children:["Sorting is central to ",e.jsx("strong",{children:"Data Structures"})," (binary search trees, priority queues), ",e.jsx("strong",{children:"Algorithms"})," (binary search requires sorted input), ",e.jsx("strong",{children:"Databases"})," (index creation, query optimization), and ",e.jsx("strong",{children:"Operating Systems"})," (scheduling, memory management). A fundamental result in theoretical CS proves that comparison-based sorting requires at least Ω(n log n) comparisons in the worst case."]}),e.jsxs("div",{className:"rounded-xl border border-primary/20 bg-gradient-to-br from-primary/5 to-primary/10 p-5 mb-6",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(M,{className:"h-4 w-4 text-primary flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-primary",children:"In Practice"})]}),e.jsxs("p",{className:"text-base text-foreground leading-relaxed",children:["Modern language runtimes use hybrid sorting algorithms for maximum performance. Python's ",e.jsx("code",{className:"bg-primary/10 px-1 rounded",children:"sorted()"})," uses Timsort (merge sort + insertion sort). C++'s ",e.jsx("code",{className:"bg-primary/10 px-1 rounded",children:"std::sort"})," uses Introsort (quick sort + heap sort + insertion sort). These hybrids leverage the strengths of multiple algorithms."]})]}),e.jsx("img",{src:"/resources/computer-science/sorting-algorithms/image_1_sorting_comparison.png",alt:"Sorting algorithm comparison diagram showing different strategies",className:"w-full max-w-lg mx-auto rounded-xl border border-border shadow-sm mb-4"})]}),e.jsxs("section",{id:"definitions",className:"mb-14 scroll-mt-20",children:[e.jsx("a",{href:"#definitions",className:"group no-underline",children:e.jsxs("h2",{className:"text-2xl font-bold mb-4 flex items-center gap-2",children:[e.jsx("span",{className:"flex items-center justify-center w-8 h-8 rounded-full bg-primary text-white text-sm font-bold",children:"2"}
1),"Key Definitions",e.jsx(m,{className:"h-4 w-4 text-muted-foreground opacity-0 group-hover:opacity-100 transition-opacity"})]})}),e.jsx("p",{className:"text-muted-foreground mb-6",children:"Essential terms for understanding sorting algorithms at the university level."}),e.jsxs("div",{className:"grid sm:grid-cols-2 gap-3 mb-6",children:[e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30 p-4",children:[e.jsx("p",{className:"text-sm font-bold text-foreground",children:"Comparison Sort"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Determines order by comparing pairs of elements; lower bound Ω(n log n)"})]}),e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30 p-4",children:[e.jsx("p",{className:"text-sm font-bold text-foreground",children:"Non-Comparison Sort"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Uses value properties (digits, counts) to sort; can achieve O(n) time"})]}),e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30 p-4",children:[e.jsx("p",{className:"text-sm font-bold text-foreground",children:"Stable Sort"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Preserves relative order of equal elements (Merge, Insertion, Bubble)"})]}),e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30 p-4",children:[e.jsx("p",{className:"text-sm font-bold text-foreground",children:"Unstable Sort"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"May change relative order of equal elements (Quick, Selection, Heap)"})]}),e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30 p-4",children:[e.jsx("p",{className:"text-sm font-bold text-foreground",children:"In-Place Sort"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Uses O(1) extra space; modifies array directly (Selection, Heap, Bubble)"})]}),e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30 p-4",children:[e.jsx("p",{className:"text-sm font-bold text-foreground",children:"Out-of-Place Sort"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Requires O(n) auxiliary space for temporary storage (Merge Sort)"})]}),e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30 p-4",children:[e.jsx("p",{className:"text-sm font-bold text-foreground",children:"Divide and Conquer"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Split problem into subproblems, solve recursively, combine results"})]}),e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30 p-4",children:[e.jsx("p",{className:"text-sm font-bold text-foreground",children:"Pivot"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Element used to partition array in Quick Sort; choice affects performance"})]}),e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30 p-4",children:[e.jsx("p",{className:"text-sm font-bold text-foreground",children:"Heapify"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Process of converting an array into a max-heap or min-heap structure"})]}),e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30 p-4",children:[e.jsx("p",{className:"text-sm font-bold text-foreground",children:"Merge"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Combining two sorted subarrays into one sorted array in O(n) time"})]}),e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30 p-4",children:[e.jsx("p",{className:"text-sm font-bold text-foreground",children:"Adaptive Sort"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Performs better when input is partially sorted (Insertion Sort, Timsort)"})]}),e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30 p-4",children:[e.jsx("p",{className:"text-sm font-bold text-foreground",children:"Inversions"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Pair (i,j) where i < j but arr[i] > arr[j]; measures âunsortednessâ"})]})]})]}),e.jsxs("section",{id:"comparison-sorts",className:"mb-14 scroll-mt-20",children:[e.jsx("a",{href:"#comparison-sorts",className:"group no-underline",children:e.jsxs("h2",{className:"text-2xl font-bold mb-4 flex items-center gap-2",children:[e.jsx("span",{className:"flex items-center justify-center w-8 h-8 rounded-full bg-primary text-white text-sm font-bold",children:"3"}),"Comparison-Based Sorts",e.jsx(m,{className:"h-4 w-4 text-muted-foreground opacity-0 group-hover:opacity-100 transition-opacity"})]})}),e.jsx("p",{className:"text-muted-foreground mb-4",children:"These algorithms determine element order exclusively through pairwise comparisons. They are general-purpose and work on any data type that supports a total ordering."}),e.jsx("h3",{className:"text-xl font-semibold mb-3",children:"Bubble Sort"}),e.jsxs("p",{className:"text-muted-foreground mb-3",children:[e.jsx("strong",{children:"Bubble Sort"})," repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. Larger elements âbubbleâ to the end with each pass. With an early-termination flag, it achieves O(n) on already-sorted input."]}),e.jsxs("div",{className:"grid sm:grid-cols-2 gap-4 mb-6",children:[e.jsxs("div",{className:"rounded-xl border border-blue-200 bg-blue-50 p-5",children:[e.jsx("p",{className:"text-base font-bold text-blue-800 mb-2",children:"Strengths"}),e.jsxs("ul",{className:"text-sm text-blue-700 space-y-1 list-disc list-inside",children:[e.jsx("li",{children:"Simplest to understand and implement"}),e.jsx("li",{children:"Stable sort"}),e.jsx("li",{children:"O(n) best case with early termination"}),e.jsx("li",{children:"In-place with O(1) space"})]})]}),e.jsxs("div",{className:"rounded-xl border border-red-200 bg-red-50 p-5",children:[e.jsx("p",{className:"text-base font-bold text-red-800 mb
1-2",children:"Weaknesses"}),e.jsxs("ul",{className:"text-sm text-red-700 space-y-1 list-disc list-inside",children:[e.jsx("li",{children:"O(n²) average and worst case"}),e.jsx("li",{children:"Many swaps per pass"}),e.jsx("li",{children:"Impractical for large datasets"}),e.jsx("li",{children:"Outperformed by Insertion Sort in most cases"})]})]})]}),e.jsx(re,{}),e.jsx("h3",{className:"text-xl font-semibold mb-3",children:"Selection Sort"}),e.jsxs("p",{className:"text-muted-foreground mb-3",children:[e.jsx("strong",{children:"Selection Sort"})," finds the minimum element from the unsorted portion and swaps it to the front. It makes exactly nâ1 swaps regardless of input, making it useful when writes are expensive. However, it always performs O(n²) comparisons."]}),e.jsx("img",{src:"/resources/computer-science/sorting-algorithms/image_4_stable_sort.png",alt:"Stable vs unstable sort illustration showing how equal elements maintain or change relative order",className:"w-full max-w-lg mx-auto rounded-xl border border-border shadow-sm mb-6"}),e.jsx("h3",{className:"text-xl font-semibold mb-3",children:"Insertion Sort"}),e.jsxs("p",{className:"text-muted-foreground mb-3",children:[e.jsx("strong",{children:"Insertion Sort"})," builds a sorted subarray one element at a time, inserting each new element into its correct position by shifting larger elements right. It excels on small or nearly sorted data, achieving O(n) when the input is already sorted."]}),e.jsxs("div",{className:"rounded-xl border border-primary/20 bg-gradient-to-br from-primary/5 to-primary/10 p-5 mb-4",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(M,{className:"h-4 w-4 text-primary flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-primary",children:"Why Insertion Sort Still Matters"})]}),e.jsx("p",{className:"text-base text-foreground leading-relaxed",children:"Despite its O(n²) worst case, Insertion Sort is used as a subroutine in production sorting algorithms. Python's Timsort and C++'s Introsort switch to Insertion Sort for small subarrays (typically n < 32) because its low overhead and cache-friendly sequential access make it faster than O(n log n) algorithms at small scales."})]}),e.jsx(ae,{})]}),e.jsxs("section",{id:"divide-conquer",className:"mb-14 scroll-mt-20",children:[e.jsx("a",{href:"#divide-conquer",className:"group no-underline",children:e.jsxs("h2",{className:"text-2xl font-bold mb-4 flex items-center gap-2",children:[e.jsx("span",{className:"flex items-center justify-center w-8 h-8 rounded-full bg-primary text-white text-sm font-bold",children:"4"}),"Divide-and-Conquer Sorts",e.jsx(m,{className:"h-4 w-4 text-muted-foreground opacity-0 group-hover:opacity-100 transition-opacity"})]})}),e.jsx("p",{className:"text-muted-foreground mb-4",children:"These algorithms break the array into smaller subproblems, solve them recursively, and combine results. They achieve the optimal O(n log n) time for comparison-based sorting."}),e.jsx("h3",{className:"text-xl font-semibold mb-3",children:"Merge Sort"}),e.jsxs("p",{className:"text-muted-foreground mb-3",children:[e.jsx("strong",{children:"Merge Sort"})," recursively divides the array into halves until single-element subarrays are reached, then merges sorted halves back together. The merge step is the key operation: it combines two sorted lists in O(n) time by comparing front elements."]}),e.jsxs("div",{className:"grid sm:grid-cols-2 gap-4 mb-6",children:[e.jsxs("div",{className:"rounded-xl border border-green-200 bg-green-50 p-5",children:[e.jsx("p",{className:"text-base font-bold text-green-800 mb-2",children:"Strengths"}),e.jsxs("ul",{className:"text-sm text-green-700 space-y-1 list-disc list-inside",children:[e.jsx("li",{children:"Guaranteed O(n log n) in all cases"}),e.jsx("li",{children:"Stable sort"}),e.jsx("li",{children:"Excellent for linked lists (no random access needed)"}),e.jsx("li",{children:"Parallelizable â subarrays sorted independently"})]})]}),e.jsxs("div",{className:"rounded-xl border border-red-200 bg-red-50 p-5",children:[e.jsx("p",{className:"text-base font-bold text-red-800 mb
1-2",children:"Weaknesses"}),e.jsxs("ul",{className:"text-sm text-red-700 space-y-1 list-disc list-inside",children:[e.jsx("li",{children:"Requires O(n) auxiliary space"}),e.jsx("li",{children:"Not in-place"}),e.jsx("li",{children:"Higher constant factors than Quick Sort in practice"}),e.jsx("li",{children:"Extra memory allocation overhead"})]})]})]}),e.jsx("img",{src:"/resources/computer-science/sorting-algorithms/image_2_merge_sort.png",alt:"Merge sort divide-and-conquer tree showing recursive splitting and merging of subarrays",className:"w-full max-w-lg mx-auto rounded-xl border border-border shadow-sm mb-6"}),e.jsx("h3",{className:"text-xl font-semibold mb-3",children:"Quick Sort"}),e.jsxs("p",{className:"text-muted-foreground mb-3",children:[e.jsx("strong",{children:"Quick Sort"})," selects a ",e.jsx("strong",{children:"pivot"})," element, partitions the array so all smaller elements are left and all larger elements are right, then recursively sorts each partition. Pivot selection is critical: a bad pivot leads to O(n²) worst case, but randomized or median-of-three selection gives O(n log n) on average."]}),e.jsx("img",{src:"/resources/computer-science/sorting-algorithms/image_3_quick_sort.png",alt:"Quick sort partitioning process showing pivot selection and element rearrangement",className:"w-full max-w-lg mx-auto rounded-xl border border-border shadow-sm mb-6"}),e.jsxs("div",{className:"rounded-xl border border-amber-200 bg-amber-50 p-5 mb-6",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(u,{className:"h-4 w-4 text-amber-600 flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-amber-700",children:"Pivot Selection Matters"})]}),e.jsxs("p",{className:"text-sm text-amber-800",children:["If you always pick the first or last element as pivot on already-sorted data, Quick Sort degrades to O(n²). Use ",e.jsx("strong",{children:"randomized pivot"})," or ",e.jsx("strong",{children:"median-of-three"})," (median of first, middle, last elements) to avoid this. C++'s Introsort falls back to Heap Sort if recursion depth exceeds 2 log n."]})]}),e.jsx("h3",{className:"text-xl font-semibold mb-3",children:"Heap Sort"}),e.jsxs("p",{className:"text-muted-foreground mb-3",children:[e.jsx("strong",{children:"Heap Sort"})," builds a max-heap from the input array, then repeatedly extracts the maximum element and places it at the end. It guarantees O(n log n) in all cases with O(1) extra space, combining the time guarantee of Merge Sort with the space efficiency of in-place algorithms."]}),e.jsxs("div",{className:"grid sm:grid-cols-3 gap-3 mb-4",children:[e.jsxs("div",{className:"rounded-xl border border-purple-200 bg-purple-50 p-4 text-center",children:[e.jsx("p",{className:"text-sm font-bold text-purple-800",children:"Build Heap"}),e.jsx("p",{className:"text-xs text-purple-700 mt-1",children:"Convert array to max-heap in O(n)"})]}),e.jsxs("div",{className:"rounded-xl border border-purple-200 bg-purple-50 p-4 text-center",children:[e.jsx("p",{className:"text-sm font-bold text-purple-800",children:"Extract Max"}),e.jsx("p",{className:"text-xs text-purple-700 mt-1",children:"Swap root with end, shrink heap"})]}),e.jsxs("div",{className:"rounded-xl border border-purple-200 bg-purple-50 p-4 text-center",children:[e.jsx("p",{className:"text-sm font-bold text-purple-800",children:"Heapify Down"}),e.jsx("p",{className:"text-xs text-purple-700 mt-1",children:"Restore heap property in O(log n)"})]})]})]}),e.jsxs("section",{id:"non-comparison",className:"mb-14 scroll-mt-20",children:[e.jsx("a",{href:"#non-comparison",className:"group no-underline",children:e.jsxs("h2",{className:"text-2xl font-bold mb-4 flex items-center gap-2",children:[e.jsx("span",{className:"flex items-center justify-center w-8 h-8 rounded-full bg-primary text-white text-sm font-bold",children:"5"}),"Non-Comparison Sorts",e.jsx(m,{className:"h-4 w-4 text-muted-foreground opacity-0 group-hover:opacity-100 transition-opacity"})]})}),e.jsx("p",{className:"text-muted-foreground mb-4",children:"Non-comparison sorts bypass the Ω(n log n) lower bound by exploiting properties of the data (integer range, digit structure) rather than relying on pairwise comparisons."}),e.jsxs("div",{className:"grid sm:grid-cols-3 gap-4 mb-6",children:[e.jsxs("div",{className:"rounded-xl border border-blue-200 bg-blue-50 p-5",children:[e.jsx("p",{className:"text-base font-bold text-blue-800 mb-2",children:"Counting Sort"}),e.jsx("p",{className:"text-sm text-blue-700 mb-2",children:"Counts occurrences of each value, then places elements based on cumulative counts."}),e.jsxs("p",{className:"text-xs text-blue-600",children:[e.jsx("strong",{children:"Time:"})," O(n + k)"]}),e.jsxs("p",{className:"text-xs text-blue-600",children:[e.jsx("strong",{children:"Space:"})," O(n + k)"]}),e.jsxs("p",{className:"text-xs text-blue-600",children:[e.jsx("strong",{children:"Stable:"})," Yes"]})]}),e.jsxs("div",{className:"rounded-xl border border-green-200 bg-green-50 p-5",children:[e.jsx("p",{className:"text-base font-bold text-green-800 mb-2",children:"Radix Sort"}),e.jsx("p",{className:"text-sm text-green-700 mb-2",children:"Sorts digit-by-digit from least significant to most significant using a stable sub-sort."}),e.jsxs("p",{className:"text-xs text-green-600",children:[e.jsx("strong",{children:"Time:"})," O(d(n + k))"]}
1),e.jsxs("p",{className:"text-xs text-green-600",children:[e.jsx("strong",{children:"Space:"})," O(n + k)"]}),e.jsxs("p",{className:"text-xs text-green-600",children:[e.jsx("strong",{children:"Stable:"})," Yes"]})]}),e.jsxs("div",{className:"rounded-xl border border-purple-200 bg-purple-50 p-5",children:[e.jsx("p",{className:"text-base font-bold text-purple-800 mb-2",children:"Bucket Sort"}),e.jsx("p",{className:"text-sm text-purple-700 mb-2",children:"Distributes elements into buckets, sorts each bucket, then concatenates results."}),e.jsxs("p",{className:"text-xs text-purple-600",children:[e.jsx("strong",{children:"Time:"})," O(n + k) avg"]}),e.jsxs("p",{className:"text-xs text-purple-600",children:[e.jsx("strong",{children:"Space:"})," O(n + k)"]}),e.jsxs("p",{className:"text-xs text-purple-600",children:[e.jsx("strong",{children:"Stable:"})," Depends on sub-sort"]})]})]}),e.jsx("img",{src:"/resources/computer-science/sorting-algorithms/image_5_sorting_applications.png",alt:"Real-world sorting applications showing databases, search engines, and scheduling",className:"w-full max-w-lg mx-auto rounded-xl border border-border shadow-sm"})]}),e.jsxs("section",{id:"complexity",className:"mb-14 scroll-mt-20",children:[e.jsx("a",{href:"#complexity",className:"group no-underline",children:e.jsxs("h2",{className:"text-2xl font-bold mb-4 flex items-center gap-2",children:[e.jsx("span",{className:"flex items-center justify-center w-8 h-8 rounded-full bg-primary text-white text-sm font-bold",children:"6"}),"Complexity Comparison",e.jsx(m,{className:"h-4 w-4 text-muted-foreground opacity-0 group-hover:opacity-100 transition-opacity"})]})}),e.jsx("p",{className:"text-muted-foreground mb-4",children:"This table summarizes the time and space complexity for each algorithm across best, average, and worst cases."}),e.jsx("div",{className:"overflow-x-auto mb-6",children:e.jsxs("table",{className:"w-full text-sm border-collapse",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b-2 border-border",children:[e.jsx("th",{className:"text-left py-3 px-3 font-bold text-foreground",children:"Algorithm"}),e.jsx("th",{className:"text-left py-3 px-3 font-bold text-foreground",children:"Best"}),e.jsx("th",{className:"text-left py-3 px-3 font-bold text-foreground",children:"Average"}),e.jsx("th",{className:"text-left py-3 px-3 font-bold text-foreground",children:"Worst"}),e.jsx("th",{className:"text-left py-3 px-3 font-bold text-foreground",children:"Space"}),e.jsx("th",{className:"text-center py-3 px-3 font-bold text-foreground",children:"Stable"})]})}),e.jsxs("tbody",{className:"text-muted-foreground",children:[e.jsxs("tr",{className:"border-b border-border",children:[e.jsx("td",{className:"py-2.5 px-3 font-medium text-foreground",children:"Bubble Sort"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n²)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n²)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(1)"}),e.jsx("td",{className:"py-2.5 px-3 text-center text-green-600 font-bold",children:"Yes"})]}),e.jsxs("tr",{className:"border-b border-border bg-muted/20",children:[e.jsx("td",{className:"py-2.5 px-3 font-medium text-foreground",children:"Selection Sort"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n²)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n²)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n²)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(1)"}),e.jsx("td",{className:"py-2.5 px-3 text-center text-red-600 font-bold",children:"No"})]}),e.jsxs("tr",{className:"border-b border-border",children:[e.jsx("td",{className:"py-2.5 px-3 font-medium text-foreground",children:"Insertion Sort"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n²)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n²)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(1)"}),e.jsx("td",{className:"py-2.5 px-3 text-center text-green-600 font-bold",children:"Yes"})]}),e.jsxs("tr",{className:"border-b border-border bg-muted/20",children:[e.jsx("td",{className:"py-2.5 px-3 font-medium text-foreground",children:"Merge Sort"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n log n)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n log n)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n log n)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n)"}),e.jsx("td",{className:"py-2.5 px-3 text-center text-green-600 font-bold",children:"Yes"})]}),e.jsxs("tr",{className:"border-b border-border",children:[e.jsx("td",{className:"py-2.5 px-3 font-medium text-foreground",children:"Qu
1ick Sort"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n log n)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n log n)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs text-red-600",children:"O(n²)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(log n)"}),e.jsx("td",{className:"py-2.5 px-3 text-center text-red-600 font-bold",children:"No"})]}),e.jsxs("tr",{className:"border-b border-border bg-muted/20",children:[e.jsx("td",{className:"py-2.5 px-3 font-medium text-foreground",children:"Heap Sort"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n log n)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n log n)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(n log n)"}),e.jsx("td",{className:"py-2.5 px-3 font-mono text-xs",children:"O(1)"}),e.jsx("td",{className:"py-2.5 px-3 text-center text-red-600 font-bold",children:"No"})]})]})]})}),e.jsxs("div",{className:"rounded-xl border border-primary/20 bg-gradient-to-br from-primary/5 to-primary/10 p-5",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(M,{className:"h-4 w-4 text-primary flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-primary",children:"Choosing an Algorithm"})]}),e.jsxs("p",{className:"text-base text-foreground leading-relaxed",children:[e.jsx("strong",{children:"Need guaranteed performance?"})," Merge Sort or Heap Sort. ",e.jsx("strong",{children:"Need in-place + fast average?"})," Quick Sort. ",e.jsx("strong",{children:"Small or nearly sorted data?"})," Insertion Sort. ",e.jsx("strong",{children:"Need stability?"})," Merge Sort or Insertion Sort. ",e.jsx("strong",{children:"Integer keys with bounded range?"})," Counting Sort or Radix Sort."]})]})]}),e.jsxs("section",{id:"code-walkthroughs",className:"mb-14 scroll-mt-20",children:[e.jsx("a",{href:"#code-walkthroughs",className:"group no-underline",children:e.jsxs("h2",{className:"text-2xl font-bold mb-4 flex items-center gap-2",children:[e.jsx("span",{className:"flex items-center justify-center w-8 h-8 rounded-full bg-primary text-white text-sm font-bold",children:"7"}),"Code Walkthroughs",e.jsx(m,{className:"h-4 w-4 text-muted-foreground opacity-0 group-hover:opacity-100 transition-opacity"})]})}),e.jsxs("div",{className:"rounded-xl border border-blue-200 bg-blue-50 p-5 mb-6",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(P,{className:"h-4 w-4 text-blue-600 flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-blue-600",children:"Introductory"})]}),e.jsx("p",{className:"text-base font-bold text-blue-800 mb-3",children:"Bubble Sort"}),e.jsx("pre",{className:"rounded-lg bg-gray-900 text-green-400 p-4 text-sm overflow-x-auto mb-3 font-mono",children:`def bubble_sort(arr): 2 n = len(arr) 3 for i in range(n - 1): 4 swapped = False 5 for j in range(n - 1 - i): 6 if arr[j] > arr[j + 1]: 7 arr[j], arr[j + 1] = arr[j + 1], arr[j] 8 swapped = True 9 if not swapped: 10 break 11 return arr`}),e.jsxs("div",{className:"space-y-2 text-sm text-blue-700",children:[e.jsxs("p",{children:[e.jsx("strong",{children:"Line 3:"})," Outer loop runs nâ1 passes. After each pass, the largest unsorted element reaches its final position."]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Line 5:"})," Inner loop compares adjacent pairs. Range shrinks by ",e.jsx("code",{className:"bg-blue-100 px-1 rounded",children:"i"})," because last ",e.jsx("code",{className:"bg-blue-100 px-1 rounded",children:"i"})," elements are already sorted."]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Lines 6â8:"})," If left element is greater, swap them. Track whether any swap occurred."]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Lines 9â10:"})," Early termination â if no swaps in a full pass, array is sorted."]})]}),e.jsxs("div",{className:"mt-3 flex gap-3 text-xs",children:[e.jsx("span",{className:"rounded bg-blue-100 px-2 py-0.5 text-blue-700",children:"Time: O(n²)"}),e.jsx("span",{className:"rounded bg-blue-100 px-2 py-0.5 text-blue-700",children:"Space: O(1)"})]}),e.jsx("p",{className:"text-xs text-blue-600 mt-2 italic",children:"Key insight: Each pass guarantees the next-largest element reaches its final position. The swapped flag enables O(n) best case."})]}),e.jsxs("div",{className:"rounded-xl border border-green-200 bg-green-50 p-5 mb-6",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(P,{className:"h-4 w-4 text-green-600 flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-green-600",children:"Intermediate"})]}),e.jsx("p",{className:"text-base font-bold text-green-800 mb-3",children:"Merge Sort"}),e.jsx("pre",{className:"rounded-lg bg-gray-900 text-green-400 p-4 text-sm overflow-x-auto mb-3 font-mono",children:`def merge_sort(arr): 12 if len(arr) <= 1: 13 return arr 14 mid = len(arr) // 2 15 left = merge_sort(arr[:mid]) 16 right = merge_sort(arr[mid:]) 17 return merge(left, right) 18 19def merge(left, right): 20 result, i, j = [], 0, 0 21 while i < len(left) and j < len(right): 22 if left[i] <= right[j]: 23 result.append(left[i]); i += 1 24 else: 25 result.append(right[j]); j += 1 26 result.extend(left[i:]) 27 result.extend(right[j:]) 28 return result`}),e.jsxs("div",{className:"space-y-2 text-sm text-green-700",children:[e.jsxs("p",{children:[e.jsx("strong",{children:"Lines 2â3:"})," Base case â a single-element array is already sorted."]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Lines 4â6:"})," Divide: split at midpoint and recursively sort each half."]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Lines 11â15:"})," Merge: compare front elements of each sorted half, append the smaller one. The ",e.jsx("code",{className:"bg-green-100 px-1 rounded",children:"<="})," ensures stability."]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Lines 16â17:"})," Append remaining elements from whichever half is not exhausted."]})]}),e.jsxs("div",{className:"mt-3 flex gap-3 text-xs",children:[e.jsx("span",{className:"rounded bg-green-100 px-2 py-0.5 text-green-700",children:"Time: O(n log n)"}),e.jsx("span",{className:"rounded bg-green-100 px-2 py-0.5 text-green-700",children:"Space: O(n)"})]}),e.jsx("p",{className:"text-xs text-green-600 mt-2 italic",children:"Key insight: Work happens in the merge step. Each level of recursion processes all n elements, and there are log n levels."})]}),e.jsxs("div",{className:"rounded-xl border border-purple-200 bg-purple-50 p-5 mb-6",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(P,{className:"h-4 w-4 text-purple-600 flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-purple-600",children:"Intermediate"})]}),e.jsx("p",{className:"text-base font-bold text-purple-800 mb-3",children:"Qu
28ick Sort (Lomuto Partition)"}),e.jsx("pre",{className:"rounded-lg bg-gray-900 text-green-400 p-4 text-sm overflow-x-auto mb-3 font-mono",children:`def quick_sort(arr, lo, hi): 29 if lo < hi: 30 p = partition(arr, lo, hi) 31 quick_sort(arr, lo, p - 1) 32 quick_sort(arr, p + 1, hi) 33 34def partition(arr, lo, hi): 35 pivot = arr[hi] 36 i = lo - 1 37 for j in range(lo, hi): 38 if arr[j] <= pivot: 39 i += 1 40 arr[i], arr[j] = arr[j], arr[i] 41 arr[i + 1], arr[hi] = arr[hi], arr[i + 1] 42 return i + 1`}),e.jsxs("div",{className:"space-y-2 text-sm text-purple-700",children:[e.jsxs("p",{children:[e.jsx("strong",{children:"Line 2:"})," Base case â subarrays of size 0 or 1 need no sorting."]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Line 3:"})," Partition returns the final index of the pivot after rearranging."]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Lines 8â9:"})," Choose last element as pivot. ",e.jsx("code",{className:"bg-purple-100 px-1 rounded",children:"i"})," tracks boundary of elements ⤠pivot."]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Lines 10â13:"})," Scan with ",e.jsx("code",{className:"bg-purple-100 px-1 rounded",children:"j"}),". When ",e.jsx("code",{className:"bg-purple-100 px-1 rounded",children:"arr[j] ⤠pivot"}),", expand the âsmallâ partition by swapping."]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Line 14:"})," Place pivot at its correct position between the two partitions."]})]}),e.jsxs("div",{className:"mt-3 flex gap-3 text-xs",children:[e.jsx("span",{className:"rounded bg-purple-100 px-2 py-0.5 text-purple-700",children:"Time: O(n log n) avg"}),e.jsx("span",{className:"rounded bg-purple-100 px-2 py-0.5 text-purple-700",children:"Space: O(log n)"})]}),e.jsx("p",{className:"text-xs text-purple-600 mt-2 italic",children:"Key insight: Work happens in the partition step. The pivot ends up in its final sorted position, and no merge is needed."})]})]}),e.jsxs("section",{id:"memory-aids",className:"mb-14 scroll-mt-20",children:[e.jsx("a",{href:"#memory-aids",className:"group no-underline",children:e.jsxs("h2",{className:"text-2xl font-bold mb-4 flex items-center gap-2",children:[e.jsx("span",{className:"flex items-center justify-center w-8 h-8 rounded-full bg-primary text-white text-sm font-bold",children:"8"}),"Memory Aids",e.jsx(m,{className:"h-4 w-4 text-muted-foreground opacity-0 group-hover:opacity-100 transition-opacity"})]})}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"rounded-xl border border-primary/20 bg-gradient-to-br from-primary/5 to-primary/10 p-5",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(N,{className:"h-4 w-4 text-primary flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-primary",children:"Bubble Sort"})]}),e.jsx("p",{className:"text-xl italic font-serif text-foreground leading-relaxed",children:"âBubbles rise to the surface â largest elements bubble to the end with each pass.â"})]}),e.jsxs("div",{className:"rounded-xl border border-primary/20 bg-gradient-to-br from-primary/5 to-primary/10 p-5",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(N,{className:"h-4 w-4 text-primary flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-primary",children:"Selection Sort"})]}),e.jsx("p",{className:"text-xl italic font-serif text-foreground leading-relaxed",children:"âPick the smallest card from your hand and place it in the sorted pile. Repeat.â"})]}),e.jsxs("div",{className:"rounded-xl border border-primary/20 bg-gradient-to-br from-primary/5 to-primary/10 p-5",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(N,{className:"h-4 w-4 text-primary flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-primary",children:"Insertion Sort"})]}),e.jsx("p",{className:"text-xl italic font-serif text-foreground leading-relaxed",children:"âSort like organizing playing cards â pick each card and slide it into
42its correct position in your hand.â"})]}),e.jsxs("div",{className:"rounded-xl border border-primary/20 bg-gradient-to-br from-primary/5 to-primary/10 p-5",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(N,{className:"h-4 w-4 text-primary flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-primary",children:"Merge Sort"})]}),e.jsx("p",{className:"text-xl italic font-serif text-foreground leading-relaxed",children:"âSplit the deck in half, sort each half, then merge by comparing top cards. Divide and conquer!â"})]}),e.jsxs("div",{className:"rounded-xl border border-primary/20 bg-gradient-to-br from-primary/5 to-primary/10 p-5",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(N,{className:"h-4 w-4 text-primary flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-primary",children:"Quick Sort"})]}),e.jsx("p",{className:"text-xl italic font-serif text-foreground leading-relaxed",children:"âPick a volunteer (pivot). Everyone shorter goes left, everyone taller goes right. Repeat in each group.â"})]}),e.jsxs("div",{className:"rounded-xl border border-primary/20 bg-gradient-to-br from-primary/5 to-primary/10 p-5",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(N,{className:"h-4 w-4 text-primary flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-primary",children:"Stability Mnemonic"})]}),e.jsxs("p",{className:"text-xl italic font-serif text-foreground leading-relaxed",children:["âBIM is stable: ",e.jsx("strong",{children:"B"}),"ubble, ",e.jsx("strong",{children:"I"}),"nsertion, ",e.jsx("strong",{children:"M"}),"erge. The rest (Selection, Quick, Heap) are not.â"]})]})]})]}),e.jsxs("section",{id:"common-mistakes",className:"mb-14 scroll-mt-20",children:[e.jsx("a",{href:"#common-mistakes",className:"group no-underline",children:e.jsxs("h2",{className:"text-2xl font-bold mb-4 flex items-center gap-2",children:[e.jsx("span",{className:"flex items-center justify-center w-8 h-8 rounded-full bg-primary text-white text-sm font-bold",children:"9"}),"Common Mistakes",e.jsx(m,{className:"h-4 w-4 text-muted-foreground opacity-0 group-hover:opacity-100 transition-opacity"})]})}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"rounded-xl border border-amber-200 bg-gradient-to-br from-amber-50 to-amber-100/60 p-5",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(u,{className:"h-4 w-4 text-amber-600 flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-amber-700",children:"Off-by-One Errors in Loop Bounds"})]}),e.jsxs("p",{className:"text-base text-amber-900 font-medium leading-relaxed",children:["Using ",e.jsx("code",{className:"bg-amber-100 px-1 rounded",children:"range(n)"})," instead of ",e.jsx("code",{className:"bg-amber-100 px-1 rounded",children:"range(n - 1)"})," in Bubble Sort's outer loop"]}),e.jsxs("p",{className:"text-sm text-amber-800 mt-2",children:["Carefully trace loop bounds with a small example. In Bubble Sort, the outer loop needs nâ1 passes, and the inner loop range shrinks by ",e.jsx("code",{className:"bg-amber-100 px-1 rounded",children:"i"})," each pass to avoid accessing out-of-bounds indices."]})]}),e.jsxs("div",{className:"rounded-xl border border-amber-200 bg-gradient-to-br from-amber-50 to-amber-100/60 p-5",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(u,{className:"h-4 w-4 text-amber-600 flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-amber-700",children:"Bad Pivot Selection in Quick Sort"})]}),e.jsx("p",{className:"text-base text-amber-900 font-medium leading-relaxed",children:"Always choosing the first or last element as pivot on sorted input"}),e.jsx("p",{className:"text-sm text-amber-800 mt-2",children:"This causes O(n²) worst-case behavior because every partition is maximally unbalanced. Use randomized pivot selection or median-of-three to prevent this. Introsort falls back to Heap Sort if depth exceeds 2 log n."})]}),e.jsxs("div",{className:"rounded-xl border border-amber-200 bg-gradient-to-br from-amber-50 to-amber-100/60 p-5",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(u,{className:"h-4 w-4 text-amber-600 flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-amber-700",children:"Confusing Average-Case with Worst-Case"})]}),e.jsx("p",{className:"text-base text-amber-900 font-medium leading-relaxed",children:"âQuick Sort is always O(n log n)â"}),e.jsx("p",{className:"text-sm text-amber-800 mt-2",children:"Qu
42ick Sort's average case is O(n log n), but its worst case is O(n²). For safety-critical systems or adversarial inputs, Merge Sort or Heap Sort with guaranteed O(n log n) worst case is a better choice."})]}),e.jsxs("div",{className:"rounded-xl border border-amber-200 bg-gradient-to-br from-amber-50 to-amber-100/60 p-5",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(u,{className:"h-4 w-4 text-amber-600 flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-amber-700",children:"Using Unstable Sort When Stability Is Required"})]}),e.jsx("p",{className:"text-base text-amber-900 font-medium leading-relaxed",children:"Sorting records by multiple fields with Quick Sort"}),e.jsx("p",{className:"text-sm text-amber-800 mt-2",children:"If you sort by name then by department using an unstable sort, people in the same department will lose their alphabetical ordering. Use Merge Sort or Insertion Sort when stability matters, or use a composite key."})]}),e.jsxs("div",{className:"rounded-xl border border-amber-200 bg-gradient-to-br from-amber-50 to-amber-100/60 p-5",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(u,{className:"h-4 w-4 text-amber-600 flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-amber-700",children:"Stack Overflow with Deep Recursion"})]}),e.jsx("p",{className:"text-base text-amber-900 font-medium leading-relaxed",children:"Running recursive Quick Sort or Merge Sort on very large arrays"}),e.jsxs("p",{className:"text-sm text-amber-800 mt-2",children:["Python's default recursion limit is 1000. For n = 100,000 with worst-case Quick Sort, you get 100,000 recursive calls. Use iterative implementations or increase ",e.jsx("code",{className:"bg-amber-100 px-1 rounded",children:"sys.setrecursionlimit()"})," carefully."]})]}),e.jsxs("div",{className:"rounded-xl border border-amber-200 bg-gradient-to-br from-amber-50 to-amber-100/60 p-5",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(u,{className:"h-4 w-4 text-amber-600 flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-amber-700",children:"Forgetting Merge Sort's O(n) Space"})]}),e.jsx("p",{className:"text-base text-amber-900 font-medium leading-relaxed",children:"Using Merge Sort on memory-constrained systems without accounting for auxiliary space"}),e.jsx("p",{className:"text-sm text-amber-800 mt-2",children:"Merge Sort requires O(n) auxiliary space for the temporary array during merging. For very large datasets, this can cause memory exhaustion. Consider Heap Sort (O(1) space) or in-place merge sort variants if memory is limited."})]}),e.jsxs("div",{className:"rounded-xl border border-amber-200 bg-gradient-to-br from-amber-50 to-amber-100/60 p-5",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(u,{className:"h-4 w-4 text-amber-600 flex-shrink-0"}),e.jsx("span",{className:"text-xs font-bold uppercase tracking-widest text-amber-700",children:"Not Handling Edge Cases"})]}),e.jsx("p",{className:"text-base text-amber-900 font-medium leading-relaxed",children:"Failing to test empty arrays, single elements, or all-equal elements"}),e.jsx("p",{className:"text-sm text-amber-800 mt-2",children:"Always verify your implementation handles edge cases: empty arrays (length 0), single elements (already sorted), all identical values (should return unchanged), and already-sorted or reverse-sorted input."})]})]})]}),e.jsxs("section",{id:"faq",className:"mb-14 scroll-mt-20",children:[e.jsx("a",{href:"#faq",className:"group no-underline",children:e.jsxs("h2",{className:"text-2xl font-bold mb-6 flex items-center gap-2",children:["Frequently Asked Questions",e.jsx(m,{className:"h-4 w-4 text-muted-foreground opacity-0 group-hover:opacity-100 transition-opacity"})]})}),e.jsx("dl",{className:"space-y-5",children:Q.map((t,s)=>e.jsxs("div",{className:"rounded-xl border border-border p-5",children:[e.jsx("dt",{className:"font-semibold text-foreground mb-2",children:t.question}),e.jsx("dd",{className:"text-muted-foreground",children:t.answer})]},s))})]}),e.jsxs("section",{id:"quiz",className:"mb-14 scroll-mt-20",children:[e.jsx("a",{href:"#quiz",className:"group no-underline",children:e.jsxs("h2",{className:"text-2xl font-bold mb-2 flex items-center gap-2",children:["Practice Quiz",e.jsx(m,{className:"h-4 w-4 text-muted-foreground opacity-0 group-hover:opacity-100 transition-opacity"})]})}),e.jsx(ee,{headless:!0})]}),e.jsxs("section",{className:"mb-14 rounded-xl border border-primary/20 bg-gradient-to-br from-primary/5 to-primary/10 p-6",children:[e.jsx("h2",{className:"text-xl font-semibold mb-3",children:"Study Tips"}),e.jsxs("ul",{className:"space-y-2 text-muted-foreground",children:[e.jsxs("li",{children:[e.jsx("strong",{children:"Trace by hand:"})," Walk through each algorithm with a small array (5-7 elements) on paper. Draw the state after every swap or recursive call."]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Compare side by side:"})," Sort the same array with Bubble, Merge, and Quick Sort. Count comparisons and swaps to internalize complexity differences."]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Implement from memory:"})," After studying, close your notes and code each algorithm from scratch. This reveals gaps in understanding."]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Visualize recursion trees:"})," Draw the recursion tree for Merge Sort and Quick Sort to understand why they achieve O(n log n) and how pivot selection affects depth."]})]})]}),e.jsxs("section",{className:"rounded-xl border border-border bg-muted/30 p-6",children:[e.jsx("h2",{className:"text-xl font-semibold mb-3",children:"Related Topics"}),e.jsxs("ul",{className:"space-y-2",children:[e.jsx("li",{children:e.jsx(y,{to:"/resources/computer-science/arrays-and-linked-lists",className:"text-primary hover:underline",children:"Arrays & Linked Lists"})}),e.jsx("li",{children:e.jsx(y,{to:"/resources/computer-science/trees-and-graphs",className:"text-primary hover:underline",children:"Trees & Graphs"})}),e.jsx("li",{children:e.jsx(y,{to:"/resources/computer-science/big-o-notation",className:"text-primary hover:underline",children:"Big-O Notation"})}),e.jsx("li",{children:e.jsx(y,{to:"/resources/computer-science/recursion",className:"text-primary hover:underline",children:"Recursion"})})]})]})]})]})})]})}export{Ne as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.