-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue-searching-sorting-hashing.html
More file actions
1089 lines (943 loc) · 40.7 KB
/
queue-searching-sorting-hashing.html
File metadata and controls
1089 lines (943 loc) · 40.7 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Data Structures Notes - ZenYukti</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 20px;
line-height: 1.6;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: white;
border-radius: 15px;
overflow: hidden;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 30px;
text-align: center;
border-bottom: 5px solid #f093fb;
}
.header h1 {
font-size: 2.5em;
margin-bottom: 10px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
.header p {
font-size: 1.2em;
opacity: 0.9;
}
.content {
padding: 40px;
}
.section {
margin-bottom: 40px;
padding: 25px;
border-radius: 10px;
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
border-left: 5px solid #667eea;
}
.section h2 {
color: #667eea;
margin-bottom: 15px;
font-size: 1.8em;
border-bottom: 2px solid #764ba2;
padding-bottom: 10px;
}
.section h3 {
color: #764ba2;
margin-top: 20px;
margin-bottom: 10px;
font-size: 1.4em;
}
.definition {
background: #fff;
padding: 15px;
border-radius: 8px;
margin: 15px 0;
border-left: 4px solid #f093fb;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.code-block {
background: #2d3748;
color: #68d391;
padding: 20px;
border-radius: 8px;
overflow-x: auto;
margin: 15px 0;
font-family: 'Courier New', monospace;
font-size: 1.1em;
box-shadow: 0 4px 6px rgba(0,0,0,0.3);
white-space: pre;
line-height: 1.6;
}
.code-block code {
color: #68d391;
white-space: pre;
}
.key-points {
background: #fff3cd;
padding: 15px;
border-radius: 8px;
margin: 15px 0;
border-left: 4px solid #ffc107;
}
.key-points ul {
margin-left: 20px;
}
.key-points li {
margin: 8px 0;
}
.complexity {
display: inline-block;
background: #e7f3ff;
padding: 5px 15px;
border-radius: 5px;
margin: 5px 0;
color: #0066cc;
font-weight: bold;
}
.footer {
background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
color: white;
padding: 25px;
text-align: center;
border-top: 5px solid #f093fb;
}
.footer p {
margin: 5px 0;
}
table {
width: 100%;
border-collapse: collapse;
margin: 15px 0;
background: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
th {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 12px;
text-align: left;
}
td {
padding: 12px;
border-bottom: 1px solid #e2e8f0;
}
.viva-section {
background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
padding: 25px;
border-radius: 10px;
margin: 30px 0;
border-left: 5px solid #ff6b6b;
}
.viva-section h2 {
color: #c92a2a;
margin-bottom: 20px;
}
.viva-qa {
background: white;
padding: 15px;
margin: 10px 0;
border-radius: 8px;
border-left: 4px solid #ff6b6b;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.viva-qa strong {
color: #c92a2a;
display: block;
margin-bottom: 5px;
}
.viva-qa p {
color: #333;
margin: 0;
line-height: 1.6;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1><a href="https://zenyukti.in">🎓 ZenYukti</a></h1>
<p>Data Structures: Queues, Searching & Sorting</p>
</div>
<div class="content">
<!-- QUEUES SECTION -->
<div class="section">
<h2>📋 QUEUES</h2>
<div class="definition">
<strong>Definition:</strong> A Queue is a linear data structure following FIFO (First In First Out) principle. Elements are added at the rear and removed from the front.
</div>
<h3>Queue Operations</h3>
<div class="key-points">
<ul>
<li><strong>Create:</strong> Initialize an empty queue</li>
<li><strong>Enqueue (Add):</strong> Insert element at rear</li>
<li><strong>Dequeue (Delete):</strong> Remove element from front</li>
<li><strong>IsFull:</strong> Check if queue is full</li>
<li><strong>IsEmpty:</strong> Check if queue is empty</li>
<li><strong>Peek/Front:</strong> View front element without removing</li>
</ul>
</div>
<h3>Array Implementation of Queue</h3>
<div class="code-block"><code>#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int queue[MAX];
int front = -1, rear = -1;
int isFull() {
return rear == MAX - 1;
}
int isEmpty() {
return front == -1 || front > rear;
}
void enqueue(int value) {
if (isFull()) {
printf("Queue Overflow!\n");
return;
}
if (front == -1) front = 0;
queue[++rear] = value;
printf("Inserted %d\n", value);
}
int dequeue() {
if (isEmpty()) {
printf("Queue Underflow!\n");
return -1;
}
return queue[front++];
}
void display() {
if (isEmpty()) {
printf("Queue is empty\n");
return;
}
printf("Queue: ");
for (int i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}</code></div>
<h3>Circular Queue</h3>
<div class="definition">
<strong>Circular Queue:</strong> A queue where the last position connects back to the first, utilizing memory efficiently by reusing vacant spaces.
</div>
<div class="code-block"><code>#define MAX 5
int cqueue[MAX];
int front = -1, rear = -1;
int isFull() {
return (rear + 1) % MAX == front;
}
int isEmpty() {
return front == -1;
}
void enqueue(int value) {
if (isFull()) {
printf("Queue is Full!\n");
return;
}
if (front == -1) front = 0;
rear = (rear + 1) % MAX;
cqueue[rear] = value;
}
int dequeue() {
if (isEmpty()) {
printf("Queue is Empty!\n");
return -1;
}
int val = cqueue[front];
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
return val;
}</code></div>
<h3>Linked List Implementation</h3>
<div class="code-block"><code>struct Node {
int data;
struct Node* next;
};
struct Node *front = NULL, *rear = NULL;
void enqueue(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
return;
}
rear->next = newNode;
rear = newNode;
}
int dequeue() {
if (front == NULL) {
printf("Queue is Empty!\n");
return -1;
}
struct Node* temp = front;
int val = temp->data;
front = front->next;
if (front == NULL) rear = NULL;
free(temp);
return val;
}</code></div>
<h3>Dequeue (Double-Ended Queue)</h3>
<div class="definition">
<strong>Dequeue:</strong> Elements can be inserted or deleted from both front and rear ends.
<br><strong>Types:</strong> Input-restricted (insertion only at rear), Output-restricted (deletion only at front)
</div>
<h3>Priority Queue</h3>
<div class="definition">
<strong>Priority Queue:</strong> Each element has a priority. Higher priority elements are dequeued before lower priority ones.
<br><strong>Types:</strong> Ascending (min priority first), Descending (max priority first)
</div>
<div class="code-block"><code>struct PQNode {
int data;
int priority;
};
void enqueue(struct PQNode pq[], int *n, int value, int priority) {
pq[*n].data = value;
pq[*n].priority = priority;
(*n)++;
}
int dequeue(struct PQNode pq[], int *n) {
int highestPriority = 0;
for (int i = 1; i < *n; i++) {
if (pq[i].priority > pq[highestPriority].priority)
highestPriority = i;
}
int val = pq[highestPriority].data;
// Shift elements
for (int i = highestPriority; i < *n - 1; i++) {
pq[i] = pq[i + 1];
}
(*n)--;
return val;
}</code></div>
</div>
<!-- SEARCHING SECTION -->
<div class="section">
<h2>🔍 SEARCHING</h2>
<h3>1. Sequential (Linear) Search</h3>
<div class="definition">
Search element by traversing array from start to end.
<br><span class="complexity">Time: O(n)</span> <span class="complexity">Space: O(1)</span>
</div>
<div class="code-block"><code>int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key)
return i; // Return index
}
return -1; // Not found
}</code></div>
<h3>2. Binary Search</h3>
<div class="definition">
Efficient search on <strong>sorted arrays</strong>. Divides search space in half each iteration.
<br><span class="complexity">Time: O(log n)</span> <span class="complexity">Space: O(1)</span>
</div>
<div class="code-block"><code>int binarySearch(int arr[], int n, int key) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
// Recursive Version
int binarySearchRecursive(int arr[], int low, int high, int key) {
if (low > high) return -1;
int mid = low + (high - low) / 2;
if (arr[mid] == key) return mid;
if (arr[mid] > key)
return binarySearchRecursive(arr, low, mid - 1, key);
return binarySearchRecursive(arr, mid + 1, high, key);
}</code></div>
<h3>3. Index Sequential Search</h3>
<div class="definition">
Combines indexing with sequential search. Creates an index table for blocks, then searches within the block.
<br><strong>Process:</strong> Search index → Identify block → Sequential search in block
</div>
<h3>Hashing</h3>
<div class="definition">
<strong>Hashing:</strong> Technique to map data to fixed-size values using a hash function. Enables O(1) average-case search.
<br><strong>Hash Function:</strong> h(key) = key % tableSize
</div>
<div class="key-points">
<strong>Example:</strong> Hash Table Size = 10
<ul>
<li>Insert 25: h(25) = 25 % 10 = 5 → Store at index 5</li>
<li>Insert 42: h(42) = 42 % 10 = 2 → Store at index 2</li>
<li>Insert 35: h(35) = 35 % 10 = 5 → <strong>Collision!</strong> (index 5 occupied)</li>
</ul>
</div>
<h3>Collision Resolution Techniques</h3>
<table>
<tr>
<th>Technique</th>
<th>Description</th>
</tr>
<tr>
<td><strong>Chaining</strong></td>
<td>Each slot contains a linked list of colliding elements</td>
</tr>
<tr>
<td><strong>Linear Probing</strong></td>
<td>h'(key) = (h(key) + i) % size, probe sequentially</td>
</tr>
<tr>
<td><strong>Quadratic Probing</strong></td>
<td>h'(key) = (h(key) + i²) % size</td>
</tr>
<tr>
<td><strong>Double Hashing</strong></td>
<td>h'(key) = (h1(key) + i * h2(key)) % size</td>
</tr>
</table>
<h3>1. Chaining Example</h3>
<div class="definition">
<strong>Scenario:</strong> Insert 15, 25, 35, 45 with table size = 10
<br><br>
<strong>Hash Values:</strong>
<br>• 15 % 10 = 5 → Index 5
<br>• 25 % 10 = 5 → Index 5 (Collision! Add to chain)
<br>• 35 % 10 = 5 → Index 5 (Collision! Add to chain)
<br>• 45 % 10 = 5 → Index 5 (Collision! Add to chain)
<br><br>
<strong>Result:</strong> Index 5 → [15] → [25] → [35] → [45] → NULL
</div>
<div class="code-block"><code>#define SIZE 10
struct Node {
int key;
struct Node* next;
};
struct Node* hashTable[SIZE] = {NULL};
void insertChaining(int key) {
int index = key % SIZE;
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->key = key;
newNode->next = hashTable[index];
hashTable[index] = newNode;
}
int searchChaining(int key) {
int index = key % SIZE;
struct Node* temp = hashTable[index];
while (temp != NULL) {
if (temp->key == key) return 1;
temp = temp->next;
}
return 0;
}</code></div>
<h3>2. Linear Probing Example</h3>
<div class="definition">
<strong>Scenario:</strong> Insert 15, 25, 35, 45 with table size = 10
<br><br>
<strong>Insertion Process:</strong>
<br>• 15: h(15) = 5 → Store at index 5
<br>• 25: h(25) = 5 (occupied) → Try (5+1)%10 = 6 → Store at index 6
<br>• 35: h(35) = 5 (occupied) → Try 6 (occupied) → Try 7 → Store at index 7
<br>• 45: h(45) = 5 (occupied) → Try 6, 7 (occupied) → Try 8 → Store at index 8
<br><br>
<strong>Table:</strong> [-, -, -, -, -, 15, 25, 35, 45, -]
</div>
<div class="code-block"><code>int hashTable[SIZE];
int isOccupied[SIZE] = {0};
void insertLinearProbing(int key) {
int index = key % SIZE;
int i = 0;
while (isOccupied[(index + i) % SIZE]) {
i++;
if (i == SIZE) {
printf("Hash table is full!\n");
return;
}
}
int finalIndex = (index + i) % SIZE;
hashTable[finalIndex] = key;
isOccupied[finalIndex] = 1;
}</code></div>
<h3>3. Quadratic Probing Example</h3>
<div class="definition">
<strong>Scenario:</strong> Insert 15, 25, 35 with table size = 10
<br><br>
<strong>Insertion Process:</strong>
<br>• 15: h(15) = 5 → Store at index 5
<br>• 25: h(25) = 5 (occupied) → Try (5+1²)%10 = 6 → Store at index 6
<br>• 35: h(35) = 5 (occupied) → Try (5+1²)%10 = 6 (occupied) → Try (5+2²)%10 = 9 → Store at 9
<br><br>
<strong>Formula:</strong> h'(key) = (h(key) + i²) % size
</div>
<h3>4. Double Hashing Example</h3>
<div class="definition">
<strong>Hash Functions:</strong>
<br>• h1(key) = key % 10
<br>• h2(key) = 7 - (key % 7)
<br><br>
<strong>Scenario:</strong> Insert 15, 25 with table size = 10
<br><br>
<strong>Insertion Process:</strong>
<br>• 15: h1(15) = 5 → Store at index 5
<br>• 25: h1(25) = 5 (occupied)
<br> - h2(25) = 7 - (25 % 7) = 7 - 4 = 3
<br> - Try (5 + 1×3) % 10 = 8 → Store at index 8
</div>
</div>
<!-- SORTING SECTION -->
<div class="section">
<h2>📊 SORTING</h2>
<h3>1. Bubble Sort</h3>
<div class="definition">
Repeatedly swaps adjacent elements if in wrong order. Largest element "bubbles up" to end.
<br><span class="complexity">Time: O(n²)</span> <span class="complexity">Space: O(1)</span>
</div>
<div class="code-block"><code>void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}</code></div>
<h3>2. Selection Sort</h3>
<div class="definition">
Selects minimum element and places it at beginning. Repeat for remaining array.
<br><span class="complexity">Time: O(n²)</span> <span class="complexity">Space: O(1)</span>
</div>
<div class="code-block"><code>void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx])
minIdx = j;
}
int temp = arr[minIdx];
arr[minIdx] = arr[i];
arr[i] = temp;
}
}</code></div>
<h3>3. Insertion Sort</h3>
<div class="definition">
Builds sorted array one element at a time. Insert each element into correct position.
<br><span class="complexity">Time: O(n²)</span> <span class="complexity">Space: O(1)</span>
</div>
<div class="code-block"><code>void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}</code></div>
<h3>4. Quick Sort</h3>
<div class="definition">
Divide-and-conquer algorithm. Pick pivot, partition array, recursively sort subarrays.
<br><span class="complexity">Time: O(n log n) average</span> <span class="complexity">Space: O(log n)</span>
</div>
<div class="code-block"><code>int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}</code></div>
<h3>5. Merge Sort</h3>
<div class="definition">
Divide array into halves, sort them, then merge. Stable sorting algorithm.
<br><span class="complexity">Time: O(n log n)</span> <span class="complexity">Space: O(n)</span>
</div>
<div class="code-block"><code>void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) L[i] = arr[l + i];
for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j])
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}</code></div>
<h3>6. Heap Sort</h3>
<div class="definition">
Uses binary heap data structure. Build max heap, then extract elements.
<br><span class="complexity">Time: O(n log n)</span> <span class="complexity">Space: O(1)</span>
</div>
<div class="code-block"><code>void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}</code></div>
<h3>7. Radix Sort</h3>
<div class="definition">
Non-comparative sorting. Sorts digit by digit starting from least significant.
<br><span class="complexity">Time: O(d × n)</span> where d = number of digits
</div>
<div class="code-block"><code>int getMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max) max = arr[i];
return max;
}
void countSort(int arr[], int n, int exp) {
int output[n];
int count[10] = {0};
for (int i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for (int i = 0; i < n; i++)
arr[i] = output[i];
}
void radixSort(int arr[], int n) {
int max = getMax(arr, n);
for (int exp = 1; max / exp > 0; exp *= 10)
countSort(arr, n, exp);
}</code></div>
<h3>Sorting Comparison Table</h3>
<table>
<tr>
<th>Algorithm</th>
<th>Best Case</th>
<th>Average Case</th>
<th>Worst Case</th>
<th>Space</th>
<th>Stable</th>
</tr>
<tr>
<td>Bubble Sort</td>
<td>O(n)</td>
<td>O(n²)</td>
<td>O(n²)</td>
<td>O(1)</td>
<td>Yes</td>
</tr>
<tr>
<td>Selection Sort</td>
<td>O(n²)</td>
<td>O(n²)</td>
<td>O(n²)</td>
<td>O(1)</td>
<td>No</td>
</tr>
<tr>
<td>Insertion Sort</td>
<td>O(n)</td>
<td>O(n²)</td>
<td>O(n²)</td>
<td>O(1)</td>
<td>Yes</td>
</tr>
<tr>
<td>Quick Sort</td>
<td>O(n log n)</td>
<td>O(n log n)</td>
<td>O(n²)</td>
<td>O(log n)</td>
<td>No</td>
</tr>
<tr>
<td>Merge Sort</td>
<td>O(n log n)</td>
<td>O(n log n)</td>
<td>O(n log n)</td>
<td>O(n)</td>
<td>Yes</td>
</tr>
<tr>
<td>Heap Sort</td>
<td>O(n log n)</td>
<td>O(n log n)</td>
<td>O(n log n)</td>
<td>O(1)</td>
<td>No</td>
</tr>
<tr>
<td>Radix Sort</td>
<td>O(d × n)</td>
<td>O(d × n)</td>
<td>O(d × n)</td>
<td>O(n + k)</td>
<td>Yes</td>
</tr>
</table>
</div>
<!-- TOWER OF HANOI SECTION -->
<div class="section">
<h2>🗼 TOWER OF HANOI</h2>
<div class="definition">
<strong>Problem:</strong> Move n disks from source rod to destination rod using auxiliary rod.
<br><strong>Rules:</strong> Only one disk at a time, larger disk cannot be on smaller disk
<br><strong>Recurrence:</strong> T(n) = 2T(n-1) + 1
<br><span class="complexity">Time: O(2ⁿ)</span> <span class="complexity">Moves: 2ⁿ - 1</span>
</div>
<div class="code-block"><code>void towerOfHanoi(int n, char source, char dest, char aux) {
if (n == 1) {
printf("Move disk 1 from %c to %c\n", source, dest);
return;
}
towerOfHanoi(n - 1, source, aux, dest);
printf("Move disk %d from %c to %c\n", n, source, dest);
towerOfHanoi(n - 1, aux, dest, source);
}</code></div>
<h3>Dry Run for n = 3</h3>
<div class="key-points">
<strong>Objective:</strong> Move 3 disks from Rod A to Rod C using Rod B
<br><strong>Initial Configuration:</strong>
<pre style="background: white; padding: 10px; border-radius: 5px; margin: 10px 0;">
Rod A Rod B Rod C
-----
---
-
------- ------- -------
A B C
</pre>
<strong>Step-by-Step Execution:</strong>
<ul>
<li><strong>Move 1:</strong> Disk 1: A → C
<pre style="background: white; padding: 5px; border-radius: 5px; margin: 5px 0;">
----- -
---
------- ------- -------</pre>
</li>
<li><strong>Move 2:</strong> Disk 2: A → B
<pre style="background: white; padding: 5px; border-radius: 5px; margin: 5px 0;">
----- --- -
------- ------- -------</pre>
</li>
<li><strong>Move 3:</strong> Disk 1: C → B
<pre style="background: white; padding: 5px; border-radius: 5px; margin: 5px 0;">
----- -
---
------- ------- -------</pre>
</li>
<li><strong>Move 4:</strong> Disk 3: A → C
<pre style="background: white; padding: 5px; border-radius: 5px; margin: 5px 0;">
- -----
---
------- ------- -------</pre>
</li>
<li><strong>Move 5:</strong> Disk 1: B → A
<pre style="background: white; padding: 5px; border-radius: 5px; margin: 5px 0;">
- -----
---
------- ------- -------</pre>
</li>
<li><strong>Move 6:</strong> Disk 2: B → C
<pre style="background: white; padding: 5px; border-radius: 5px; margin: 5px 0;">
- - -----
---
------- ------- -------</pre>
</li>
<li><strong>Move 7:</strong> Disk 1: A → C (COMPLETE!)
<pre style="background: white; padding: 5px; border-radius: 5px; margin: 5px 0;">
-
- -----
---
------- ------- -------</pre>
</li>
</ul>
<strong>✅ Total Moves Required: 2³ - 1 = 7 moves</strong>
</div>
<div class="definition">
<strong>Recursive Call Tree Visualization:</strong>
<pre style="background: white; padding: 15px; border-radius: 5px; margin: 10px 0; font-size: 0.95em; color: #333;">
TOH(3, A→C, aux=B) [Move 3 disks from A to C]
│
├─── TOH(2, A→B, aux=C) [Move 2 smaller disks to B]
│ │
│ ├─── TOH(1, A→C, aux=B)
│ │ └─── <strong style="color: #e53e3e;">MOVE disk 1: A → C</strong>
│ │
│ ├─── <strong style="color: #e53e3e;">MOVE disk 2: A → B</strong>
│ │
│ └─── TOH(1, C→B, aux=A)
│ └─── <strong style="color: #e53e3e;">MOVE disk 1: C → B</strong>
│
├─── <strong style="color: #e53e3e;">MOVE disk 3: A → C</strong> [Move largest disk]
│
└─── TOH(2, B→C, aux=A) [Move 2 disks from B to C]
│
├─── TOH(1, B→A, aux=C)
│ └─── <strong style="color: #e53e3e;">MOVE disk 1: B → A</strong>
│
├─── <strong style="color: #e53e3e;">MOVE disk 2: B → C</strong>
│
└─── TOH(1, A→C, aux=B)
└─── <strong style="color: #e53e3e;">MOVE disk 1: A → C</strong>
</pre>
<strong>Key Insight:</strong> To move n disks:
<br>1. Move (n-1) disks to auxiliary rod
<br>2. Move largest disk to destination
<br>3. Move (n-1) disks from auxiliary to destination
</div>
</div>
<!-- VIVA QUESTIONS SECTION -->
<div class="viva-section">
<h2>🎤 VIVA VOICE QUESTIONS & ANSWERS</h2>
<div class="viva-qa">
<strong>Q1. What is the main difference between Stack and Queue?</strong>
<p>Stack follows LIFO (Last In First Out) principle where insertion and deletion happen at the same end, while Queue follows FIFO (First In First Out) where insertion at rear and deletion at front.</p>
</div>
<div class="viva-qa">
<strong>Q2. Why do we need Circular Queue?</strong>
<p>In linear queue, even after dequeue operations, front elements' space cannot be reused, leading to overflow. Circular queue reuses empty spaces by connecting rear to front, utilizing memory efficiently.</p>
</div>
<div class="viva-qa">
<strong>Q3. What is the advantage of linked list implementation over array implementation of queue?</strong>
<p>Linked list implementation has dynamic size with no overflow issue, while array implementation has fixed size. However, array implementation has better cache locality and less memory overhead.</p>
</div>
<div class="viva-qa">
<strong>Q4. Explain Priority Queue with real-life example.</strong>
<p>Priority Queue processes elements based on priority, not insertion order. Real-life example: Hospital emergency room where critical patients are treated first regardless of arrival time.</p>
</div>
<div class="viva-qa">