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?

Exercise 5.4 (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.

Hide 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,
 1,
 2,
 2,
 3,
 3,
 5,
 5,
 6,
 6,
 6,
 10,
 10,
 13,
 15,
 15,
 18,
 20,
 20,
 21,
 21,
 23,
 23,
 24,
 27,
 27,
 28,
 28,
 28,
 29,
 29,
 32,
 32,
 33,
 34,
 34,
 34,
 35,
 36,
 36,
 39,
 39,
 40,
 40,
 40,
 42,
 43,
 45,
 46,
 47,
 47,
 49,
 50,
 50,
 51,
 51,
 51,
 51,
 52,
 53,
 54,
 55,
 56,
 57,
 57,
 58,
 59,
 60,
 60,
 61,
 62,
 62,
 63,
 63,
 63,
 64,
 66,
 66,
 67,
 68,
 68,
 69,
 75,
 76,
 76,
 77,
 78,
 79,
 79,
 80,
 82,
 86,
 87,
 89,
 89,
 90,
 90,
 91,
 93,
 93,
 95,
 95,
 97,
 97,
 98,
 99,
 99,
 100,
 101,
 102,
 103,
 104,
 105,
 106,
 106,
 107,
 108,
 109,
 109,
 110,
 114,
 117,
 117,
 117,
 124,
 126,
 126,
 128,
 134,
 136,
 136,
 137,
 137,
 137,
 138,
 139,
 139,
 140,
 141,
 143,
 144,
 144,
 144,
 144,
 144,
 145,
 145,
 148,
 148,
 149,
 150,
 150,
 151,
 151,
 152,
 152,
 153,
 153,
 154,
 154,
 155,
 155,
 155,
 156,
 156,
 157,
 157,
 157,
 158,
 158,
 159,
 161,
 161,
 162,
 163,
 164,
 166,
 167,
 168,
 171,
 172,
 173,
 175,
 176,
 180,
 183,
 183,
 184,
 184,
 186,
 187,
 187,
 188,
 188,
 189,
 190,
 190,
 191,
 191,
 191,
 192,
 194,
 194,
 196,
 196,
 196,
 196,
 197,
 197,
 198,
 198,
 198,
 201,
 201,
 202,
 202,
 203,
 203,
 203,
 203,
 203,
 204,
 205,
 206,
 207,
 207,
 208,
 210,
 210,
 210,
 210,
 212,
 213,
 214,
 215,
 218,
 221,
 222,
 222,
 223,
 224,
 225,
 226,
 226,
 226,
 228,
 228,
 228,
 229,
 229,
 229,
 231,
 232,
 233,
 234,
 234,
 234,
 235,
 237,
 237,
 238,
 239,
 239,
 240,
 240,
 242,
 243,
 246,
 247,
 247,
 249,
 252,
 254,
 256,
 257,
 257,
 258,
 259,
 260,
 260,
 264,
 265,
 265,
 265,
 265,
 267,
 267,
 267,
 267,
 268,
 268,
 269,
 270,
 272,
 272,
 273,
 274,
 274,
 275,
 275,
 277,
 277,
 279,
 279,
 279,
 279,
 280,
 283,
 286,
 287,
 289,
 289,
 289,
 290,
 294,
 295,
 295,
 295,
 296,
 298,
 298,
 298,
 299,
 299,
 301,
 303,
 305,
 306,
 308,
 309,
 309,
 309,
 310,
 312,
 312,
 312,
 313,
 315,
 317,
 319,
 320,
 321,
 324,
 324,
 325,
 326,
 326,
 326,
 326,
 328,
 329,
 331,
 331,
 332,
 332,
 333,
 333,
 333,
 333,
 333,
 334,
 334,
 336,
 337,
 338,
 338,
 341,
 343,
 345,
 346,
 346,
 346,
 347,
 348,
 348,
 348,
 350,
 350,
 351,
 352,
 352,
 352,
 355,
 359,
 359,
 359,
 360,
 363,
 363,
 363,
 364,
 365,
 371,
 372,
 373,
 375,
 376,
 377,
 377,
 377,
 378,
 379,
 380,
 380,
 381,
 381,
 381,
 384,
 385,
 385,
 386,
 386,
 386,
 388,
 389,
 389,
 389,
 389,
 390,
 393,
 394,
 397,
 399,
 400,
 401,
 403,
 403,
 404,
 404,
 405,
 406,
 408,
 409,
 409,
 410,
 411,
 412,
 414,
 414,
 415,
 415,
 416,
 417,
 417,
 419,
 423,
 426,
 427,
 427,
 427,
 428,
 429,
 429,
 430,
 430,
 430,
 431,
 431,
 431,
 432,
 433,
 439,
 441,
 442,
 442,
 444,
 445,
 446,
 446,
 446,
 446,
 447,
 447,
 447,
 447,
 449,
 451,
 452,
 452,
 455,
 455,
 455,
 456,
 457,
 457,
 457,
 458,
 459,
 459,
 460,
 462,
 463,
 463,
 464,
 464,
 466,
 469,
 473,
 474,
 475,
 475,
 477,
 477,
 478,
 478,
 479,
 482,
 483,
 484,
 489,
 489,
 491,
 491,
 491,
 491,
 491,
 492,
 495,
 495,
 497,
 497,
 498,
 499,
 499,
 500,
 500,
 500,
 501,
 501,
 501,
 501,
 502,
 502,
 502,
 503,
 503,
 504,
 506,
 508,
 508,
 510,
 513,
 513,
 514,
 514,
 515,
 520,
 522,
 523,
 525,
 525,
 525,
 527,
 527,
 527,
 529,
 529,
 529,
 530,
 531,
 535,
 536,
 537,
 537,
 538,
 538,
 541,
 543,
 544,
 546,
 548,
 548,
 548,
 552,
 554,
 555,
 564,
 565,
 565,
 566,
 566,
 568,
 568,
 569,
 570,
 572,
 575,
 575,
 576,
 577,
 579,
 579,
 580,
 580,
 582,
 583,
 585,
 588,
 590,
 592,
 593,
 593,
 593,
 594,
 595,
 596,
 598,
 599,
 599,
 600,
 600,
 602,
 603,
 604,
 604,
 604,
 607,
 610,
 612,
 612,
 613,
 614,
 614,
 614,
 616,
 617,
 618,
 618,
 619,
 619,
 620,
 623,
 624,
 626,
 627,
 627,
 627,
 629,
 629,
 629,
 630,
 632,
 632,
 633,
 633,
 635,
 635,
 635,
 636,
 636,
 636,
 639,
 639,
 640,
 641,
 644,
 644,
 647,
 648,
 650,
 651,
 653,
 654,
 655,
 655,
 656,
 657,
 657,
 658,
 659,
 661,
 661,
 663,
 663,
 665,
 666,
 666,
 672,
 672,
 673,
 673,
 673,
 674,
 675,
 676,
 677,
 677,
 680,
 681,
 684,
 684,
 684,
 684,
 687,
 687,
 688,
 689,
 689,
 691,
 692,
 694,
 696,
 696,
 696,
 697,
 700,
 700,
 702,
 702,
 704,
 704,
 704,
 705,
 706,
 706,
 707,
 708,
 708,
 709,
 710,
 710,
 710,
 710,
 712,
 713,
 713,
 715,
 720,
 720,
 720,
 722,
 723,
 724,
 725,
 725,
 728,
 729,
 729,
 731,
 731,
 731,
 733,
 734,
 736,
 736,
 737,
 738,
 739,
 740,
 740,
 741,
 743,
 745,
 746,
 747,
 750,
 750,
 751,
 751,
 752,
 752,
 753,
 756,
 757,
 757,
 758,
 759,
 759,
 759,
 759,
 760,
 761,
 762,
 762,
 764,
 764,
 764,
 765,
 765,
 766,
 769,
 773,
 773,
 776,
 779,
 780,
 781,
 782,
 782,
 784,
 786,
 787,
 792,
 794,
 794,
 795,
 795,
 795,
 795,
 796,
 799,
 800,
 800,
 801,
 802,
 802,
 802,
 803,
 804,
 805,
 805,
 805,
 808,
 809,
 810,
 810,
 810,
 811,
 812,
 813,
 814,
 818,
 818,
 818,
 820,
 820,
 821,
 822,
 823,
 824,
 825,
 826,
 827,
 828,
 828,
 828,
 830,
 831,
 832,
 833,
 834,
 835,
 835,
 838,
 839,
 842,
 843,
 843,
 844,
 846,
 846,
 847,
 847,
 850,
 852,
 853,
 853,
 853,
 853,
 854,
 854,
 854,
 858,
 858,
 859,
 861,
 862,
 863,
 863,
 864,
 864,
 865,
 866,
 866,
 868,
 868,
 869,
 870,
 871,
 872,
 874,
 874,
 875,
 875,
 876,
 879,
 879,
 880,
 883,
 883,
 883,
 883,
 885,
 886,
 886,
 886,
 886,
 889,
 890,
 890,
 891,
 895,
 895,
 896,
 897,
 897,
 897,
 898,
 899,
 899,
 899,
 902,
 902,
 904,
 904,
 904,
 906,
 906,
 906,
 908,
 911,
 911,
 912,
 913,
 913,
 914,
 914,
 916,
 919,
 919,
 919,
 919,
 921,
 924,
 925,
 926,
 927,
 929,
 930,
 930,
 930,
 932,
 932,
 933,
 934,
 936,
 937,
 939,
 940,
 941,
 941,
 943,
 944,
 944,
 944,
 949,
 949,
 949,
 949,
 950,
 952,
 956,
 957,
 958,
 958,
 959,
 959,
 960,
 960,
 960,
 968,
 969,
 971,
 971,
 972,
 972,
 972,
 972,
 972,
 974,
 975,
 975,
 976,
 977,
 977,
 977,
 978,
 980,
 981,
 986,
 986,
 987,
 997,
 997,
 998,
 999,
 999,
 999,
 1000,
 1000]