Space and Time Complexity

Space complexity refers to the amount of memory used by an algorithm to complete its execution, as a function of the size of the input. The space complexity of an algorithm can be affected by various factors such as the size of the input data, the data structures used in the algorithm, the number and size of temporary variables, and the recursion depth. Time complexity refers to the amount of time required by an algorithm to run as the input size grows. It is usually measured in terms of the "Big O" notation, which describes the upper bound of an algorithm's time complexity.

Why do you think a programmer should care about space and time complexity?

A programmer should care about space and time complexity beacuse of these following reasons.

  1. Efficiency:Space and time complexity help analyze the efficiency of an algorithm. An efficient algorithm can save computational resources, reducing the time and memory required to process data. This is particularly important when working with large datasets or complex operations.
    1. An algorithm with lower space and time complexity is more likely to scale well with increasing data size or complexity. This is crucial for applications that need to handle significant amounts of data or adapt to future growth.
  1. Responsiveness: A faster algorithm can provide better user experiences, especially in interactive applications or real-time systems where responsiveness is critical.

Take a look at our lassen volcano example from the data compression tech talk. The first code block is the original image. In the second code block, change the baseWidth to rescale the image.

from IPython.display import Image, display
from pathlib import Path 

# prepares a series of images
def image_data(path=Path("images/"), images=None):  # path of static images is defaulted
    for image in images:
        # File to open
        image['filename'] = path / image['file']  # file with path
    return images

def image_display(images):
    for image in images:  
        display(Image(filename=image['filename']))

if __name__ == "__main__":
    lassen_volcano = image_data(images=[{'source': "Peter Carolin", 'label': "Lassen Volcano", 'file': "lassen-volcano.jpg"}])
    image_display(lassen_volcano)
    
from IPython.display import HTML, display
from pathlib import Path 
from PIL import Image as pilImage 
from io import BytesIO
import base64

# prepares a series of images
def image_data(path=Path("images/"), images=None):  # path of static images is defaulted
    for image in images:
        # File to open
        image['filename'] = path / image['file']  # file with path
    return images

def scale_image(img):
    #baseWidth = 625
    #baseWidth = 1250
    #baseWidth = 2500
    baseWidth = 5000 # see the effect of doubling or halfing the baseWidth 
    #baseWidth = 10000 
    #baseWidth = 20000
    #baseWidth = 40000
    scalePercent = (baseWidth/float(img.size[0]))
    scaleHeight = int((float(img.size[1])*float(scalePercent)))
    scale = (baseWidth, scaleHeight)
    return img.resize(scale)

def image_to_base64(img, format):
    with BytesIO() as buffer:
        img.save(buffer, format)
        return base64.b64encode(buffer.getvalue()).decode()
    
def image_management(image):  # path of static images is defaulted        
    # Image open return PIL image object
    img = pilImage.open(image['filename'])
    
    # Python Image Library operations
    image['format'] = img.format
    image['mode'] = img.mode
    image['size'] = img.size
    image['width'], image['height'] = img.size
    image['pixels'] = image['width'] * image['height']
    # Scale the Image
    img = scale_image(img)
    image['pil'] = img
    image['scaled_size'] = img.size
    image['scaled_width'], image['scaled_height'] = img.size
    image['scaled_pixels'] = image['scaled_width'] * image['scaled_height']
    # Scaled HTML
    image['html'] = '<img src="data:image/png;base64,%s">' % image_to_base64(image['pil'], image['format'])


if __name__ == "__main__":
    # Use numpy to concatenate two arrays
    images = image_data(images = [{'source': "Peter Carolin", 'label': "Lassen Volcano", 'file': "lassen-volcano.jpg"}])
    
    # Display meta data, scaled view, and grey scale for each image
    for image in images:
        image_management(image)
        print("---- meta data -----")
        print(image['label'])
        print(image['source'])
        print(image['format'])
        print(image['mode'])
        print("Original size: ", image['size'], " pixels: ", f"{image['pixels']:,}")
        print("Scaled size: ", image['scaled_size'], " pixels: ", f"{image['scaled_pixels']:,}")
        
        print("-- original image --")
        display(HTML(image['html'])) 
---- meta data -----
Lassen Volcano
Peter Carolin
JPEG
RGB
Original size:  (800, 533)  pixels:  426,400
Scaled size:  (5000, 3331)  pixels:  16,655,000
-- original image --

Do you think this is a time complexity or space complexity or both problem?

I think that this code primarily deals with both time and space complexity and this is because

  • The code involves various operations such as opening and resizing images, saving them to buffers, and encoding them in base64 format. These operations contribute to the time complexity of the code. Additionally, the loop that iterates through the list of images (in the main block) adds to the time complexity since the number of iterations will depend on the number of images in the list.

  • It also deals ith Space complexity because the code stores multiple copies of the image (original and scaled) and their metadata in dictionaries, which contribute to the space complexity. The size of these data structures will depend on the number of images being processed and the size of the images themselves. Moreover, the BytesIO buffer used to save the image temporarily also contributes to the space complexity.

Big O Notation

  • Constant O(1)
  • Linear O(n)
  • Quadratic O(n^2)
  • Logarithmic O(logn)
  • Exponential (O(2^n))
numbers = list(range(1000))
print(numbers)
[0, 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]

Constant O(1)

Time

An example of a constant time algorithm is accessing a specific element in an array. It does not matter how large the array is, accessing an element in the array takes the same amount of time. Therefore, the time complexity of this operation is constant, denoted by O(1).

print(numbers[263])

ncaa_bb_ranks = {1:"Alabama",2:"Houston", 3:"Purdue", 4:"Kansas"}
#look up a value in a dictionary given a key
print(ncaa_bb_ranks[1]) 
263
Alabama

Space

This function takes two number inputs and returns their sum. The function does not create any additional data structures or variables that are dependent on the input size, so its space complexity is constant, or O(1). Regardless of how large the input numbers are, the function will always require the same amount of memory to execute.

def sum(a, b): 
  return a + b

print(sum(90,88))
print(sum(.9,.88))
178
1.78

Linear O(n)

Time

An example of a linear time algorithm is traversing a list or an array. When the size of the list or array increases, the time taken to traverse it also increases linearly with the size. Hence, the time complexity of this operation is O(n), where n is the size of the list or array being traversed.

for i in numbers:
    print(i)
0
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

Space

This function takes a list of elements arr as input and returns a new list with the elements in reverse order. The function creates a new list reversed_arr of the same size as arr to store the reversed elements. The size of reversed_arr depends on the size of the input arr, so the space complexity of this function is O(n). As the input size increases, the amount of memory required to execute the function also increases linearly.

def reverse_list(arr):
    n = len(arr) 
    reversed_arr = [None] * n #create a list of None based on the length or arr
    for i in range(n):
        reversed_arr[n-i-1] = arr[i] #stores the value at the index of arr to the value at the index of reversed_arr starting at the beginning for arr and end for reversed_arr 
    return reversed_arr

print(numbers)
print(reverse_list(numbers))
[0, 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]
[999, 998, 997, 996, 995, 994, 993, 992, 991, 990, 989, 988, 987, 986, 985, 984, 983, 982, 981, 980, 979, 978, 977, 976, 975, 974, 973, 972, 971, 970, 969, 968, 967, 966, 965, 964, 963, 962, 961, 960, 959, 958, 957, 956, 955, 954, 953, 952, 951, 950, 949, 948, 947, 946, 945, 944, 943, 942, 941, 940, 939, 938, 937, 936, 935, 934, 933, 932, 931, 930, 929, 928, 927, 926, 925, 924, 923, 922, 921, 920, 919, 918, 917, 916, 915, 914, 913, 912, 911, 910, 909, 908, 907, 906, 905, 904, 903, 902, 901, 900, 899, 898, 897, 896, 895, 894, 893, 892, 891, 890, 889, 888, 887, 886, 885, 884, 883, 882, 881, 880, 879, 878, 877, 876, 875, 874, 873, 872, 871, 870, 869, 868, 867, 866, 865, 864, 863, 862, 861, 860, 859, 858, 857, 856, 855, 854, 853, 852, 851, 850, 849, 848, 847, 846, 845, 844, 843, 842, 841, 840, 839, 838, 837, 836, 835, 834, 833, 832, 831, 830, 829, 828, 827, 826, 825, 824, 823, 822, 821, 820, 819, 818, 817, 816, 815, 814, 813, 812, 811, 810, 809, 808, 807, 806, 805, 804, 803, 802, 801, 800, 799, 798, 797, 796, 795, 794, 793, 792, 791, 790, 789, 788, 787, 786, 785, 784, 783, 782, 781, 780, 779, 778, 777, 776, 775, 774, 773, 772, 771, 770, 769, 768, 767, 766, 765, 764, 763, 762, 761, 760, 759, 758, 757, 756, 755, 754, 753, 752, 751, 750, 749, 748, 747, 746, 745, 744, 743, 742, 741, 740, 739, 738, 737, 736, 735, 734, 733, 732, 731, 730, 729, 728, 727, 726, 725, 724, 723, 722, 721, 720, 719, 718, 717, 716, 715, 714, 713, 712, 711, 710, 709, 708, 707, 706, 705, 704, 703, 702, 701, 700, 699, 698, 697, 696, 695, 694, 693, 692, 691, 690, 689, 688, 687, 686, 685, 684, 683, 682, 681, 680, 679, 678, 677, 676, 675, 674, 673, 672, 671, 670, 669, 668, 667, 666, 665, 664, 663, 662, 661, 660, 659, 658, 657, 656, 655, 654, 653, 652, 651, 650, 649, 648, 647, 646, 645, 644, 643, 642, 641, 640, 639, 638, 637, 636, 635, 634, 633, 632, 631, 630, 629, 628, 627, 626, 625, 624, 623, 622, 621, 620, 619, 618, 617, 616, 615, 614, 613, 612, 611, 610, 609, 608, 607, 606, 605, 604, 603, 602, 601, 600, 599, 598, 597, 596, 595, 594, 593, 592, 591, 590, 589, 588, 587, 586, 585, 584, 583, 582, 581, 580, 579, 578, 577, 576, 575, 574, 573, 572, 571, 570, 569, 568, 567, 566, 565, 564, 563, 562, 561, 560, 559, 558, 557, 556, 555, 554, 553, 552, 551, 550, 549, 548, 547, 546, 545, 544, 543, 542, 541, 540, 539, 538, 537, 536, 535, 534, 533, 532, 531, 530, 529, 528, 527, 526, 525, 524, 523, 522, 521, 520, 519, 518, 517, 516, 515, 514, 513, 512, 511, 510, 509, 508, 507, 506, 505, 504, 503, 502, 501, 500, 499, 498, 497, 496, 495, 494, 493, 492, 491, 490, 489, 488, 487, 486, 485, 484, 483, 482, 481, 480, 479, 478, 477, 476, 475, 474, 473, 472, 471, 470, 469, 468, 467, 466, 465, 464, 463, 462, 461, 460, 459, 458, 457, 456, 455, 454, 453, 452, 451, 450, 449, 448, 447, 446, 445, 444, 443, 442, 441, 440, 439, 438, 437, 436, 435, 434, 433, 432, 431, 430, 429, 428, 427, 426, 425, 424, 423, 422, 421, 420, 419, 418, 417, 416, 415, 414, 413, 412, 411, 410, 409, 408, 407, 406, 405, 404, 403, 402, 401, 400, 399, 398, 397, 396, 395, 394, 393, 392, 391, 390, 389, 388, 387, 386, 385, 384, 383, 382, 381, 380, 379, 378, 377, 376, 375, 374, 373, 372, 371, 370, 369, 368, 367, 366, 365, 364, 363, 362, 361, 360, 359, 358, 357, 356, 355, 354, 353, 352, 351, 350, 349, 348, 347, 346, 345, 344, 343, 342, 341, 340, 339, 338, 337, 336, 335, 334, 333, 332, 331, 330, 329, 328, 327, 326, 325, 324, 323, 322, 321, 320, 319, 318, 317, 316, 315, 314, 313, 312, 311, 310, 309, 308, 307, 306, 305, 304, 303, 302, 301, 300, 299, 298, 297, 296, 295, 294, 293, 292, 291, 290, 289, 288, 287, 286, 285, 284, 283, 282, 281, 280, 279, 278, 277, 276, 275, 274, 273, 272, 271, 270, 269, 268, 267, 266, 265, 264, 263, 262, 261, 260, 259, 258, 257, 256, 255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 237, 236, 235, 234, 233, 232, 231, 230, 229, 228, 227, 226, 225, 224, 223, 222, 221, 220, 219, 218, 217, 216, 215, 214, 213, 212, 211, 210, 209, 208, 207, 206, 205, 204, 203, 202, 201, 200, 199, 198, 197, 196, 195, 194, 193, 192, 191, 190, 189, 188, 187, 186, 185, 184, 183, 182, 181, 180, 179, 178, 177, 176, 175, 174, 173, 172, 171, 170, 169, 168, 167, 166, 165, 164, 163, 162, 161, 160, 159, 158, 157, 156, 155, 154, 153, 152, 151, 150, 149, 148, 147, 146, 145, 144, 143, 142, 141, 140, 139, 138, 137, 136, 135, 134, 133, 132, 131, 130, 129, 128, 127, 126, 125, 124, 123, 122, 121, 120, 119, 118, 117, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Quadratic O(n^2)

Time

An example of a quadratic time algorithm is nested loops. When there are two nested loops that both iterate over the same collection, the time taken to complete the algorithm grows quadratically with the size of the collection. Hence, the time complexity of this operation is O(n^2), where n is the size of the collection being iterated over.

for i in numbers:
    for j in numbers:
        print(i,j)
0 0
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
0 10
0 11
0 12
0 13
0 14
0 15
0 16
0 17
0 18
0 19
0 20
0 21
0 22
0 23
0 24
0 25
0 26
0 27
0 28
0 29
0 30
0 31
0 32
0 33
0 34
0 35
0 36
0 37
0 38
0 39
0 40
0 41
0 42
0 43
0 44
0 45
0 46
0 47
0 48
0 49
0 50
0 51
0 52
0 53
0 54
0 55
0 56
0 57
0 58
0 59
0 60
0 61
0 62
0 63
0 64
0 65
0 66
0 67
0 68
0 69
0 70
0 71
0 72
0 73
0 74
0 75
0 76
0 77
0 78
0 79
0 80
0 81
0 82
0 83
0 84
0 85
0 86
0 87
0 88
0 89
0 90
0 91
0 92
0 93
0 94
0 95
0 96
0 97
0 98
0 99
0 100
0 101
0 102
0 103
0 104
0 105
0 106
0 107
0 108
0 109
0 110
0 111
0 112
0 113
0 114
0 115
0 116
0 117
0 118
0 119
0 120
0 121
0 122
0 123
0 124
0 125
0 126
0 127
0 128
0 129
0 130
0 131
0 132
0 133
0 134
0 135
0 136
0 137
0 138
0 139
0 140
0 141
0 142
0 143
0 144
0 145
0 146
0 147
0 148
0 149
0 150
0 151
0 152
0 153
0 154
0 155
0 156
0 157
0 158
0 159
0 160
0 161
0 162
0 163
0 164
0 165
0 166
0 167
0 168
0 169
0 170
0 171
0 172
0 173
0 174
0 175
0 176
0 177
0 178
0 179
0 180
0 181
0 182
0 183
0 184
0 185
0 186
0 187
0 188
0 189
0 190
0 191
0 192
0 193
0 194
0 195
0 196
0 197
0 198
0 199
0 200
0 201
0 202
0 203
0 204
0 205
0 206
0 207
0 208
0 209
0 210
0 211
0 212
0 213
0 214
0 215
0 216
0 217
0 218
0 219
0 220
0 221
0 222
0 223
0 224
0 225
0 226
0 227
0 228
0 229
0 230
0 231
0 232
0 233
0 234
0 235
0 236
0 237
0 238
0 239
0 240
0 241
0 242
0 243
0 244
0 245
0 246
0 247
0 248
0 249
0 250
0 251
0 252
0 253
0 254
0 255
0 256
0 257
0 258
0 259
0 260
0 261
0 262
0 263
0 264
0 265
0 266
0 267
0 268
0 269
0 270
0 271
0 272
0 273
0 274
0 275
0 276
0 277
0 278
0 279
0 280
0 281
0 282
0 283
0 284
0 285
0 286
0 287
0 288
0 289
0 290
0 291
0 292
0 293
0 294
0 295
0 296
0 297
0 298
0 299
0 300
0 301
0 302
0 303
0 304
0 305
0 306
0 307
0 308
0 309
0 310
0 311
0 312
0 313
0 314
0 315
0 316
0 317
0 318
0 319
0 320
0 321
0 322
0 323
0 324
0 325
0 326
0 327
0 328
0 329
0 330
0 331
0 332
0 333
0 334
0 335
0 336
0 337
0 338
0 339
0 340
0 341
0 342
0 343
0 344
0 345
0 346
0 347
0 348
0 349
0 350
0 351
0 352
0 353
0 354
0 355
0 356
0 357
0 358
0 359
0 360
0 361
0 362
0 363
0 364
0 365
0 366
0 367
0 368
0 369
0 370
0 371
0 372
0 373
0 374
0 375
0 376
0 377
0 378
0 379
0 380
0 381
0 382
0 383
0 384
0 385
0 386
0 387
0 388
0 389
0 390
0 391
0 392
0 393
0 394
0 395
0 396
0 397
0 398
0 399
0 400
0 401
0 402
0 403
0 404
0 405
0 406
0 407
0 408
0 409
0 410
0 411
0 412
0 413
0 414
0 415
0 416
0 417
0 418
0 419
0 420
0 421
0 422
0 423
0 424
0 425
0 426
0 427
0 428
0 429
0 430
0 431
0 432
0 433
0 434
0 435
0 436
0 437
0 438
0 439
0 440
0 441
0 442
0 443
0 444
0 445
0 446
0 447
0 448
0 449
0 450
0 451
0 452
0 453
0 454
0 455
0 456
0 457
0 458
0 459
0 460
0 461
0 462
0 463
0 464
0 465
0 466
0 467
0 468
0 469
0 470
0 471
0 472
0 473
0 474
0 475
0 476
0 477
0 478
0 479
0 480
0 481
0 482
0 483
0 484
0 485
0 486
0 487
0 488
0 489
0 490
0 491
0 492
0 493
0 494
0 495
0 496
0 497
0 498
0 499
0 500
0 501
0 502
0 503
0 504
0 505
0 506
0 507
0 508
0 509
0 510
0 511
0 512
0 513
0 514
0 515
0 516
0 517
0 518
0 519
0 520
0 521
0 522
0 523
0 524
0 525
0 526
0 527
0 528
0 529
0 530
0 531
0 532
0 533
0 534
0 535
0 536
0 537
0 538
0 539
0 540
0 541
0 542
0 543
0 544
0 545
0 546
0 547
0 548
0 549
0 550
0 551
0 552
0 553
0 554
0 555
0 556
0 557
0 558
0 559
0 560
0 561
0 562
0 563
0 564
0 565
0 566
0 567
0 568
0 569
0 570
0 571
0 572
0 573
0 574
0 575
0 576
0 577
0 578
0 579
0 580
0 581
0 582
0 583
0 584
0 585
0 586
0 587
0 588
0 589
0 590
0 591
0 592
0 593
0 594
0 595
0 596
0 597
0 598
0 599
0 600
0 601
0 602
0 603
0 604
0 605
0 606
0 607
0 608
0 609
0 610
0 611
0 612
0 613
0 614
0 615
0 616
0 617
0 618
0 619
0 620
0 621
0 622
0 623
0 624
0 625
0 626
0 627
0 628
0 629
0 630
0 631
0 632
0 633
0 634
0 635
0 636
0 637
0 638
0 639
0 640
0 641
0 642
0 643
0 644
0 645
0 646
0 647
0 648
0 649
0 650
0 651
0 652
0 653
0 654
0 655
0 656
0 657
0 658
0 659
0 660
0 661
0 662
0 663
0 664
0 665
0 666
0 667
0 668
0 669
0 670
0 671
0 672
0 673
0 674
0 675
0 676
0 677
0 678
0 679
0 680
0 681
0 682
0 683
0 684
0 685
0 686
0 687
0 688
0 689
0 690
0 691
0 692
0 693
0 694
0 695
0 696
0 697
0 698
0 699
0 700
0 701
0 702
0 703
0 704
0 705
0 706
0 707
0 708
0 709
0 710
0 711
0 712
0 713
0 714
0 715
0 716
0 717
0 718
0 719
0 720
0 721
0 722
0 723
0 724
0 725
0 726
0 727
0 728
0 729
0 730
0 731
0 732
0 733
0 734
0 735
0 736
0 737
0 738
0 739
0 740
0 741
0 742
0 743
0 744
0 745
0 746
0 747
0 748
0 749
0 750
0 751
0 752
0 753
0 754
0 755
0 756
0 757
0 758
0 759
0 760
0 761
0 762
0 763
0 764
0 765
0 766
0 767
0 768
0 769
0 770
0 771
0 772
0 773
0 774
0 775
0 776
0 777
0 778
0 779
0 780
0 781
0 782
0 783
0 784
0 785
0 786
0 787
0 788
0 789
0 790
0 791
0 792
0 793
0 794
0 795
0 796
0 797
0 798
0 799
0 800
0 801
0 802
0 803
0 804
0 805
0 806
0 807
0 808
0 809
0 810
0 811
0 812
0 813
0 814
0 815
0 816
0 817
0 818
0 819
0 820
0 821
0 822
0 823
0 824
0 825
0 826
0 827
0 828
0 829
0 830
0 831
0 832
0 833
0 834
0 835
0 836
0 837
0 838
0 839
0 840
0 841
0 842
0 843
0 844
0 845
0 846
0 847
0 848
0 849
0 850
0 851
0 852
0 853
0 854
0 855
0 856
0 857
0 858
0 859
0 860
0 861
0 862
0 863
0 864
0 865
0 866
0 867
0 868
0 869
0 870
0 871
0 872
0 873
0 874
0 875
0 876
0 877
0 878
0 879
0 880
0 881
0 882
0 883
0 884
0 885
0 886
0 887
0 888
0 889
0 890
0 891
0 892
0 893
0 894
0 895
0 896
0 897
0 898
0 899
0 900
0 901
0 902
0 903
0 904
0 905
0 906
0 907
0 908
0 909
0 910
0 911
0 912
0 913
0 914
0 915
0 916
0 917
0 918
0 919
0 920
0 921
0 922
0 923
0 924
0 925
0 926
0 927
0 928
0 929
0 930
0 931
0 932
0 933
0 934
0 935
0 936
0 937
0 938
0 939
0 940
0 941
0 942
0 943
0 944
0 945
0 946
0 947
0 948
0 949
0 950
0 951
0 952
0 953
0 954
0 955
0 956
0 957
0 958
0 959
0 960
0 961
0 962
0 963
0 964
0 965
0 966
0 967
0 968
0 969
0 970
0 971
0 972
0 973
0 974
0 975
0 976
0 977
0 978
0 979
0 980
0 981
0 982
0 983
0 984
0 985
0 986
0 987
0 988
0 989
0 990
0 991
0 992
0 993
0 994
0 995
0 996
0 997
0 998
0 999
1 0
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1 55
1 56
1 57
1 58
1 59
1 60
1 61
1 62
1 63
1 64
1 65
1 66
1 67
1 68
1 69
1 70
1 71
1 72
1 73
1 74
1 75
1 76
1 77
1 78
1 79
1 80
1 81
1 82
1 83
1 84
1 85
1 86
1 87
1 88
1 89
1 90
1 91
1 92
1 93
1 94
1 95
1 96
1 97
1 98
1 99
1 100
1 101
1 102
1 103
1 104
1 105
1 106
1 107
1 108
1 109
1 110
1 111
1 112
1 113
1 114
1 115
1 116
1 117
1 118
1 119
1 120
1 121
1 122
1 123
1 124
1 125
1 126
1 127
1 128
1 129
1 130
1 131
1 132
1 133
1 134
1 135
1 136
1 137
1 138
1 139
1 140
1 141
1 142
1 143
1 144
1 145
1 146
1 147
1 148
1 149
1 150
1 151
1 152
1 153
1 154
1 155
1 156
1 157
1 158
1 159
1 160
1 161
1 162
1 163
1 164
1 165
1 166
1 167
1 168
1 169
1 170
1 171
1 172
1 173
1 174
1 175
1 176
1 177
1 178
1 179
1 180
1 181
1 182
1 183
1 184
1 185
1 186
1 187
1 188
1 189
1 190
1 191
1 192
1 193
1 194
1 195
1 196
1 197
1 198
1 199
1 200
1 201
1 202
1 203
1 204
1 205
1 206
1 207
1 208
1 209
1 210
1 211
1 212
1 213
1 214
1 215
1 216
1 217
1 218
1 219
1 220
1 221
1 222
1 223
1 224
1 225
1 226
1 227
1 228
1 229
1 230
1 231
1 232
1 233
1 234
1 235
1 236
1 237
1 238
1 239
1 240
1 241
1 242
1 243
1 244
1 245
1 246
1 247
1 248
1 249
1 250
1 251
1 252
1 253
1 254
1 255
1 256
1 257
1 258
1 259
1 260
1 261
1 262
1 263
1 264
1 265
1 266
1 267
1 268
1 269
1 270
1 271
1 272
1 273
1 274
1 275
1 276
1 277
1 278
1 279
1 280
1 281
1 282
1 283
1 284
1 285
1 286
1 287
1 288
1 289
1 290
1 291
1 292
1 293
1 294
1 295
1 296
1 297
1 298
1 299
1 300
1 301
1 302
1 303
1 304
1 305
1 306
1 307
1 308
1 309
1 310
1 311
1 312
1 313
1 314
1 315
1 316
1 317
1 318
1 319
1 320
1 321
1 322
1 323
1 324
1 325
1 326
1 327
1 328
1 329
1 330
1 331
1 332
1 333
1 334
1 335
1 336
1 337
1 338
1 339
1 340
1 341
1 342
1 343
1 344
1 345
1 346
1 347
1 348
1 349
1 350
1 351
1 352
1 353
1 354
1 355
1 356
1 357
1 358
1 359
1 360
1 361
1 362
1 363
1 364
1 365
1 366
1 367
1 368
1 369
1 370
1 371
1 372
1 373
1 374
1 375
1 376
1 377
1 378
1 379
1 380
1 381
1 382
1 383
1 384
1 385
1 386
1 387
1 388
1 389
1 390
1 391
1 392
1 393
1 394
1 395
1 396
1 397
1 398
1 399
1 400
1 401
1 402
1 403
1 404
1 405
1 406
1 407
1 408
1 409
1 410
1 411
1 412
1 413
1 414
1 415
1 416
1 417
1 418
1 419
1 420
1 421
1 422
1 423
1 424
1 425
1 426
1 427
1 428
1 429
1 430
1 431
1 432
1 433
1 434
1 435
1 436
1 437
1 438
1 439
1 440
1 441
1 442
1 443
1 444
1 445
1 446
1 447
1 448
1 449
1 450
1 451
1 452
1 453
1 454
1 455
1 456
1 457
1 458
1 459
1 460
1 461
1 462
1 463
1 464
1 465
1 466
1 467
1 468
1 469
1 470
1 471
1 472
1 473
1 474
1 475
1 476
1 477
1 478
1 479
1 480
1 481
1 482
1 483
1 484
1 485
1 486
1 487
1 488
1 489
1 490
1 491
1 492
1 493
1 494
1 495
1 496
1 497
1 498
1 499
1 500
1 501
1 502
1 503
1 504
1 505
1 506
1 507
1 508
1 509
1 510
1 511
1 512
1 513
1 514
1 515
1 516
1 517
1 518
1 519
1 520
1 521
1 522
1 523
1 524
1 525
1 526
1 527
1 528
1 529
1 530
1 531
1 532
1 533
1 534
1 535
1 536
1 537
1 538
1 539
1 540
1 541
1 542
1 543
1 544
1 545
1 546
1 547
1 548
1 549
1 550
1 551
1 552
1 553
1 554
1 555
1 556
1 557
1 558
1 559
1 560
1 561
1 562
1 563
1 564
1 565
1 566
1 567
1 568
1 569
1 570
1 571
1 572
1 573
1 574
1 575
1 576
1 577
1 578
1 579
1 580
1 581
1 582
1 583
1 584
1 585
1 586
1 587
1 588
1 589
1 590
1 591
1 592
1 593
1 594
1 595
1 596
1 597
1 598
1 599
1 600
1 601
1 602
1 603
1 604
1 605
1 606
1 607
1 608
1 609
1 610
1 611
1 612
1 613
1 614
1 615
1 616
1 617
1 618
1 619
1 620
1 621
1 622
1 623
1 624
1 625
1 626
1 627
1 628
1 629
1 630
1 631
1 632
1 633
1 634
1 635
1 636
1 637
1 638
1 639
1 640
1 641
1 642
1 643
1 644
1 645
1 646
1 647
1 648
1 649
1 650
1 651
1 652
1 653
1 654
1 655
1 656
1 657
1 658
1 659
1 660
1 661
1 662
1 663
1 664
1 665
1 666
1 667
1 668
1 669
1 670
1 671
1 672
1 673
1 674
1 675
1 676
1 677
1 678
1 679
1 680
1 681
1 682
1 683
1 684
1 685
1 686
1 687
1 688
1 689
1 690
1 691
1 692
1 693
1 694
1 695
1 696
1 697
1 698
1 699
1 700
1 701
1 702
1 703
1 704
1 705
1 706
1 707
1 708
1 709
1 710
1 711
1 712
1 713
1 714
1 715
1 716
1 717
1 718
1 719
1 720
1 721
1 722
1 723
1 724
1 725
1 726
1 727
1 728
1 729
1 730
1 731
1 732
1 733
1 734
1 735
1 736
1 737
1 738
1 739
1 740
1 741
1 742
1 743
1 744
1 745
1 746
1 747
1 748
1 749
1 750
1 751
1 752
1 753
1 754
1 755
1 756
1 757
1 758
1 759
1 760
1 761
1 762
1 763
1 764
1 765
1 766
1 767
1 768
1 769
1 770
1 771
1 772
1 773
1 774
1 775
1 776
1 777
1 778
1 779
1 780
1 781
1 782
1 783
1 784
1 785
1 786
1 787
1 788
1 789
1 790
1 791
1 792
1 793
1 794
1 795
1 796
1 797
1 798
1 799
1 800
1 801
1 802
1 803
1 804
1 805
1 806
1 807
1 808
1 809
1 810
1 811
1 812
1 813
1 814
1 815
1 816
1 817
1 818
1 819
1 820
1 821
1 822
1 823
1 824
1 825
1 826
1 827
1 828
1 829
1 830
1 831
1 832
1 833
1 834
1 835
1 836
1 837
1 838
1 839
1 840
1 841
1 842
1 843
1 844
1 845
1 846
1 847
1 848
1 849
1 850
1 851
1 852
1 853
1 854
1 855
1 856
1 857
1 858
1 859
1 860
1 861
1 862
1 863
1 864
1 865
1 866
1 867
1 868
1 869
1 870
1 871
1 872
1 873
1 874
1 875
1 876
1 877
1 878
1 879
1 880
1 881
1 882
1 883
1 884
1 885
1 886
1 887
1 888
1 889
1 890
1 891
1 892
1 893
1 894
1 895
1 896
1 897
1 898
1 899
1 900
1 901
1 902
1 903
1 904
1 905
1 906
1 907
1 908
1 909
1 910
1 911
1 912
1 913
1 914
1 915
1 916
1 917
1 918
1 919
1 920
1 921
1 922
1 923
1 924
1 925
1 926
1 927
1 928
1 929
1 930
1 931
1 932
1 933
1 934
1 935
1 936
1 937
1 938
1 939
1 940
1 941
1 942
1 943
1 944
1 945
1 946
1 947
1 948
1 949
1 950
1 951
1 952
1 953
1 954
1 955
1 956
1 957
1 958
1 959
1 960
1 961
1 962
1 963
1 964
1 965
1 966
1 967
1 968
1 969
1 970
1 971
1 972
1 973
1 974
1 975
1 976
1 977
1 978
1 979
1 980
1 981
1 982
1 983
1 984
1 985
1 986
1 987
1 988
1 989
1 990
1 991
1 992
1 993
1 994
1 995
1 996
1 997
1 998
1 999
2 0
2 1
2 2
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
2 11
2 12
2 13
2 14
2 15
2 16
2 17
2 18
2 19
2 20
2 21
2 22
2 23
2 24
2 25
2 26
2 27
2 28
2 29
2 30
2 31
2 32
2 33
2 34
2 35
2 36
2 37
2 38
2 39
2 40
2 41
2 42
2 43
2 44
2 45
2 46
2 47
2 48
2 49
2 50
2 51
2 52
2 53
2 54
2 55
2 56
2 57
2 58
2 59
2 60
2 61
2 62
2 63
2 64
2 65
2 66
2 67
2 68
2 69
2 70
2 71
2 72
2 73
2 74
2 75
2 76
2 77
2 78
2 79
2 80
2 81
2 82
2 83
2 84
2 85
2 86
2 87
2 88
2 89
2 90
2 91
2 92
2 93
2 94
2 95
2 96
2 97
2 98
2 99
2 100
2 101
2 102
2 103
2 104
2 105
2 106
2 107
2 108
2 109
2 110
2 111
2 112
2 113
2 114
2 115
2 116
2 117
2 118
2 119
2 120
2 121
2 122
2 123
2 124
2 125
2 126
2 127
2 128
2 129
2 130
2 131
2 132
2 133
2 134
2 135
2 136
2 137
2 138
2 139
2 140
2 141
2 142
2 143
2 144
2 145
2 146
2 147
2 148
2 149
2 150
2 151
2 152
2 153
2 154
2 155
2 156
2 157
2 158
2 159
2 160
2 161
2 162
2 163
2 164
2 165
2 166
2 167
2 168
2 169
2 170
2 171
2 172
2 173
2 174
2 175
2 176
2 177
2 178
2 179
2 180
2 181
2 182
2 183
2 184
2 185
2 186
2 187
2 188
2 189
2 190
2 191
2 192
2 193
2 194
2 195
2 196
2 197
2 198
2 199
2 200
2 201
2 202
2 203
2 204
2 205
2 206
2 207
2 208
2 209
2 210
2 211
2 212
2 213
2 214
2 215
2 216
2 217
2 218
2 219
2 220
2 221
2 222
2 223
2 224
2 225
2 226
2 227
2 228
2 229
2 230
2 231
2 232
2 233
2 234
2 235
2 236
2 237
2 238
2 239
2 240
2 241
2 242
2 243
2 244
2 245
2 246
2 247
2 248
2 249
2 250
2 251
2 252
2 253
2 254
2 255
2 256
2 257
2 258
2 259
2 260
2 261
2 262
2 263
2 264
2 265
2 266
2 267
2 268
2 269
2 270
2 271
2 272
2 273
2 274
2 275
2 276
2 277
2 278
2 279
2 280
2 281
2 282
2 283
2 284
2 285
2 286
2 287
2 288
2 289
2 290
2 291
2 292
2 293
2 294
2 295
2 296
2 297
2 298
2 299
2 300
2 301
2 302
2 303
2 304
2 305
2 306
2 307
2 308
2 309
2 310
2 311
2 312
2 313
2 314
2 315
2 316
2 317
2 318
2 319
2 320
2 321
2 322
2 323
2 324
2 325
2 326
2 327
2 328
2 329
2 330
2 331
2 332
2 333
2 334
2 335
2 336
2 337
2 338
2 339
2 340
2 341
2 342
2 343
2 344
2 345
2 346
2 347
2 348
2 349
2 350
2 351
2 352
2 353
2 354
2 355
2 356
2 357
2 358
2 359
2 360
2 361
2 362
2 363
2 364
2 365
2 366
2 367
2 368
2 369
2 370
2 371
2 372
2 373
2 374
2 375
2 376
2 377
2 378
2 379
2 380
2 381
2 382
2 383
2 384
2 385
2 386
2 387
2 388
2 389
2 390
2 391
2 392
2 393
2 394
2 395
2 396
2 397
2 398
2 399
2 400
2 401
2 402
2 403
2 404
2 405
2 406
2 407
2 408
2 409
2 410
2 411
2 412
2 413
2 414
2 415
2 416
2 417
2 418
2 419
2 420
2 421
2 422
2 423
2 424
2 425
2 426
2 427
2 428
2 429
2 430
2 431
2 432
2 433
2 434
2 435
2 436
2 437
2 438
2 439
2 440
2 441
2 442
2 443
2 444
2 445
2 446
2 447
2 448
2 449
2 450
2 451
2 452
2 453
2 454
2 455
2 456
2 457
2 458
2 459
2 460
2 461
2 462
2 463
2 464
2 465
2 466
2 467
2 468
2 469
2 470
2 471
2 472
2 473
2 474
2 475
2 476
2 477
2 478
2 479
2 480
2 481
2 482
2 483
2 484
2 485
2 486
2 487
2 488
2 489
2 490
2 491
2 492
2 493
2 494
2 495
2 496
2 497
2 498
2 499
2 500
2 501
2 502
2 503
2 504
2 505
2 506
2 507
2 508
2 509
2 510
2 511
2 512
2 513
2 514
2 515
2 516
2 517
2 518
2 519
2 520
2 521
2 522
2 523
2 524
2 525
2 526
2 527
2 528
2 529
2 530
2 531
2 532
2 533
2 534
2 535
2 536
2 537
2 538
2 539
2 540
2 541
2 542
2 543
2 544
2 545
2 546
2 547
2 548
2 549
2 550
2 551
2 552
2 553
2 554
2 555
2 556
2 557
2 558
2 559
2 560
2 561
2 562
2 563
2 564
2 565
2 566
2 567
2 568
2 569
2 570
2 571
2 572
2 573
2 574
2 575
2 576
2 577
2 578
2 579
2 580
2 581
2 582
2 583
2 584
2 585
2 586
2 587
2 588
2 589
2 590
2 591
2 592
2 593
2 594
2 595
2 596
2 597
2 598
2 599
2 600
2 601
2 602
2 603
2 604
2 605
2 606
2 607
2 608
2 609
2 610
2 611
2 612
2 613
2 614
2 615
2 616
2 617
2 618
2 619
2 620
2 621
2 622
2 623
2 624
2 625
2 626
2 627
2 628
2 629
2 630
2 631
2 632
2 633
2 634
2 635
2 636
2 637
2 638
2 639
2 640
2 641
2 642
2 643
2 644
2 645
2 646
2 647
2 648
2 649
2 650
2 651
2 652
2 653
2 654
2 655
2 656
2 657
2 658
2 659
2 660
2 661
2 662
2 663
2 664
2 665
2 666
2 667
2 668
2 669
2 670
2 671
2 672
2 673
2 674
2 675
2 676
2 677
2 678
2 679
2 680
2 681
2 682
2 683
2 684
2 685
2 686
2 687
2 688
2 689
2 690
2 691
2 692
2 693
2 694
2 695
2 696
2 697
2 698
2 699
2 700
2 701
2 702
2 703
2 704
2 705
2 706
2 707
2 708
2 709
2 710
2 711
2 712
2 713
2 714
2 715
2 716
2 717
2 718
2 719
2 720
2 721
2 722
2 723
2 724
2 725
2 726
2 727
2 728
2 729
2 730
2 731
2 732
2 733
2 734
2 735
2 736
2 737
2 738
2 739
2 740
2 741
2 742
2 743
2 744
2 745
2 746
2 747
2 748
2 749
2 750
2 751
2 752
2 753
2 754
2 755
2 756
2 757
2 758
2 759
2 760
2 761
2 762
2 763
2 764
2 765
2 766
2 767
2 768
2 769
2 770
2 771
2 772
2 773
2 774
2 775
2 776
2 777
2 778
2 779
2 780
2 781
2 782
2 783
2 784
2 785
2 786
2 787
2 788
2 789
2 790
2 791
2 792
2 793
2 794
2 795
2 796
2 797
2 798
2 799
2 800
2 801
2 802
2 803
2 804
2 805
2 806
2 807
2 808
2 809
2 810
2 811
2 812
2 813
2 814
2 815
2 816
2 817
2 818
2 819
2 820
2 821
2 822
2 823
2 824
2 825
2 826
2 827
2 828
2 829
2 830
2 831
2 832
2 833
2 834
2 835
2 836
2 837
2 838
2 839
2 840
2 841
2 842
2 843
2 844
2 845
2 846
2 847
2 848
2 849
2 850
2 851
2 852
2 853
2 854
2 855
2 856
2 857
2 858
2 859
2 860
2 861
2 862
2 863
2 864
2 865
2 866
2 867
2 868
2 869
2 870
2 871
2 872
2 873
2 874
2 875
2 876
2 877
2 878
2 879
2 880
2 881
2 882
2 883
2 884
2 885
2 886
2 887
2 888
2 889
2 890
2 891
2 892
2 893
2 894
2 895
2 896
2 897
2 898
2 899
2 900
2 901
2 902
2 903
2 904
2 905
2 906
2 907
2 908
2 909
2 910
2 911
2 912
2 913
2 914
2 915
2 916
2 917
2 918
2 919
2 920
2 921
2 922
2 923
2 924
2 925
2 926
2 927
2 928
2 929
2 930
2 931
2 932
2 933
2 934
2 935
2 936
2 937
2 938
2 939
2 940
2 941
2 942
2 943
2 944
2 945
2 946
2 947
2 948
2 949
2 950
2 951
2 952
2 953
2 954
2 955
2 956
2 957
2 958
2 959
2 960
2 961
2 962
2 963
2 964
2 965
2 966
2 967
2 968
2 969
2 970
2 971
2 972
2 973
2 974
2 975
2 976
2 977
2 978
2 979
2 980
2 981
2 982
2 983
2 984
2 985
2 986
2 987
2 988
2 989
2 990
2 991
2 992
2 993
2 994
2 995
2 996
2 997
2 998
2 999
3 0
3 1
3 2
3 3
3 4
3 5
3 6
3 7
3 8
3 9
3 10
3 11
3 12
3 13
3 14
3 15
3 16
3 17
3 18
3 19
3 20
3 21
3 22
3 23
3 24
3 25
3 26
3 27
3 28
3 29
3 30
3 31
3 32
3 33
3 34
3 35
3 36
3 37
3 38
3 39
3 40
3 41
3 42
3 43
3 44
3 45
3 46
3 47
3 48
3 49
3 50
3 51
3 52
3 53
3 54
3 55
3 56
3 57
3 58
3 59
3 60
3 61
3 62
3 63
3 64
3 65
3 66
3 67
3 68
3 69
3 70
3 71
3 72
3 73
3 74
3 75
3 76
3 77
3 78
3 79
3 80
3 81
3 82
3 83
3 84
3 85
3 86
3 87
3 88
3 89
3 90
3 91
3 92
3 93
3 94
3 95
3 96
3 97
3 98
3 99
3 100
3 101
3 102
3 103
3 104
3 105
3 106
3 107
3 108
3 109
3 110
3 111
3 112
3 113
3 114
3 115
3 116
3 117
3 118
3 119
3 120
3 121
3 122
3 123
3 124
3 125
3 126
3 127
3 128
3 129
3 130
3 131
3 132
3 133
3 134
3 135
3 136
3 137
3 138
3 139
3 140
3 141
3 142
3 143
3 144
3 145
3 146
3 147
3 148
3 149
3 150
3 151
3 152
3 153
3 154
3 155
3 156
3 157
3 158
3 159
3 160
3 161
3 162
3 163
3 164
3 165
3 166
3 167
3 168
3 169
3 170
3 171
3 172
3 173
3 174
3 175
3 176
3 177
3 178
3 179
3 180
3 181
3 182
3 183
3 184
3 185
3 186
3 187
3 188
3 189
3 190
3 191
3 192
3 193
3 194
3 195
3 196
3 197
3 198
3 199
3 200
3 201
3 202
3 203
3 204
3 205
3 206
3 207
3 208
3 209
3 210
3 211
3 212
3 213
3 214
3 215
3 216
3 217
3 218
3 219
3 220
3 221
3 222
3 223
3 224
3 225
3 226
3 227
3 228
3 229
3 230
3 231
3 232
3 233
3 234
3 235
3 236
3 237
3 238
3 239
3 240
3 241
3 242
3 243
3 244
3 245
3 246
3 247
3 248
3 249
3 250
3 251
3 252
3 253
3 254
3 255
3 256
3 257
3 258
3 259
3 260
3 261
3 262
3 263
3 264
3 265
3 266
3 267
3 268
3 269
3 270
3 271
3 272
3 273
3 274
3 275
3 276
3 277
3 278
3 279
3 280
3 281
3 282
3 283
3 284
3 285
3 286
3 287
3 288
3 289
3 290
3 291
3 292
3 293
3 294
3 295
3 296
3 297
3 298
3 299
3 300
3 301
3 302
3 303
3 304
3 305
3 306
3 307
3 308
3 309
3 310
3 311
3 312
3 313
3 314
3 315
3 316
3 317
3 318
3 319
3 320
3 321
3 322
3 323
3 324
3 325
3 326
3 327
3 328
3 329
3 330
3 331
3 332
3 333
3 334
3 335
3 336
3 337
3 338
3 339
3 340
3 341
3 342
3 343
3 344
3 345
3 346
3 347
3 348
3 349
3 350
3 351
3 352
3 353
3 354
3 355
3 356
3 357
3 358
3 359
3 360
3 361
3 362
3 363
3 364
3 365
3 366
3 367
3 368
3 369
3 370
3 371
3 372
3 373
3 374
3 375
3 376
3 377
3 378
3 379
3 380
3 381
3 382
3 383
3 384
3 385
3 386
3 387
3 388
3 389
3 390
3 391
3 392
3 393
3 394
3 395
3 396
3 397
3 398
3 399
3 400
3 401
3 402
3 403
3 404
3 405
3 406
3 407
3 408
3 409
3 410
3 411
3 412
3 413
3 414
3 415
3 416
3 417
3 418
3 419
3 420
3 421
3 422
3 423
3 424
3 425
3 426
3 427
3 428
3 429
3 430
3 431
3 432
3 433
3 434
3 435
3 436
3 437
3 438
3 439
3 440
3 441
3 442
3 443
3 444
3 445
3 446
3 447
3 448
3 449
3 450
3 451
3 452
3 453
3 454
3 455
3 456
3 457
3 458
3 459
3 460
3 461
3 462
3 463
3 464
3 465
3 466
3 467
3 468
3 469
3 470
3 471
3 472
3 473
3 474
3 475
3 476
3 477
3 478
3 479
3 480
3 481
3 482
3 483
3 484
3 485
3 486
3 487
3 488
3 489
3 490
3 491
3 492
3 493
3 494
3 495
3 496
3 497
3 498
3 499
3 500
3 501
3 502
3 503
3 504
3 505
3 506
3 507
3 508
3 509
3 510
3 511
3 512
3 513
3 514
3 515
3 516
3 517
3 518
3 519
3 520
3 521
3 522
3 523
3 524
3 525
3 526
3 527
3 528
3 529
3 530
3 531
3 532
3 533
3 534
3 535
3 536
3 537
3 538
3 539
3 540
3 541
3 542
3 543
3 544
3 545
3 546
3 547
3 548
3 549
3 550
3 551
3 552
3 553
3 554
3 555
3 556
3 557
3 558
3 559
3 560
3 561
3 562
3 563
3 564
3 565
3 566
3 567
3 568
3 569
3 570
3 571
3 572
3 573
3 574
3 575
3 576
3 577
3 578
3 579
3 580
3 581
3 582
3 583
3 584
3 585
3 586
3 587
3 588
3 589
3 590
3 591
3 592
3 593
3 594
3 595
3 596
3 597
3 598
3 599
3 600
3 601
3 602
3 603
3 604
3 605
3 606
3 607
3 608
3 609
3 610
3 611
3 612
3 613
3 614
3 615
3 616
3 617
3 618
3 619
3 620
3 621
3 622
3 623
3 624
3 625
3 626
3 627
3 628
3 629
3 630
3 631
3 632
3 633
3 634
3 635
3 636
3 637
3 638
3 639
3 640
3 641
3 642
3 643
3 644
3 645
3 646
3 647
3 648
3 649
3 650
3 651
3 652
3 653
3 654
3 655
3 656
3 657
3 658
3 659
3 660
3 661
3 662
3 663
3 664
3 665
3 666
3 667
3 668
3 669
3 670
3 671
3 672
3 673
3 674
3 675
3 676
3 677
3 678
3 679
3 680
3 681
3 682
3 683
3 684
3 685
3 686
3 687
3 688
3 689
3 690
3 691
3 692
3 693
3 694
3 695
3 696
3 697
3 698
3 699
3 700
3 701
3 702
3 703
3 704
3 705
3 706
3 707
3 708
3 709
3 710
3 711
3 712
3 713
3 714
3 715
3 716
3 717
3 718
3 719
3 720
3 721
3 722
3 723
3 724
3 725
3 726
3 727
3 728
3 729
3 730
3 731
3 732
3 733
3 734
3 735
3 736
3 737
3 738
3 739
3 740
3 741
3 742
3 743
3 744
3 745
3 746
3 747
3 748
3 749
3 750
3 751
3 752
3 753
3 754
3 755
3 756
3 757
3 758
3 759
3 760
3 761
3 762
3 763
3 764
3 765
3 766
3 767
3 768
3 769
3 770
3 771
3 772
3 773
3 774
3 775
3 776
3 777
3 778
3 779
3 780
3 781
3 782
3 783
3 784
3 785
3 786
3 787
3 788
3 789
3 790
3 791
3 792
3 793
3 794
3 795
3 796
3 797
3 798
3 799
3 800
3 801
3 802
3 803
3 804
3 805
3 806
3 807
3 808
3 809
3 810
3 811
3 812
3 813
3 814
3 815
3 816
3 817
3 818
3 819
3 820
3 821
3 822
3 823
3 824
3 825
3 826
3 827
3 828
3 829
3 830
3 831
3 832
3 833
3 834
3 835
3 836
3 837
3 838
3 839
3 840
3 841
3 842
3 843
3 844
3 845
3 846
3 847
3 848
3 849
3 850
3 851
3 852
3 853
3 854
3 855
3 856
3 857
3 858
3 859
3 860
3 861
3 862
3 863
3 864
3 865
3 866
3 867
3 868
3 869
3 870
3 871
3 872
3 873
3 874
3 875
3 876
3 877
3 878
3 879
3 880
3 881
3 882
3 883
3 884
3 885
3 886
3 887
3 888
3 889
3 890
3 891
3 892
3 893
3 894
3 895
3 896
3 897
3 898
3 899
3 900
3 901
3 902
3 903
3 904
3 905
3 906
3 907
3 908
3 909
3 910
3 911
3 912
3 913
3 914
3 915
3 916
3 917
3 918
3 919
3 920
3 921
3 922
3 923
3 924
3 925
3 926
3 927
3 928
3 929
3 930
3 931
3 932
3 933
3 934
3 935
3 936
3 937
3 938
3 939
3 940
3 941
3 942
3 943
3 944
3 945
3 946
3 947
3 948
3 949
3 950
3 951
3 952
3 953
3 954
3 955
3 956
3 957
3 958
3 959
3 960
3 961
3 962
3 963
3 964
3 965
3 966
3 967
3 968
3 969
3 970
3 971
3 972
3 973
3 974
3 975
3 976
3 977
3 978
3 979
3 980
3 981
3 982
3 983
3 984
3 985
3 986
3 987
3 988
3 989
3 990
3 991
3 992
3 993
3 994
3 995
3 996
3 997
3 998
3 999
4 0
4 1
4 2
4 3
4 4
4 5
4 6
4 7
4 8
4 9
4 10
4 11
4 12
4 13
4 14
4 15
4 16
4 17
4 18
4 19
4 20
4 21
4 22
4 23
4 24
4 25
4 26
4 27
4 28
4 29
4 30
4 31
4 32
4 33
4 34
4 35
4 36
4 37
4 38
4 39
4 40
4 41
4 42
4 43
4 44
4 45
4 46
4 47
4 48
4 49
4 50
4 51
4 52
4 53
4 54
4 55
4 56
4 57
4 58
4 59
4 60
4 61
4 62
4 63
4 64
4 65
4 66
4 67
4 68
4 69
4 70
4 71
4 72
4 73
4 74
4 75
4 76
4 77
4 78
4 79
4 80
4 81
4 82
4 83
4 84
4 85
4 86
4 87
4 88
4 89
4 90
4 91
4 92
4 93
4 94
4 95
4 96
4 97
4 98
4 99
4 100
4 101
4 102
4 103
4 104
4 105
4 106
4 107
4 108
4 109
4 110
4 111
4 112
4 113
4 114
4 115
4 116
4 117
4 118
4 119
4 120
4 121
4 122
4 123
4 124
4 125
4 126
4 127
4 128
4 129
4 130
4 131
4 132
4 133
4 134
4 135
4 136
4 137
4 138
4 139
4 140
4 141
4 142
4 143
4 144
4 145
4 146
4 147
4 148
4 149
4 150
4 151
4 152
4 153
4 154
4 155
4 156
4 157
4 158
4 159
4 160
4 161
4 162
4 163
4 164
4 165
4 166
4 167
4 168
4 169
4 170
4 171
4 172
4 173
4 174
4 175
4 176
4 177
4 178
4 179
4 180
4 181
4 182
4 183
4 184
4 185
4 186
4 187
4 188
4 189
4 190
4 191
4 192
4 193
4 194
4 195
4 196
4 197
4 198
4 199
4 200
4 201
4 202
4 203
4 204
4 205
4 206
4 207
4 208
4 209
4 210
4 211
4 212
4 213
4 214
4 215
4 216
4 217
4 218
4 219
4 220
4 221
4 222
4 223
4 224
4 225
4 226
4 227
4 228
4 229
4 230
4 231
4 232
4 233
4 234
4 235
4 236
4 237
4 238
4 239
4 240
4 241
4 242
4 243
4 244
4 245
4 246
4 247
4 248
4 249
4 250
4 251
4 252
4 253
4 254
4 255
4 256
4 257
4 258
4 259
4 260
4 261
4 262
4 263
4 264
4 265
4 266
4 267
4 268
4 269
4 270
4 271
4 272
4 273
4 274
4 275
4 276
4 277
4 278
4 279
4 280
4 281
4 282
4 283
4 284
4 285
4 286
4 287
4 288
4 289
4 290
4 291
4 292
4 293
4 294
4 295
4 296
4 297
4 298
4 299
4 300
4 301
4 302
4 303
4 304
4 305
4 306
4 307
4 308
4 309
4 310
4 311
4 312
4 313
4 314
4 315
4 316
4 317
4 318
4 319
4 320
4 321
4 322
4 323
4 324
4 325
4 326
4 327
4 328
4 329
4 330
4 331
4 332
4 333
4 334
4 335
4 336
4 337
4 338
4 339
4 340
4 341
4 342
4 343
4 344
4 345
4 346
4 347
4 348
4 349
4 350
4 351
4 352
4 353
4 354
4 355
4 356
4 357
4 358
4 359
4 360
4 361
4 362
4 363
4 364
4 365
4 366
4 367
4 368
4 369
4 370
4 371
4 372
4 373
4 374
4 375
4 376
4 377
4 378
4 379
4 380
4 381
4 382
4 383
4 384
4 385
4 386
4 387
4 388
4 389
4 390
4 391
4 392
4 393
4 394
4 395
4 396
4 397
4 398
4 399
4 400
4 401
4 402
4 403
4 404
4 405
4 406
4 407
4 408
4 409
4 410
4 411
4 412
4 413
4 414
4 415
4 416
4 417
4 418
4 419
4 420
4 421
4 422
4 423
4 424
4 425
4 426
4 427
4 428
4 429
4 430
4 431
4 432
4 433
4 434
4 435
4 436
4 437
4 438
4 439
4 440
4 441
4 442
4 443
4 444
4 445
4 446
4 447
4 448
4 449
4 450
4 451
4 452
4 453
4 454
4 455
4 456
4 457
4 458
4 459
4 460
4 461
4 462
4 463
4 464
4 465
4 466
4 467
4 468
4 469
4 470
4 471
4 472
4 473
4 474
4 475
4 476
4 477
4 478
4 479
4 480
4 481
4 482
4 483
4 484
4 485
4 486
4 487
4 488
4 489
4 490
4 491
4 492
4 493
4 494
4 495
4 496
4 497
4 498
4 499
4 500
4 501
4 502
4 503
4 504
4 505
4 506
4 507
4 508
4 509
4 510
4 511
4 512
4 513
4 514
4 515
4 516
4 517
4 518
4 519
4 520
4 521
4 522
4 523
4 524
4 525
4 526
4 527
4 528
4 529
4 530
4 531
4 532
4 533
4 534
4 535
4 536
4 537
4 538
4 539
4 540
4 541
4 542
4 543
4 544
4 545
4 546
4 547
4 548
4 549
4 550
4 551
4 552
4 553
4 554
4 555
4 556
4 557
4 558
4 559
4 560
4 561
4 562
4 563
4 564
4 565
4 566
4 567
4 568
4 569
4 570
4 571
4 572
4 573
4 574
4 575
4 576
4 577
4 578
4 579
4 580
4 581
4 582
4 583
4 584
4 585
4 586
4 587
4 588
4 589
4 590
4 591
4 592
4 593
4 594
4 595
4 596
4 597
4 598
4 599
4 600
4 601
4 602
4 603
4 604
4 605
4 606
4 607
4 608
4 609
4 610
4 611
4 612
4 613
4 614
4 615
4 616
4 617
4 618
4 619
4 620
4 621
4 622
4 623
4 624
4 625
4 626
4 627
4 628
4 629
4 630
4 631
4 632
4 633
4 634
4 635
4 636
4 637
4 638
4 639
4 640
4 641
4 642
4 643
4 644
4 645
4 646
4 647
4 648
4 649
4 650
4 651
4 652
4 653
4 654
4 655
4 656
4 657
4 658
4 659
4 660
4 661
4 662
4 663
4 664
4 665
4 666
4 667
4 668
4 669
4 670
4 671
4 672
4 673
4 674
4 675
4 676
4 677
4 678
4 679
4 680
4 681
4 682
4 683
4 684
4 685
4 686
4 687
4 688
4 689
4 690
4 691
4 692
4 693
4 694
4 695
4 696
4 697
4 698
4 699
4 700
4 701
4 702
4 703
4 704
4 705
4 706
4 707
4 708
4 709
4 710
4 711
4 712
4 713
4 714
4 715
4 716
4 717
4 718
4 719
4 720
4 721
4 722
4 723
4 724
4 725
4 726
4 727
4 728
4 729
4 730
4 731
4 732
4 733
4 734
4 735
4 736
4 737
4 738
4 739
4 740
4 741
4 742
4 743
4 744
4 745
4 746
4 747
4 748
4 749
4 750
4 751
4 752
4 753
4 754
4 755
4 756
4 757
4 758
4 759
4 760
4 761
4 762
4 763
4 764
4 765
4 766
4 767
4 768
4 769
4 770
4 771
4 772
4 773
4 774
4 775
4 776
4 777
4 778
4 779
4 780
4 781
4 782
4 783
4 784
4 785
4 786
4 787
4 788
4 789
4 790
4 791
4 792
4 793
4 794
4 795
4 796
4 797
4 798
4 799
4 800
4 801
4 802
4 803
4 804
4 805
4 806
4 807
4 808
4 809
4 810
4 811
4 812
4 813
4 814
4 815
4 816
4 817
4 818
4 819
4 820
4 821
4 822
4 823
4 824
4 825
4 826
4 827
4 828
4 829
4 830
4 831
4 832
4 833
4 834
4 835
4 836
4 837
4 838
4 839
4 840
4 841
4 842
4 843
4 844
4 845
4 846
4 847
4 848
4 849
4 850
4 851
4 852
4 853
4 854
4 855
4 856
4 857
4 858
4 859
4 860
4 861
4 862
4 863
4 864
4 865
4 866
4 867
4 868
4 869
4 870
4 871
4 872
4 873
4 874
4 875
4 876
4 877
4 878
4 879
4 880
4 881
4 882
4 883
4 884
4 885
4 886
4 887
4 888
4 889
4 890
4 891
4 892
4 893
4 894
4 895
4 896
4 897
4 898
4 899
4 900
4 901
4 902
4 903
4 904
4 905
4 906
4 907
4 908
4 909
4 910
4 911
4 912
4 913
4 914
4 915
4 916
4 917
4 918
4 919
4 920
4 921
4 922
4 923
4 924
4 925
4 926
4 927
4 928
4 929
4 930
4 931
4 932
4 933
4 934
4 935
4 936
4 937
4 938
4 939
4 940
4 941
4 942
4 943
4 944
4 945
4 946
4 947
4 948
4 949
4 950
4 951
4 952
4 953
4 954
4 955
4 956
4 957
4 958
4 959
4 960
4 961
4 962
4 963
4 964
4 965
4 966
4 967
4 968
4 969
4 970
4 971
4 972
4 973
4 974
4 975
4 976
4 977
4 978
4 979
4 980
4 981
4 982
4 983
4 984
4 985
4 986
4 987
4 988
4 989
4 990
4 991
4 992
4 993
4 994
4 995
4 996
4 997
4 998
4 999
5 0
5 1
5 2
5 3
5 4
5 5
5 6
5 7
5 8
5 9
5 10
5 11
5 12
5 13
5 14
5 15
5 16
5 17
5 18
5 19
5 20
5 21
5 22
5 23
5 24
5 25
5 26
5 27
5 28
5 29
5 30
5 31
5 32
5 33
5 34
5 35
5 36
5 37
5 38
5 39
5 40
5 41
5 42
5 43
5 44
5 45
5 46
5 47
5 48
5 49
5 50
5 51
5 52
5 53
5 54
5 55
5 56
5 57
5 58
5 59
5 60
5 61
5 62
5 63
5 64
5 65
5 66
5 67
5 68
5 69
5 70
5 71
5 72
5 73
5 74
5 75
5 76
5 77
5 78
5 79
5 80
5 81
5 82
5 83
5 84
5 85
5 86
5 87
5 88
5 89
5 90
5 91
5 92
5 93
5 94
5 95
5 96
5 97
5 98
5 99
5 100
5 101
5 102
5 103
5 104
5 105
5 106
5 107
5 108
5 109
5 110
5 111
5 112
5 113
5 114
5 115
5 116
5 117
5 118
5 119
5 120
5 121
5 122
5 123
5 124
5 125
5 126
5 127
5 128
5 129
5 130
5 131
5 132
5 133
5 134
5 135
5 136
5 137
5 138
5 139
5 140
5 141
5 142
5 143
5 144
5 145
5 146
5 147
5 148
5 149
5 150
5 151
5 152
5 153
5 154
5 155
5 156
5 157
5 158
5 159
5 160
5 161
5 162
5 163
5 164
5 165
5 166
5 167
5 168
5 169
5 170
5 171
5 172
5 173
5 174
5 175
5 176
5 177
5 178
5 179
5 180
5 181
5 182
5 183
5 184
5 185
5 186
5 187
5 188
5 189
5 190
5 191
5 192
5 193
5 194
5 195
5 196
5 197
5 198
5 199
5 200
5 201
5 202
5 203
5 204
5 205
5 206
5 207
5 208
5 209
5 210
5 211
5 212
5 213
5 214
5 215
5 216
5 217
5 218
5 219
5 220
5 221
5 222
5 223
5 224
5 225
5 226
5 227
5 228
5 229
5 230
5 231
5 232
5 233
5 234
5 235
5 236
5 237
5 238
5 239
5 240
5 241
5 242
5 243
5 244
5 245
5 246
5 247
5 248
5 249
5 250
5 251
5 252
5 253
5 254
5 255
5 256
5 257
5 258
5 259
5 260
5 261
5 262
5 263
5 264
5 265
5 266
5 267
5 268
5 269
5 270
5 271
5 272
5 273
5 274
5 275
5 276
5 277
5 278
5 279
5 280
5 281
5 282
5 283
5 284
5 285
5 286
5 287
5 288
5 289
5 290
5 291
5 292
5 293
5 294
5 295
5 296
5 297
5 298
5 299
5 300
5 301
5 302
5 303
5 304
5 305
5 306
5 307
5 308
5 309
5 310
5 311
5 312
5 313
5 314
5 315
5 316
5 317
5 318
5 319
5 320
5 321
5 322
5 323
5 324
5 325
5 326
5 327
5 328
5 329
5 330
5 331
5 332
5 333
5 334
5 335
5 336
5 337
5 338
5 339
5 340
5 341
5 342
5 343
5 344
5 345
5 346
5 347
5 348
5 349
5 350
5 351
5 352
5 353
5 354
5 355
5 356
5 357
5 358
5 359
5 360
5 361
5 362
5 363
5 364
5 365
5 366
5 367
5 368
5 369
5 370
5 371
5 372
5 373
5 374
5 375
5 376
5 377
5 378
5 379
5 380
5 381
5 382
5 383
5 384
5 385
5 386
5 387
5 388
5 389
5 390
5 391
5 392
5 393
5 394
5 395
5 396
5 397
5 398
5 399
5 400
5 401
5 402
5 403
5 404
5 405
5 406
5 407
5 408
5 409
5 410
5 411
5 412
5 413
5 414
5 415
5 416
5 417
5 418
5 419
5 420
5 421
5 422
5 423
5 424
5 425
5 426
5 427
5 428
5 429
5 430
5 431
5 432
5 433
5 434
5 435
5 436
5 437
5 438
5 439
5 440
5 441
5 442
5 443
5 444
5 445
5 446
5 447
5 448
5 449
5 450
5 451
5 452
5 453
5 454
5 455
5 456
5 457
5 458
5 459
5 460
5 461
5 462
5 463
5 464
5 465
5 466
5 467
5 468
5 469
5 470
5 471
5 472
5 473
5 474
5 475
5 476
5 477
5 478
5 479
5 480
5 481
5 482
5 483
5 484
5 485
5 486
5 487
5 488
5 489
5 490
5 491
5 492
5 493
5 494
5 495
5 496
5 497
5 498
5 499
5 500
5 501
5 502
5 503
5 504
5 505
5 506
5 507
5 508
5 509
5 510
5 511
5 512
5 513
5 514
5 515
5 516
5 517
5 518
5 519
5 520
5 521
5 522
5 523
5 524
5 525
5 526
5 527
5 528
5 529
5 530
5 531
5 532
5 533
5 534
5 535
5 536
5 537
5 538
5 539
5 540
5 541
5 542
5 543
5 544
5 545
5 546
5 547
5 548
5 549
5 550
5 551
5 552
5 553
5 554
5 555
5 556
5 557
5 558
5 559
5 560
5 561
5 562
5 563
5 564
5 565
5 566
5 567
5 568
5 569
5 570
5 571
5 572
5 573
5 574
5 575
5 576
5 577
5 578
5 579
5 580
5 581
5 582
5 583
5 584
5 585
5 586
5 587
5 588
5 589
5 590
5 591
5 592
5 593
5 594
5 595
5 596
5 597
5 598
5 599
5 600
5 601
5 602
5 603
5 604
5 605
5 606
5 607
5 608
5 609
5 610
5 611
5 612
5 613
5 614
5 615
5 616
5 617
5 618
5 619
5 620
5 621
5 622
5 623
5 624
5 625
5 626
5 627
5 628
5 629
5 630
5 631
5 632
5 633
5 634
5 635
5 636
5 637
5 638
5 639
5 640
5 641
5 642
5 643
5 644
5 645
5 646
5 647
5 648
5 649
5 650
5 651
5 652
5 653
5 654
5 655
5 656
5 657
5 658
5 659
5 660
5 661
5 662
5 663
5 664
5 665
5 666
5 667
5 668
5 669
5 670
5 671
5 672
5 673
5 674
5 675
5 676
5 677
5 678
5 679
5 680
5 681
5 682
5 683
5 684
5 685
5 686
5 687
5 688
5 689
5 690
5 691
5 692
5 693
5 694
5 695
5 696
5 697
5 698
5 699
5 700
5 701
5 702
5 703
5 704
5 705
5 706
5 707
5 708
5 709
5 710
5 711
5 712
5 713
5 714
5 715
5 716
5 717
5 718
5 719
5 720
5 721
5 722
5 723
5 724
5 725
5 726
5 727
5 728
5 729
5 730
5 731
5 732
5 733
5 734
5 735
5 736
5 737
5 738
5 739
5 740
5 741
5 742
5 743
5 744
5 745
5 746
5 747
5 748
5 749
5 750
5 751
5 752
5 753
5 754
5 755
5 756
5 757
5 758
5 759
5 760
5 761
5 762
5 763
5 764
5 765
5 766
5 767
5 768
5 769
5 770
5 771
5 772
5 773
5 774
5 775
5 776
5 777
5 778
5 779
5 780
5 781
5 782
5 783
5 784
5 785
5 786
5 787
5 788
5 789
5 790
5 791
5 792
5 793
5 794
5 795
5 796
5 797
5 798
5 799
5 800
5 801
5 802
5 803
5 804
5 805
5 806
5 807
5 808
5 809
5 810
5 811
5 812
5 813
5 814
5 815
5 816
5 817
5 818
5 819
5 820
5 821
5 822
5 823
5 824
5 825
5 826
5 827
5 828
5 829
5 830
5 831
5 832
5 833
5 834
5 835
5 836
5 837
5 838
5 839
5 840
5 841
5 842
5 843
5 844
5 845
5 846
5 847
5 848
5 849
5 850
5 851
5 852
5 853
5 854
5 855
5 856
5 857
5 858
5 859
5 860
5 861
5 862
5 863
5 864
5 865
5 866
5 867
5 868
5 869
5 870
5 871
5 872
5 873
5 874
5 875
5 876
5 877
5 878
5 879
5 880
5 881
5 882
5 883
5 884
5 885
5 886
5 887
5 888
5 889
5 890
5 891
5 892
5 893
5 894
5 895
5 896
5 897
5 898
5 899
5 900
5 901
5 902
5 903
5 904
5 905
5 906
5 907
5 908
5 909
5 910
5 911
5 912
5 913
5 914
5 915
5 916
5 917
5 918
5 919
5 920
5 921
5 922
5 923
5 924
5 925
5 926
5 927
5 928
5 929
5 930
5 931
5 932
5 933
5 934
5 935
5 936
5 937
5 938
5 939
5 940
5 941
5 942
5 943
5 944
5 945
5 946
5 947
5 948
5 949
5 950
5 951
5 952
5 953
5 954
5 955
5 956
5 957
5 958
5 959
5 960
5 961
5 962
5 963
5 964
5 965
5 966
5 967
5 968
5 969
5 970
5 971
5 972
5 973
5 974
5 975
5 976
5 977
5 978
5 979
5 980
5 981
5 982
5 983
5 984
5 985
5 986
5 987
5 988
5 989
5 990
5 991
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/Users/nikhilchakravarthula/vscode/Blog1/_notebooks/2023-03-22-DS-space_time_complexity.ipynb Cell 22 in <cell line: 2>()
      <a href='vscode-notebook-cell:/Users/nikhilchakravarthula/vscode/Blog1/_notebooks/2023-03-22-DS-space_time_complexity.ipynb#X30sZmlsZQ%3D%3D?line=1'>2</a> for i in numbers:
      <a href='vscode-notebook-cell:/Users/nikhilchakravarthula/vscode/Blog1/_notebooks/2023-03-22-DS-space_time_complexity.ipynb#X30sZmlsZQ%3D%3D?line=2'>3</a>     for j in numbers:
----> <a href='vscode-notebook-cell:/Users/nikhilchakravarthula/vscode/Blog1/_notebooks/2023-03-22-DS-space_time_complexity.ipynb#X30sZmlsZQ%3D%3D?line=3'>4</a>         print(i,j)

File ~/opt/anaconda3/lib/python3.9/site-packages/ipykernel/iostream.py:531, in OutStream.write(self, string)
    529 is_child = (not self._is_master_process())
    530 # only touch the buffer in the IO thread to avoid races
--> 531 self.pub_thread.schedule(lambda: self._buffer.write(string))
    532 if is_child:
    533     # mp.Pool cannot be trusted to flush promptly (or ever),
    534     # and this helps.
    535     if self._subprocess_flush_pending:

File ~/opt/anaconda3/lib/python3.9/site-packages/ipykernel/iostream.py:216, in IOPubThread.schedule(self, f)
    214     self._events.append(f)
    215     # wake event thread (message content is ignored)
--> 216     self._event_pipe.send(b'')
    217 else:
    218     f()

File ~/opt/anaconda3/lib/python3.9/site-packages/zmq/sugar/socket.py:620, in Socket.send(self, data, flags, copy, track, routing_id, group)
    613         data = zmq.Frame(
    614             data,
    615             track=track,
    616             copy=copy or None,
    617             copy_threshold=self.copy_threshold,
    618         )
    619     data.group = group
--> 620 return super().send(data, flags=flags, copy=copy, track=track)

File zmq/backend/cython/socket.pyx:746, in zmq.backend.cython.socket.Socket.send()

File zmq/backend/cython/socket.pyx:793, in zmq.backend.cython.socket.Socket.send()

File zmq/backend/cython/socket.pyx:250, in zmq.backend.cython.socket._send_copy()

File ~/opt/anaconda3/lib/python3.9/site-packages/zmq/backend/cython/checkrc.pxd:13, in zmq.backend.cython.checkrc._check_rc()

KeyboardInterrupt: 

Space

This function takes two matrices matrix1 and matrix2 as input and returns their product as a new matrix. The function creates a new matrix result with dimensions m by n to store the product of the input matrices. The size of result depends on the size of the input matrices, so the space complexity of this function is O(n^2). As the size of the input matrices increases, the amount of memory required to execute the function also increases quadratically.

def multiply_matrices(matrix1, matrix2):
    m = len(matrix1) 
    n = len(matrix2[0])
    result = [[0] * n] * m #this creates the new matrix based on the size of matrix 1 and 2
    for i in range(m):
        for j in range(n):
            for k in range(len(matrix2)):
                result[i][j] += matrix1[i][k] * matrix2[k][j]
    return result

print(multiply_matrices([[1,2],[3,4]], [[3,4],[1,2]]))
[[18, 28], [18, 28]]

Logarithmic O(logn)

Time

An example of a log time algorithm is binary search. Binary search is an algorithm that searches for a specific element in a sorted list by repeatedly dividing the search interval in half. As a result, the time taken to complete the search grows logarithmically with the size of the list. Hence, the time complexity of this operation is O(log n), where n is the size of the list being searched.

def binary_search(arr, low, high, target):
    while low <= high:
        mid = (low + high) // 2 #integer division
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            low = mid + 1
        else:
            high = mid - 1

target = 263
result = binary_search(numbers, 0, len(numbers) - 1, target)

print(result)
263

Space

The same algorithm above has a O(logn) space complexity. The function takes an array arr, its lower and upper bounds low and high, and a target value target. The function searches for target within the bounds of arr by recursively dividing the search space in half until the target is found or the search space is empty. The function does not create any new data structures that depend on the size of arr. Instead, the function uses the call stack to keep track of the recursive calls. Since the maximum depth of the recursive calls is O(logn), where n is the size of arr, the space complexity of this function is O(logn). As the size of arr increases, the amount of memory required to execute the function grows logarithmically.

Exponential O(2^n)

Time

An example of an O(2^n) algorithm is the recursive implementation of the Fibonacci sequence. The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. The recursive implementation of the Fibonacci sequence calculates each number by recursively calling itself with the two preceding numbers until it reaches the base case (i.e., the first or second number in the sequence). The algorithm takes O(2^n) time in the worst case because it has to calculate each number in the sequence by making two recursive calls.

def fibonacci(n):
    if n <= 1:
        return n
    else:
        return fibonacci(n-1) + fibonacci(n-2)

#print(fibonacci(5))
#print(fibonacci(10))
#print(fibonacci(20))
print(fibonacci(30))
#print(fibonacci(40))
832040

Space

This function takes a set s as input and generates all possible subsets of s. The function does this by recursively generating the subsets of the set without the first element, and then adding the first element to each of those subsets to generate the subsets that include the first element. The function creates a new list for each recursive call that stores the subsets, and each element in the list is a new list that represents a subset. The number of subsets that can be generated from a set of size n is 2^n, so the space complexity of this function is O(2^n). As the size of the input set increases, the amount of memory required to execute the function grows exponentially.

def generate_subsets(s):
    if not s:
        return [[]]
    subsets = generate_subsets(s[1:])
    return [[s[0]] + subset for subset in subsets] + subsets

print(generate_subsets([1,2,3]))
#print(generate_subsets(numbers))
[[1, 2, 3], [1, 2], [1, 3], [1], [2, 3], [2], [3], []]

Using the time library, we are able to see the difference in time it takes to calculate the fibonacci function above.

  • Based on what is known about the other time complexities, hypothesize the resulting elapsed time if the function is replaced.
import time

start_time = time.time()
print(fibonacci(34))
end_time = time.time()

total_time = end_time - start_time
print("Time taken:", total_time, "seconds")

start_time = time.time()
print(fibonacci(35))
end_time = time.time()

total_time = end_time - start_time
print("Time taken:", total_time, "seconds")
5702887
Time taken: 1.0746629238128662 seconds
9227465
Time taken: 1.7209889888763428 seconds

Hacks

  • Record your findings when testing the time elapsed of the different algorithms.
  • Although we will go more in depth later, time complexity is a key concept that relates to the different sorting algorithms. Do some basic research on the different types of sorting algorithms and their time complexity.
  • Why is time and space complexity important when choosing an algorithm?
  • Should you always use a constant time algorithm / Should you never use an exponential time algorithm? Explain?
  • What are some general patterns that you noticed to determine each algorithm's time and space complexity?

Complete the Time and Space Complexity analysis questions linked below. Practice

Hacks Answers

  1. Time complexity is an important concept when discussing sorting algorithms, as it helps us to understand the efficiency of different algorithms. Here are some common sorting algorithms and their time complexity: Bubble Sort: O(n^2) average and worst-case, O(n) best-case
    • Selection Sort: O(n^2) average and worst-case, O(n^2) best-case
    • Insertion Sort: O(n^2) average and worst-case, O(n) best-case
    • Merge Sort: O(n*log(n)) average, worst-case, and best-case
    • Quick Sort: O(n*log(n)) average and best-case, O(n^2) worst-case
    • Heap Sort: O(nlog(n)) average and worst-case, O(nlog(n)) best-case
  1. Time and space complexity are crucial when choosing an algorithm because they give us an estimate of the resources required to perform a task. Time complexity relates to the number of operations an algorithm takes to complete, while space complexity concerns the amount of memory needed. By understanding these complexities, we can choose the most efficient algorithm for a given problem, balancing the trade-offs between execution time and memory usage.

  2. It's not always necessary to use a constant-time algorithm, nor is it always necessary to avoid an exponential-time algorithm. The choice depends on the specific problem, input size, and available resources. For small input sizes, an exponential-time algorithm might be acceptable, as the performance difference may be negligible. Conversely, a constant-time algorithm might not always be optimal, as it may not be the most efficient or practical solution for the problem at hand.

  3. Some general patterns to determine an algorithm's time and space complexity include:

    • Nested loops: The presence of nested loops often indicates a quadratic time complexity (O(n^2)). For example, Bubble Sort, Selection Sort, and Insertion Sort have nested loops.
    • Divide and conquer: Algorithms that break the problem into smaller subproblems and recursively solve them, such as Merge Sort and Quick Sort, usually have a logarithmic component (O(n*log(n))).
    • Data structure manipulation: The time and space complexity of an algorithm can be influenced by the data structures used. For instance, Heap Sort relies on a binary heap, which results in O(n*log(n)) time complexity.
    • Single-pass algorithms: Algorithms that make a single pass through the data, such as counting sort or radix sort, can have linear time complexity (O(n)), but may come with trade-offs in space complexity or limited applicability to specific types of data.





Practice Answers:

  1. O(N + M) time, O(1) space
  2. O(N*N)
  3. O(nLogn)
  4. X will always be a better choice for large inputs
  5. O(log N)
  6. Both of the above
  7. By counting the number of primitive operations performed by the algorithm on a given input size.
  8. O(logkn) I got this one wrong
  9. n(n-1)
  10. False