5.3. Zahlen sortieren in Python#
Wunderbar, wir können nun Karten sortieren. Aber können wir vielleicht noch viel mehr? Sehen Sie sich den Code aus dem letzten Abschnitt genau an. An welcher Stelle geht es wirklich um Karten? Wie und wo müsste man den Code ändern um, z. B., Zahlen zu sortieren?
(Zahlen sortieren in Python)
Ändern oder erweitern Sie Ihren Programmiercode um Zahlen anstatt Karten zu sortieren.
# Kopie des Codes des vorherigen Abschnitts.
def find_smallest_index(hand):
index = 0
for i in range(len(hand)):
if is_smaller(hand[i], hand[index]):
index = i
return index
def remove_smallest_card(hand):
i = find_smallest_index(hand)
card = hand[i]
del hand[i]
return card
def stack_sort(hand):
stack = [];
while len(hand) > 0:
stack.append(remove_smallest_card(hand))
return stack
Wir benötigen lediglich einen neuen Vergleichsoperator is_smaller
.
Show code cell content
def is_smaller(number1, number2):
return number1 < number2
stack_sort([-11, 12, -6, 45, 1, 54, -55, 88])
[-55, -11, -6, 1, 12, 45, 54, 88]
import random
n_numbers = 1000
hand = [random.randint(0, n_numbers) for _ in range(n_numbers)]
stack_sort(hand)
[0,
0,
2,
2,
3,
3,
6,
6,
7,
8,
11,
12,
13,
13,
14,
15,
17,
17,
17,
19,
20,
20,
21,
21,
22,
23,
23,
23,
23,
28,
31,
33,
33,
33,
34,
39,
39,
41,
41,
41,
42,
42,
44,
45,
45,
45,
46,
47,
48,
49,
51,
51,
51,
52,
53,
56,
57,
58,
59,
59,
62,
62,
63,
63,
64,
66,
66,
67,
67,
69,
69,
71,
71,
72,
77,
81,
82,
82,
83,
83,
84,
84,
85,
87,
88,
90,
90,
91,
91,
92,
93,
93,
102,
102,
102,
108,
109,
110,
110,
112,
114,
114,
116,
118,
118,
120,
121,
123,
123,
124,
124,
125,
126,
127,
128,
129,
131,
131,
131,
131,
132,
132,
134,
135,
135,
138,
139,
141,
141,
141,
142,
143,
143,
145,
146,
147,
148,
149,
149,
150,
151,
152,
155,
157,
158,
158,
160,
160,
160,
162,
162,
163,
164,
165,
167,
176,
176,
177,
177,
177,
179,
181,
182,
184,
187,
187,
187,
188,
188,
190,
191,
191,
192,
193,
193,
193,
196,
196,
197,
199,
199,
200,
201,
202,
204,
205,
206,
206,
208,
211,
212,
214,
215,
216,
216,
221,
223,
225,
226,
226,
227,
227,
229,
230,
233,
234,
236,
237,
237,
238,
238,
239,
240,
240,
241,
241,
242,
243,
246,
247,
248,
248,
249,
250,
251,
251,
256,
256,
257,
257,
257,
257,
258,
258,
259,
259,
259,
259,
260,
263,
265,
265,
268,
268,
269,
271,
272,
276,
276,
277,
278,
279,
280,
281,
283,
283,
283,
287,
288,
289,
290,
291,
291,
292,
294,
295,
296,
299,
300,
300,
301,
302,
306,
307,
307,
307,
307,
309,
311,
312,
313,
314,
315,
316,
318,
318,
319,
319,
321,
322,
323,
324,
324,
325,
325,
326,
327,
327,
328,
329,
331,
333,
333,
334,
335,
336,
340,
341,
342,
342,
342,
343,
343,
344,
345,
347,
348,
348,
349,
350,
350,
353,
353,
353,
353,
353,
355,
356,
356,
357,
357,
360,
362,
362,
365,
365,
367,
368,
368,
369,
370,
370,
373,
373,
374,
376,
376,
376,
377,
379,
380,
381,
382,
382,
384,
384,
385,
385,
385,
386,
388,
389,
390,
390,
391,
392,
392,
394,
394,
394,
396,
401,
401,
401,
402,
402,
403,
409,
409,
410,
411,
411,
412,
413,
413,
413,
414,
415,
416,
417,
418,
420,
422,
422,
422,
423,
425,
426,
427,
427,
430,
430,
434,
434,
435,
435,
436,
439,
440,
441,
441,
442,
443,
443,
443,
443,
443,
443,
447,
448,
449,
450,
450,
451,
451,
452,
453,
455,
457,
458,
460,
460,
460,
461,
463,
465,
467,
468,
468,
471,
471,
471,
471,
472,
473,
475,
476,
478,
479,
481,
483,
483,
485,
486,
488,
490,
492,
492,
493,
493,
494,
496,
496,
496,
497,
497,
498,
498,
499,
499,
500,
501,
503,
504,
504,
504,
504,
505,
505,
507,
508,
509,
511,
512,
514,
515,
516,
516,
518,
519,
521,
521,
522,
523,
523,
524,
524,
526,
527,
529,
530,
531,
532,
532,
533,
534,
534,
535,
536,
536,
537,
537,
538,
539,
539,
540,
541,
543,
543,
544,
545,
546,
546,
548,
550,
550,
553,
553,
554,
555,
556,
556,
556,
557,
558,
558,
559,
559,
559,
560,
560,
561,
563,
564,
565,
566,
568,
568,
569,
572,
574,
575,
576,
577,
578,
581,
586,
587,
588,
589,
591,
591,
591,
591,
593,
595,
597,
597,
598,
598,
600,
600,
600,
600,
602,
603,
603,
605,
605,
605,
605,
606,
606,
609,
609,
609,
609,
609,
610,
610,
610,
610,
611,
612,
612,
612,
614,
615,
615,
615,
616,
616,
618,
618,
618,
620,
620,
621,
621,
621,
622,
623,
626,
626,
627,
628,
629,
631,
632,
632,
634,
634,
635,
637,
638,
638,
639,
639,
640,
640,
640,
640,
641,
641,
641,
642,
643,
644,
646,
647,
648,
649,
650,
651,
652,
656,
656,
657,
657,
658,
659,
659,
660,
660,
660,
660,
661,
661,
663,
664,
664,
665,
665,
666,
666,
667,
667,
669,
672,
673,
674,
675,
676,
677,
677,
678,
678,
678,
678,
679,
679,
679,
679,
681,
681,
682,
682,
686,
689,
689,
691,
691,
691,
693,
693,
694,
696,
698,
699,
700,
701,
707,
707,
707,
708,
710,
713,
713,
713,
714,
714,
714,
716,
717,
717,
718,
719,
720,
721,
721,
721,
722,
723,
725,
725,
726,
727,
727,
728,
732,
732,
733,
738,
738,
738,
739,
739,
739,
740,
740,
740,
741,
742,
744,
747,
748,
749,
749,
750,
750,
752,
752,
753,
753,
754,
755,
756,
756,
757,
758,
759,
761,
762,
762,
762,
763,
764,
765,
766,
766,
767,
773,
774,
777,
778,
780,
783,
783,
783,
785,
786,
787,
789,
790,
790,
790,
790,
791,
791,
792,
793,
796,
797,
798,
798,
799,
800,
800,
800,
801,
802,
802,
803,
803,
804,
805,
805,
806,
809,
809,
810,
810,
811,
814,
816,
817,
818,
819,
820,
822,
823,
823,
823,
825,
826,
827,
827,
828,
830,
830,
832,
834,
835,
835,
837,
839,
840,
840,
841,
842,
843,
843,
843,
844,
846,
848,
848,
848,
849,
850,
850,
851,
851,
852,
854,
855,
856,
857,
858,
861,
861,
861,
869,
869,
870,
870,
870,
870,
871,
871,
874,
874,
875,
876,
884,
885,
886,
887,
888,
888,
889,
890,
891,
892,
892,
893,
894,
894,
894,
895,
895,
895,
895,
896,
898,
898,
899,
899,
899,
902,
904,
904,
904,
905,
905,
906,
906,
908,
912,
913,
913,
914,
915,
919,
920,
920,
920,
921,
922,
922,
922,
923,
923,
924,
924,
926,
927,
927,
930,
930,
930,
931,
932,
932,
935,
935,
936,
938,
939,
943,
944,
944,
945,
946,
949,
951,
952,
954,
954,
955,
955,
958,
959,
961,
961,
961,
962,
965,
965,
965,
965,
965,
967,
967,
967,
969,
969,
970,
971,
972,
973,
974,
974,
974,
975,
976,
977,
977,
978,
978,
980,
980,
981,
983,
984,
985,
986,
988,
989,
989,
990,
993,
994,
994,
995,
995,
996,
999,
999,
1000]