diff --git a/src/common.cairo b/src/common.cairo index cf165c02e..b9194eefd 100644 --- a/src/common.cairo +++ b/src/common.cairo @@ -7,6 +7,7 @@ mod math; mod array_print; mod array_extend; mod consts; +mod merge_sort; #[cfg(test)] mod tests; diff --git a/src/common/merge_sort.cairo b/src/common/merge_sort.cairo new file mode 100644 index 000000000..06dafcffa --- /dev/null +++ b/src/common/merge_sort.cairo @@ -0,0 +1,79 @@ +use cairo_verifier::common::array_extend::ArrayExtendTrait; + +// Merge Sort +/// # Arguments +/// * `arr` - Array to sort +/// # Returns +/// * `Array` - Sorted array +fn merge_sort, +Drop, +PartialOrd>(arr: Array) -> Array { + let len = arr.len(); + if len <= 1 { + return arr; + } + + // Create left and right arrays + let middle = len / 2; + let (mut left_arr, mut right_arr) = split_array(arr, middle); + + // Recursively sort the left and right arrays + let mut sorted_left = merge_sort(left_arr); + let mut sorted_right = merge_sort(right_arr); + + let mut result_arr = array![]; + merge_recursive(sorted_left, sorted_right, ref result_arr, 0, 0); + result_arr +} + +// Merge two sorted arrays +/// # Arguments +/// * `left_arr` - Left array +/// * `right_arr` - Right array +/// * `result_arr` - Result array +/// * `left_arr_ix` - Left array index +/// * `right_arr_ix` - Right array index +/// # Returns +/// * `Array` - Sorted array +fn merge_recursive, +Drop, +PartialOrd>( + mut left_arr: Array, + mut right_arr: Array, + ref result_arr: Array, + left_arr_ix: usize, + right_arr_ix: usize +) { + if result_arr.len() == left_arr.len() + right_arr.len() { + return; + } + + if left_arr_ix == left_arr.len() { + result_arr.append(*right_arr[right_arr_ix]); + return merge_recursive(left_arr, right_arr, ref result_arr, left_arr_ix, right_arr_ix + 1); + } + + if right_arr_ix == right_arr.len() { + result_arr.append(*left_arr[left_arr_ix]); + return merge_recursive(left_arr, right_arr, ref result_arr, left_arr_ix + 1, right_arr_ix); + } + + if *left_arr[left_arr_ix] < *right_arr[right_arr_ix] { + result_arr.append(*left_arr[left_arr_ix]); + merge_recursive(left_arr, right_arr, ref result_arr, left_arr_ix + 1, right_arr_ix) + } else { + result_arr.append(*right_arr[right_arr_ix]); + merge_recursive(left_arr, right_arr, ref result_arr, left_arr_ix, right_arr_ix + 1) + } +} + +// Split an array into two arrays. +/// * `arr` - The array to split. +/// * `index` - The index to split the array at. +/// # Returns +/// * `(Array, Array)` - The two arrays. +fn split_array, +Drop>(arr: Array, index: usize) -> (Array, Array) { + let mut arr1 = array![]; + let mut arr2 = array![]; + + arr1.extend(arr.span().slice(0, index)); + arr2.extend(arr.span().slice(index, arr.len() - index)); + + (arr1, arr2) +} diff --git a/src/common/tests.cairo b/src/common/tests.cairo index c851fcf18..529a7c38b 100644 --- a/src/common/tests.cairo +++ b/src/common/tests.cairo @@ -4,3 +4,4 @@ mod test_from_span; mod test_horner_eval; mod test_array_append; mod test_math; +mod test_merge_sort; diff --git a/src/common/tests/test_merge_sort.cairo b/src/common/tests/test_merge_sort.cairo new file mode 100644 index 000000000..ba240020f --- /dev/null +++ b/src/common/tests/test_merge_sort.cairo @@ -0,0 +1,3324 @@ +use cairo_verifier::common::merge_sort::merge_sort; + +#[test] +#[available_gas(9999999999)] +fn test_merge_sort_1() { + let unsorted: Array = array![10, 5, 1, 4, 7, 113]; + assert(merge_sort(unsorted) == array![1, 4, 5, 7, 10, 113], 'Invalid sorting'); +} + +#[test] +#[available_gas(9999999999)] +fn test_merge_sort_2() { + let unsorted: Array = array![ + 416, + 472, + 858, + 828, + 379, + 186, + 90, + 239, + 248, + 549, + 725, + 929, + 40, + 850, + 539, + 416, + 119, + 632, + 593, + 765, + 558, + 431, + 997, + 814, + 35, + 103, + 604, + 833, + 866, + 353, + 571, + 813, + 980, + 814, + 364, + 612, + 225, + 258, + 557, + 292, + 983, + 872, + 513, + 857, + 398, + 163, + 144, + 963, + 781, + 457, + 382, + 557, + 457, + 222, + 466, + 265, + 293, + 50, + 838, + 648, + 570, + 84, + 612, + 830, + 549, + 594, + 689, + 682, + 63, + 585, + 504, + 251, + 674, + 680, + 339, + 486, + 384, + 387, + 791, + 410, + 733, + 383, + 108, + 44, + 268, + 180, + 166, + 299, + 682, + 997, + 54, + 492, + 867, + 948, + 394, + 569, + 809, + 44, + 873, + 186, + 693, + 176, + 680, + 601, + 548, + 810, + 260, + 508, + 194, + 478, + 601, + 131, + 983, + 621, + 929, + 457, + 603, + 780, + 513, + 58, + 47, + 190, + 952, + 63, + 661, + 429, + 894, + 811, + 996, + 477, + 595, + 315, + 981, + 979, + 447, + 603, + 195, + 787, + 218, + 12, + 609, + 718, + 657, + 575, + 766, + 495, + 325, + 391, + 543, + 770, + 203, + 372, + 92, + 433, + 331, + 743, + 680, + 671, + 871, + 178, + 859, + 269, + 71, + 460, + 958, + 110, + 832, + 143, + 724, + 744, + 521, + 327, + 891, + 404, + 80, + 74, + 249, + 25, + 776, + 649, + 726, + 770, + 187, + 814, + 403, + 413, + 809, + 434, + 960, + 71, + 574, + 707, + 758, + 516, + 178, + 302, + 249, + 879, + 503, + 732, + 250, + 725, + 557, + 272, + 798, + 669, + 249, + 837, + 702, + 437, + 978, + 493, + 51, + 561, + 765, + 447, + 569, + 238, + 679, + 926, + 835, + 560, + 620, + 273, + 160, + 918, + 143, + 947, + 175, + 471, + 290, + 792, + 134, + 592, + 251, + 266, + 641, + 313, + 185, + 755, + 277, + 498, + 481, + 951, + 321, + 683, + 908, + 987, + 368, + 148, + 313, + 544, + 49, + 44, + 210, + 341, + 182, + 194, + 999, + 128, + 178, + 527, + 822, + 27, + 537, + 696, + 985, + 618, + 908, + 14, + 408, + 836, + 855, + 694, + 254, + 948, + 225, + 357, + 783, + 485, + 8, + 657, + 795, + 159, + 888, + 74, + 842, + 183, + 473, + 404, + 497, + 238, + 250, + 829, + 430, + 425, + 669, + 1000, + 29, + 255, + 507, + 351, + 268, + 451, + 446, + 233, + 198, + 183, + 16, + 654, + 792, + 679, + 635, + 382, + 131, + 241, + 681, + 687, + 886, + 239, + 238, + 164, + 202, + 237, + 700, + 495, + 835, + 545, + 527, + 467, + 923, + 468, + 932, + 723, + 140, + 603, + 14, + 155, + 168, + 596, + 57, + 225, + 652, + 88, + 295, + 77, + 826, + 324, + 831, + 705, + 718, + 223, + 499, + 846, + 663, + 229, + 103, + 712, + 119, + 824, + 285, + 265, + 179, + 541, + 62, + 524, + 254, + 64, + 461, + 165, + 498, + 922, + 496, + 77, + 934, + 327, + 317, + 731, + 185, + 131, + 715, + 330, + 794, + 344, + 426, + 245, + 55, + 700, + 619, + 659, + 244, + 791, + 814, + 827, + 760, + 883, + 165, + 256, + 582, + 220, + 457, + 331, + 217, + 805, + 949, + 166, + 981, + 677, + 460, + 414, + 420, + 792, + 974, + 171, + 240, + 535, + 148, + 930, + 828, + 305, + 107, + 941, + 164, + 392, + 793, + 964, + 271, + 512, + 513, + 637, + 834, + 541, + 842, + 795, + 351, + 503, + 94, + 86, + 114, + 750, + 43, + 794, + 537, + 938, + 661, + 214, + 733, + 686, + 340, + 614, + 302, + 953, + 551, + 317, + 803, + 237, + 167, + 311, + 876, + 305, + 233, + 673, + 921, + 810, + 419, + 247, + 967, + 78, + 985, + 907, + 250, + 750, + 48, + 287, + 535, + 327, + 211, + 595, + 132, + 732, + 390, + 586, + 757, + 128, + 359, + 308, + 679, + 499, + 456, + 51, + 218, + 995, + 387, + 496, + 900, + 867, + 294, + 218, + 756, + 669, + 122, + 149, + 772, + 978, + 853, + 241, + 297, + 463, + 11, + 497, + 933, + 249, + 978, + 787, + 800, + 247, + 160, + 83, + 35, + 991, + 880, + 397, + 552, + 999, + 367, + 787, + 470, + 783, + 106, + 875, + 904, + 85, + 385, + 664, + 738, + 906, + 994, + 879, + 647, + 75, + 78, + 968, + 568, + 405, + 135, + 490, + 314, + 879, + 703, + 885, + 425, + 158, + 337, + 25, + 486, + 753, + 876, + 300, + 478, + 906, + 400, + 190, + 903, + 397, + 244, + 346, + 909, + 781, + 545, + 163, + 299, + 277, + 999, + 377, + 593, + 265, + 104, + 835, + 758, + 522, + 93, + 914, + 191, + 63, + 83, + 611, + 717, + 61, + 888, + 907, + 206, + 504, + 9, + 755, + 145, + 641, + 788, + 20, + 507, + 400, + 987, + 730, + 642, + 72, + 165, + 642, + 755, + 998, + 607, + 419, + 147, + 909, + 534, + 697, + 520, + 284, + 84, + 44, + 576, + 211, + 974, + 565, + 863, + 557, + 524, + 31, + 526, + 285, + 817, + 116, + 695, + 135, + 86, + 475, + 178, + 861, + 802, + 570, + 868, + 971, + 475, + 793, + 602, + 169, + 363, + 33, + 341, + 880, + 509, + 354, + 217, + 534, + 234, + 868, + 650, + 207, + 63, + 109, + 215, + 715, + 801, + 796, + 835, + 42, + 795, + 977, + 327, + 235, + 450, + 671, + 836, + 474, + 262, + 324, + 381, + 156, + 799, + 782, + 788, + 166, + 118, + 557, + 160, + 42, + 604, + 939, + 152, + 762, + 145, + 763, + 512, + 991, + 231, + 275, + 941, + 393, + 495, + 409, + 560, + 978, + 642, + 705, + 15, + 306, + 257, + 458, + 470, + 428, + 245, + 990, + 737, + 618, + 578, + 749, + 616, + 439, + 859, + 573, + 330, + 198, + 443, + 977, + 556, + 28, + 661, + 980, + 678, + 813, + 911, + 856, + 720, + 270, + 912, + 180, + 671, + 497, + 620, + 365, + 945, + 349, + 218, + 980, + 943, + 487, + 752, + 137, + 317, + 651, + 625, + 14, + 391, + 630, + 744, + 79, + 795, + 783, + 453, + 762, + 517, + 80, + 117, + 492, + 589, + 693, + 996, + 987, + 482, + 518, + 752, + 976, + 861, + 204, + 160, + 142, + 684, + 454, + 353, + 326, + 159, + 435, + 372, + 158, + 911, + 717, + 933, + 831, + 845, + 614, + 833, + 136, + 681, + 822, + 788, + 463, + 161, + 256, + 915, + 231, + 952, + 77, + 548, + 261, + 94, + 134, + 730, + 463, + 907, + 29, + 95, + 202, + 573, + 248, + 895, + 10, + 545, + 967, + 334, + 387, + 238, + 243, + 36, + 659, + 745, + 906, + 771, + 257, + 174, + 138, + 312, + 0, + 46, + 53, + 308, + 12, + 809, + 769, + 998, + 193, + 774, + 877, + 280, + 587, + 447, + 834, + 926, + 954, + 305, + 602, + 70, + 214, + 237, + 613, + 62, + 337, + 25, + 411, + 732, + 707, + 245, + 163, + 214, + 335, + 136, + 66, + 37, + 891, + 875, + 330, + 883, + 907, + 304, + 701, + 197, + 326, + 171, + 943, + 74, + 908, + 930, + 909, + 677, + 540, + 385, + 987, + 89, + 762, + 964, + 969, + 685, + 704, + 854, + 933, + 276, + 862, + 215, + 168, + 119, + 294, + 868, + 473, + 555, + 83, + 23, + 853, + 468, + 351, + 71, + 623, + 343, + 674, + 468, + 710, + 85, + 99, + 11, + 278, + 531, + 404, + 348, + 68, + 983, + 911, + 133, + 739, + 515, + 41, + 900, + 151, + 857, + 35, + 647, + 358, + 800, + 179, + 335, + 755, + 818, + 44, + 375, + 693, + 830, + 571, + 825, + 364, + 959, + 537, + 146, + 108, + 565, + 795, + 705, + 783, + 43, + 572, + 212, + 715, + 882, + 332, + 435, + 806, + 46, + 549, + 245, + 588, + 901, + 470, + 29, + 779, + 703, + 582, + 891, + 810, + 546, + 359, + 296, + 815, + 548, + 336, + 76, + 629, + 462, + 647, + 497, + 545, + 530, + 9, + 397, + 315, + 619, + 963, + 90, + 712, + 148, + 863, + 644, + 132, + 66, + 399, + 759, + 525 + ]; + assert( + merge_sort( + unsorted + ) == array![ + 0, + 8, + 9, + 9, + 10, + 11, + 11, + 12, + 12, + 14, + 14, + 14, + 15, + 16, + 20, + 23, + 25, + 25, + 25, + 27, + 28, + 29, + 29, + 29, + 31, + 33, + 35, + 35, + 35, + 36, + 37, + 40, + 41, + 42, + 42, + 43, + 43, + 44, + 44, + 44, + 44, + 44, + 46, + 46, + 47, + 48, + 49, + 50, + 51, + 51, + 53, + 54, + 55, + 57, + 58, + 61, + 62, + 62, + 63, + 63, + 63, + 63, + 64, + 66, + 66, + 68, + 70, + 71, + 71, + 71, + 72, + 74, + 74, + 74, + 75, + 76, + 77, + 77, + 77, + 78, + 78, + 79, + 80, + 80, + 83, + 83, + 83, + 84, + 84, + 85, + 85, + 86, + 86, + 88, + 89, + 90, + 90, + 92, + 93, + 94, + 94, + 95, + 99, + 103, + 103, + 104, + 106, + 107, + 108, + 108, + 109, + 110, + 114, + 116, + 117, + 118, + 119, + 119, + 119, + 122, + 128, + 128, + 131, + 131, + 131, + 132, + 132, + 133, + 134, + 134, + 135, + 135, + 136, + 136, + 137, + 138, + 140, + 142, + 143, + 143, + 144, + 145, + 145, + 146, + 147, + 148, + 148, + 148, + 149, + 151, + 152, + 155, + 156, + 158, + 158, + 159, + 159, + 160, + 160, + 160, + 160, + 161, + 163, + 163, + 163, + 164, + 164, + 165, + 165, + 165, + 166, + 166, + 166, + 167, + 168, + 168, + 169, + 171, + 171, + 174, + 175, + 176, + 178, + 178, + 178, + 178, + 179, + 179, + 180, + 180, + 182, + 183, + 183, + 185, + 185, + 186, + 186, + 187, + 190, + 190, + 191, + 193, + 194, + 194, + 195, + 197, + 198, + 198, + 202, + 202, + 203, + 204, + 206, + 207, + 210, + 211, + 211, + 212, + 214, + 214, + 214, + 215, + 215, + 217, + 217, + 218, + 218, + 218, + 218, + 220, + 222, + 223, + 225, + 225, + 225, + 229, + 231, + 231, + 233, + 233, + 234, + 235, + 237, + 237, + 237, + 238, + 238, + 238, + 238, + 239, + 239, + 240, + 241, + 241, + 243, + 244, + 244, + 245, + 245, + 245, + 245, + 247, + 247, + 248, + 248, + 249, + 249, + 249, + 249, + 250, + 250, + 250, + 251, + 251, + 254, + 254, + 255, + 256, + 256, + 257, + 257, + 258, + 260, + 261, + 262, + 265, + 265, + 265, + 266, + 268, + 268, + 269, + 270, + 271, + 272, + 273, + 275, + 276, + 277, + 277, + 278, + 280, + 284, + 285, + 285, + 287, + 290, + 292, + 293, + 294, + 294, + 295, + 296, + 297, + 299, + 299, + 300, + 302, + 302, + 304, + 305, + 305, + 305, + 306, + 308, + 308, + 311, + 312, + 313, + 313, + 314, + 315, + 315, + 317, + 317, + 317, + 321, + 324, + 324, + 325, + 326, + 326, + 327, + 327, + 327, + 327, + 330, + 330, + 330, + 331, + 331, + 332, + 334, + 335, + 335, + 336, + 337, + 337, + 339, + 340, + 341, + 341, + 343, + 344, + 346, + 348, + 349, + 351, + 351, + 351, + 353, + 353, + 354, + 357, + 358, + 359, + 359, + 363, + 364, + 364, + 365, + 367, + 368, + 372, + 372, + 375, + 377, + 379, + 381, + 382, + 382, + 383, + 384, + 385, + 385, + 387, + 387, + 387, + 390, + 391, + 391, + 392, + 393, + 394, + 397, + 397, + 397, + 398, + 399, + 400, + 400, + 403, + 404, + 404, + 404, + 405, + 408, + 409, + 410, + 411, + 413, + 414, + 416, + 416, + 419, + 419, + 420, + 425, + 425, + 426, + 428, + 429, + 430, + 431, + 433, + 434, + 435, + 435, + 437, + 439, + 443, + 446, + 447, + 447, + 447, + 450, + 451, + 453, + 454, + 456, + 457, + 457, + 457, + 457, + 458, + 460, + 460, + 461, + 462, + 463, + 463, + 463, + 466, + 467, + 468, + 468, + 468, + 470, + 470, + 470, + 471, + 472, + 473, + 473, + 474, + 475, + 475, + 477, + 478, + 478, + 481, + 482, + 485, + 486, + 486, + 487, + 490, + 492, + 492, + 493, + 495, + 495, + 495, + 496, + 496, + 497, + 497, + 497, + 497, + 498, + 498, + 499, + 499, + 503, + 503, + 504, + 504, + 507, + 507, + 508, + 509, + 512, + 512, + 513, + 513, + 513, + 515, + 516, + 517, + 518, + 520, + 521, + 522, + 524, + 524, + 525, + 526, + 527, + 527, + 530, + 531, + 534, + 534, + 535, + 535, + 537, + 537, + 537, + 539, + 540, + 541, + 541, + 543, + 544, + 545, + 545, + 545, + 545, + 546, + 548, + 548, + 548, + 549, + 549, + 549, + 551, + 552, + 555, + 556, + 557, + 557, + 557, + 557, + 557, + 558, + 560, + 560, + 561, + 565, + 565, + 568, + 569, + 569, + 570, + 570, + 571, + 571, + 572, + 573, + 573, + 574, + 575, + 576, + 578, + 582, + 582, + 585, + 586, + 587, + 588, + 589, + 592, + 593, + 593, + 594, + 595, + 595, + 596, + 601, + 601, + 602, + 602, + 603, + 603, + 603, + 604, + 604, + 607, + 609, + 611, + 612, + 612, + 613, + 614, + 614, + 616, + 618, + 618, + 619, + 619, + 620, + 620, + 621, + 623, + 625, + 629, + 630, + 632, + 635, + 637, + 641, + 641, + 642, + 642, + 642, + 644, + 647, + 647, + 647, + 648, + 649, + 650, + 651, + 652, + 654, + 657, + 657, + 659, + 659, + 661, + 661, + 661, + 663, + 664, + 669, + 669, + 669, + 671, + 671, + 671, + 673, + 674, + 674, + 677, + 677, + 678, + 679, + 679, + 679, + 680, + 680, + 680, + 681, + 681, + 682, + 682, + 683, + 684, + 685, + 686, + 687, + 689, + 693, + 693, + 693, + 694, + 695, + 696, + 697, + 700, + 700, + 701, + 702, + 703, + 703, + 704, + 705, + 705, + 705, + 707, + 707, + 710, + 712, + 712, + 715, + 715, + 715, + 717, + 717, + 718, + 718, + 720, + 723, + 724, + 725, + 725, + 726, + 730, + 730, + 731, + 732, + 732, + 732, + 733, + 733, + 737, + 738, + 739, + 743, + 744, + 744, + 745, + 749, + 750, + 750, + 752, + 752, + 753, + 755, + 755, + 755, + 755, + 756, + 757, + 758, + 758, + 759, + 760, + 762, + 762, + 762, + 763, + 765, + 765, + 766, + 769, + 770, + 770, + 771, + 772, + 774, + 776, + 779, + 780, + 781, + 781, + 782, + 783, + 783, + 783, + 783, + 787, + 787, + 787, + 788, + 788, + 788, + 791, + 791, + 792, + 792, + 792, + 793, + 793, + 794, + 794, + 795, + 795, + 795, + 795, + 795, + 796, + 798, + 799, + 800, + 800, + 801, + 802, + 803, + 805, + 806, + 809, + 809, + 809, + 810, + 810, + 810, + 811, + 813, + 813, + 814, + 814, + 814, + 814, + 815, + 817, + 818, + 822, + 822, + 824, + 825, + 826, + 827, + 828, + 828, + 829, + 830, + 830, + 831, + 831, + 832, + 833, + 833, + 834, + 834, + 835, + 835, + 835, + 835, + 836, + 836, + 837, + 838, + 842, + 842, + 845, + 846, + 850, + 853, + 853, + 854, + 855, + 856, + 857, + 857, + 858, + 859, + 859, + 861, + 861, + 862, + 863, + 863, + 866, + 867, + 867, + 868, + 868, + 868, + 871, + 872, + 873, + 875, + 875, + 876, + 876, + 877, + 879, + 879, + 879, + 880, + 880, + 882, + 883, + 883, + 885, + 886, + 888, + 888, + 891, + 891, + 891, + 894, + 895, + 900, + 900, + 901, + 903, + 904, + 906, + 906, + 906, + 907, + 907, + 907, + 907, + 908, + 908, + 908, + 909, + 909, + 909, + 911, + 911, + 911, + 912, + 914, + 915, + 918, + 921, + 922, + 923, + 926, + 926, + 929, + 929, + 930, + 930, + 932, + 933, + 933, + 933, + 934, + 938, + 939, + 941, + 941, + 943, + 943, + 945, + 947, + 948, + 948, + 949, + 951, + 952, + 952, + 953, + 954, + 958, + 959, + 960, + 963, + 963, + 964, + 964, + 967, + 967, + 968, + 969, + 971, + 974, + 974, + 976, + 977, + 977, + 978, + 978, + 978, + 978, + 979, + 980, + 980, + 980, + 981, + 981, + 983, + 983, + 983, + 985, + 985, + 987, + 987, + 987, + 987, + 990, + 991, + 991, + 994, + 995, + 996, + 996, + 997, + 997, + 998, + 998, + 999, + 999, + 999, + 1000 + ], + 'Invalid sorting' + ); +} + +#[test] +#[available_gas(9999999999)] +fn test_merge_sort_3() { + let unsorted: Array = array![ + 171124, + 848673, + 96815, + 694614, + 411, + 812243, + 356582, + 488291, + 901109, + 751917, + 899954, + 283887, + 429084, + 364262, + 868167, + 410591, + 904025, + 424133, + 949798, + 257865, + 215677, + 105227, + 112203, + 111025, + 78627, + 378886, + 153293, + 901063, + 47733, + 76442, + 687772, + 840247, + 205703, + 175767, + 323520, + 628979, + 648866, + 447851, + 268413, + 486431, + 588478, + 188615, + 320235, + 181123, + 96582, + 38533, + 997105, + 638948, + 610810, + 731886, + 207899, + 288235, + 126454, + 460712, + 89579, + 765900, + 889015, + 350582, + 879592, + 383799, + 359055, + 250337, + 252022, + 73851, + 239432, + 936208, + 284043, + 633196, + 32267, + 676981, + 417950, + 490022, + 655038, + 387019, + 996386, + 366345, + 436780, + 637320, + 360087, + 653544, + 979412, + 896833, + 63606, + 886384, + 273581, + 729574, + 471005, + 608173, + 570965, + 245237, + 506744, + 724582, + 31182, + 900324, + 615128, + 321769, + 620341, + 112626, + 889131, + 117817, + 505813, + 529691, + 44585, + 823983, + 858726, + 786705, + 309197, + 311860, + 844942, + 488708, + 401647, + 726268, + 517667, + 902851, + 974989, + 431594, + 523133, + 492691, + 703437, + 744990, + 483420, + 661763, + 564914, + 479331, + 740366, + 784283, + 869737, + 353092, + 850118, + 556207, + 184374, + 860421, + 93489, + 658032, + 445350, + 348534, + 688419, + 214009, + 535137, + 726877, + 939348, + 344230, + 828262, + 502610, + 801554, + 139386, + 39242, + 238240, + 850869, + 490978, + 729848, + 733247, + 576518, + 614496, + 281960, + 877191, + 210456, + 476560, + 146552, + 921220, + 405717, + 71657, + 742281, + 992710, + 922014, + 648990, + 920210, + 377382, + 320465, + 115296, + 22303, + 294702, + 877280, + 86239, + 843896, + 831412, + 114131, + 961344, + 860067, + 956855, + 330777, + 234680, + 64973, + 406390, + 452860, + 823839, + 66367, + 384838, + 478806, + 610549, + 439246, + 665414, + 271572, + 3654, + 723790, + 767830, + 466697, + 409263, + 414896, + 517141, + 569797, + 558364, + 999854, + 549777, + 354455, + 913904, + 722118, + 849660, + 233566, + 581528, + 590797, + 196682, + 970587, + 93989, + 453016, + 609821, + 35989, + 775542, + 843276, + 213406, + 703340, + 660369, + 61692, + 336442, + 782469, + 252665, + 652295, + 869504, + 986033, + 221488, + 139223, + 224606, + 153515, + 68565, + 997568, + 897473, + 750058, + 406207, + 89965, + 168034, + 339159, + 278911, + 937668, + 470514, + 591940, + 119639, + 32649, + 434082, + 904444, + 226957, + 613884, + 989906, + 712893, + 401875, + 442601, + 876313, + 443110, + 71663, + 677288, + 208131, + 252518, + 221790, + 965260, + 341580, + 778648, + 481708, + 120743, + 323083, + 755122, + 466361, + 803170, + 879144, + 50037, + 513421, + 250092, + 558912, + 403830, + 797781, + 626042, + 865765, + 80062, + 847173, + 44185, + 139202, + 630657, + 164587, + 551000, + 821908, + 944769, + 605216, + 690446, + 62824, + 215682, + 555103, + 176101, + 675082, + 226619, + 145262, + 418927, + 952771, + 380661, + 171913, + 522621, + 294502, + 151117, + 594687, + 690518, + 522663, + 294537, + 840379, + 390839, + 987709, + 177036, + 123058, + 401612, + 698004, + 198162, + 709209, + 157910, + 456929, + 756488, + 562276, + 138485, + 856677, + 349310, + 618025, + 839114, + 909987, + 923937, + 969611, + 76864, + 586615, + 183075, + 845224, + 320974, + 896884, + 485554, + 604297, + 93850, + 958120, + 497222, + 381298, + 133426, + 189291, + 541295, + 96292, + 612552, + 831035, + 675275, + 120792, + 496444, + 609771, + 461197, + 761876, + 966628, + 949387, + 345231, + 831559, + 741747, + 770851, + 15908, + 772462, + 242736, + 867003, + 578971, + 841055, + 982535, + 453349, + 171641, + 550163, + 113091, + 25026, + 801602, + 244203, + 972476, + 693125, + 746482, + 805026, + 463449, + 407129, + 315250, + 680930, + 402709, + 140858, + 952014, + 263346, + 348382, + 389258, + 181257, + 752625, + 389924, + 807576, + 856318, + 383878, + 282575, + 672726, + 43671, + 688143, + 381525, + 798581, + 763406, + 16984, + 563319, + 661046, + 722773, + 161110, + 376849, + 533588, + 582993, + 49425, + 262027, + 756176, + 103156, + 275051, + 860478, + 253129, + 620203, + 679447, + 263025, + 378219, + 619733, + 67104, + 360977, + 947409, + 70501, + 591458, + 603836, + 233251, + 388961, + 717462, + 731751, + 55673, + 666160, + 668716, + 315019, + 568347, + 442715, + 425132, + 365147, + 762487, + 689003, + 97501, + 103506, + 964256, + 572901, + 882213, + 192027, + 901889, + 783449, + 128646, + 159741, + 619705, + 188662, + 345276, + 149451, + 505569, + 722683, + 545237, + 304870, + 336512, + 339180, + 334201, + 850324, + 111146, + 297101, + 48964, + 331458, + 287422, + 358270, + 494542, + 675023, + 973005, + 562204, + 735987, + 29015, + 810041, + 217564, + 96294, + 771930, + 251919, + 25642, + 520577, + 19470, + 361153, + 315503, + 583383, + 6164, + 672442, + 431660, + 631110, + 366340, + 762768, + 960396, + 714961, + 525308, + 552456, + 461296, + 238108, + 221531, + 702196, + 991865, + 330719, + 387682, + 249852, + 565112, + 852710, + 21740, + 504437, + 102425, + 998838, + 838700, + 609728, + 937685, + 915175, + 459360, + 378235, + 838252, + 202703, + 848978, + 671275, + 914404, + 150645, + 88347, + 93360, + 84225, + 314232, + 423833, + 182690, + 991804, + 854067, + 984790, + 188487, + 415091, + 448688, + 516857, + 691150, + 565601, + 533194, + 311960, + 701985, + 342918, + 934491, + 105984, + 693102, + 155324, + 506306, + 723631, + 439568, + 489215, + 271599, + 493654, + 862597, + 790107, + 544968, + 359845, + 229072, + 436296, + 207494, + 69472, + 581210, + 88687, + 969121, + 984556, + 808223, + 713438, + 373891, + 201370, + 576734, + 188726, + 500189, + 410946, + 212397, + 286683, + 71346, + 784282, + 962295, + 775800, + 905102, + 594173, + 992833, + 530818, + 608373, + 461320, + 244415, + 557409, + 549102, + 204006, + 172449, + 596565, + 153299, + 34928, + 222013, + 86689, + 456166, + 782114, + 318184, + 153580, + 396392, + 304713, + 148687, + 535757, + 385571, + 124686, + 23942, + 167821, + 142000, + 543344, + 589925, + 863872, + 228089, + 56136, + 735452, + 628189, + 634433, + 194262, + 169468, + 51408, + 652812, + 665195, + 463774, + 247236, + 296325, + 516190, + 767819, + 954630, + 94746, + 707360, + 194742, + 571995, + 68438, + 506762, + 420133, + 894118, + 141699, + 157160, + 556211, + 808003, + 954908, + 723240, + 810505, + 553569, + 513716, + 651919, + 219447 + ]; + assert( + merge_sort( + unsorted + ) == array![ + 411, + 3654, + 6164, + 15908, + 16984, + 19470, + 21740, + 22303, + 23942, + 25026, + 25642, + 29015, + 31182, + 32267, + 32649, + 34928, + 35989, + 38533, + 39242, + 43671, + 44185, + 44585, + 47733, + 48964, + 49425, + 50037, + 51408, + 55673, + 56136, + 61692, + 62824, + 63606, + 64973, + 66367, + 67104, + 68438, + 68565, + 69472, + 70501, + 71346, + 71657, + 71663, + 73851, + 76442, + 76864, + 78627, + 80062, + 84225, + 86239, + 86689, + 88347, + 88687, + 89579, + 89965, + 93360, + 93489, + 93850, + 93989, + 94746, + 96292, + 96294, + 96582, + 96815, + 97501, + 102425, + 103156, + 103506, + 105227, + 105984, + 111025, + 111146, + 112203, + 112626, + 113091, + 114131, + 115296, + 117817, + 119639, + 120743, + 120792, + 123058, + 124686, + 126454, + 128646, + 133426, + 138485, + 139202, + 139223, + 139386, + 140858, + 141699, + 142000, + 145262, + 146552, + 148687, + 149451, + 150645, + 151117, + 153293, + 153299, + 153515, + 153580, + 155324, + 157160, + 157910, + 159741, + 161110, + 164587, + 167821, + 168034, + 169468, + 171124, + 171641, + 171913, + 172449, + 175767, + 176101, + 177036, + 181123, + 181257, + 182690, + 183075, + 184374, + 188487, + 188615, + 188662, + 188726, + 189291, + 192027, + 194262, + 194742, + 196682, + 198162, + 201370, + 202703, + 204006, + 205703, + 207494, + 207899, + 208131, + 210456, + 212397, + 213406, + 214009, + 215677, + 215682, + 217564, + 219447, + 221488, + 221531, + 221790, + 222013, + 224606, + 226619, + 226957, + 228089, + 229072, + 233251, + 233566, + 234680, + 238108, + 238240, + 239432, + 242736, + 244203, + 244415, + 245237, + 247236, + 249852, + 250092, + 250337, + 251919, + 252022, + 252518, + 252665, + 253129, + 257865, + 262027, + 263025, + 263346, + 268413, + 271572, + 271599, + 273581, + 275051, + 278911, + 281960, + 282575, + 283887, + 284043, + 286683, + 287422, + 288235, + 294502, + 294537, + 294702, + 296325, + 297101, + 304713, + 304870, + 309197, + 311860, + 311960, + 314232, + 315019, + 315250, + 315503, + 318184, + 320235, + 320465, + 320974, + 321769, + 323083, + 323520, + 330719, + 330777, + 331458, + 334201, + 336442, + 336512, + 339159, + 339180, + 341580, + 342918, + 344230, + 345231, + 345276, + 348382, + 348534, + 349310, + 350582, + 353092, + 354455, + 356582, + 358270, + 359055, + 359845, + 360087, + 360977, + 361153, + 364262, + 365147, + 366340, + 366345, + 373891, + 376849, + 377382, + 378219, + 378235, + 378886, + 380661, + 381298, + 381525, + 383799, + 383878, + 384838, + 385571, + 387019, + 387682, + 388961, + 389258, + 389924, + 390839, + 396392, + 401612, + 401647, + 401875, + 402709, + 403830, + 405717, + 406207, + 406390, + 407129, + 409263, + 410591, + 410946, + 414896, + 415091, + 417950, + 418927, + 420133, + 423833, + 424133, + 425132, + 429084, + 431594, + 431660, + 434082, + 436296, + 436780, + 439246, + 439568, + 442601, + 442715, + 443110, + 445350, + 447851, + 448688, + 452860, + 453016, + 453349, + 456166, + 456929, + 459360, + 460712, + 461197, + 461296, + 461320, + 463449, + 463774, + 466361, + 466697, + 470514, + 471005, + 476560, + 478806, + 479331, + 481708, + 483420, + 485554, + 486431, + 488291, + 488708, + 489215, + 490022, + 490978, + 492691, + 493654, + 494542, + 496444, + 497222, + 500189, + 502610, + 504437, + 505569, + 505813, + 506306, + 506744, + 506762, + 513421, + 513716, + 516190, + 516857, + 517141, + 517667, + 520577, + 522621, + 522663, + 523133, + 525308, + 529691, + 530818, + 533194, + 533588, + 535137, + 535757, + 541295, + 543344, + 544968, + 545237, + 549102, + 549777, + 550163, + 551000, + 552456, + 553569, + 555103, + 556207, + 556211, + 557409, + 558364, + 558912, + 562204, + 562276, + 563319, + 564914, + 565112, + 565601, + 568347, + 569797, + 570965, + 571995, + 572901, + 576518, + 576734, + 578971, + 581210, + 581528, + 582993, + 583383, + 586615, + 588478, + 589925, + 590797, + 591458, + 591940, + 594173, + 594687, + 596565, + 603836, + 604297, + 605216, + 608173, + 608373, + 609728, + 609771, + 609821, + 610549, + 610810, + 612552, + 613884, + 614496, + 615128, + 618025, + 619705, + 619733, + 620203, + 620341, + 626042, + 628189, + 628979, + 630657, + 631110, + 633196, + 634433, + 637320, + 638948, + 648866, + 648990, + 651919, + 652295, + 652812, + 653544, + 655038, + 658032, + 660369, + 661046, + 661763, + 665195, + 665414, + 666160, + 668716, + 671275, + 672442, + 672726, + 675023, + 675082, + 675275, + 676981, + 677288, + 679447, + 680930, + 687772, + 688143, + 688419, + 689003, + 690446, + 690518, + 691150, + 693102, + 693125, + 694614, + 698004, + 701985, + 702196, + 703340, + 703437, + 707360, + 709209, + 712893, + 713438, + 714961, + 717462, + 722118, + 722683, + 722773, + 723240, + 723631, + 723790, + 724582, + 726268, + 726877, + 729574, + 729848, + 731751, + 731886, + 733247, + 735452, + 735987, + 740366, + 741747, + 742281, + 744990, + 746482, + 750058, + 751917, + 752625, + 755122, + 756176, + 756488, + 761876, + 762487, + 762768, + 763406, + 765900, + 767819, + 767830, + 770851, + 771930, + 772462, + 775542, + 775800, + 778648, + 782114, + 782469, + 783449, + 784282, + 784283, + 786705, + 790107, + 797781, + 798581, + 801554, + 801602, + 803170, + 805026, + 807576, + 808003, + 808223, + 810041, + 810505, + 812243, + 821908, + 823839, + 823983, + 828262, + 831035, + 831412, + 831559, + 838252, + 838700, + 839114, + 840247, + 840379, + 841055, + 843276, + 843896, + 844942, + 845224, + 847173, + 848673, + 848978, + 849660, + 850118, + 850324, + 850869, + 852710, + 854067, + 856318, + 856677, + 858726, + 860067, + 860421, + 860478, + 862597, + 863872, + 865765, + 867003, + 868167, + 869504, + 869737, + 876313, + 877191, + 877280, + 879144, + 879592, + 882213, + 886384, + 889015, + 889131, + 894118, + 896833, + 896884, + 897473, + 899954, + 900324, + 901063, + 901109, + 901889, + 902851, + 904025, + 904444, + 905102, + 909987, + 913904, + 914404, + 915175, + 920210, + 921220, + 922014, + 923937, + 934491, + 936208, + 937668, + 937685, + 939348, + 944769, + 947409, + 949387, + 949798, + 952014, + 952771, + 954630, + 954908, + 956855, + 958120, + 960396, + 961344, + 962295, + 964256, + 965260, + 966628, + 969121, + 969611, + 970587, + 972476, + 973005, + 974989, + 979412, + 982535, + 984556, + 984790, + 986033, + 987709, + 989906, + 991804, + 991865, + 992710, + 992833, + 996386, + 997105, + 997568, + 998838, + 999854 + ], + 'Invalid sorting' + ); +}