├── LICENSE ├── README.md ├── fasc_1_mmix_chap_1_basic_concepts ├── sec_1.3.2_the_mmix_assembly_language │ ├── CMakeLists.txt │ ├── program_h_hail_the_world.c │ ├── program_m_find_the_maximum.c │ ├── program_m_find_the_maximum.test.sh │ └── program_p_print_table_of_500_primes.c └── sec_1.4.2_coroutines │ ├── CMakeLists.txt │ ├── README.md │ ├── data │ └── inout │ │ ├── in.0.txt │ │ ├── in.1.txt │ │ ├── in.2.txt │ │ ├── in.3.txt │ │ ├── out.0.txt │ │ ├── out.1.txt │ │ ├── out.2.txt │ │ └── out.3.txt │ ├── inout_c.coroutinestruct.c │ ├── inout_c.onefunc.c │ └── inout_cplusplus.cpp ├── tools ├── README.md ├── gendata.sh ├── table_5.5.1_n_1000.mmix.txt ├── table_5.5.1_n_1000.taocp.txt └── texttobinary.sh ├── vol_1_fundamental_algorithms_chap_2_information_structures ├── sec_2.2.3_linked_allocation │ ├── CMakeLists.txt │ ├── algorithm_t_topological_sort.c │ └── data │ │ └── algorithm_t_topological_sort │ │ ├── in.0.le.dat │ │ ├── in.0.txt │ │ ├── in.1.le.dat │ │ ├── in.1.txt │ │ ├── in.2.le.dat │ │ ├── in.2.txt │ │ ├── out.0.le.dat │ │ ├── out.1.le.dat │ │ ├── out.2.le.dat │ │ └── out.2.txt └── sec_2.2.4_circular_lists │ ├── CMakeLists.txt │ ├── algorithm_a_addition_of_polynomials.c │ ├── algorithm_m_multiplication_of_polynomials.c │ ├── data │ ├── algorithm_a_addition_of_polynomials │ │ ├── in.0.le.dat │ │ ├── in.0.txt │ │ ├── in.1.le.dat │ │ ├── in.1.txt │ │ ├── out.0.le.dat │ │ └── out.1.le.dat │ └── algorithm_m_multiplication_of_polynomials │ │ ├── in.0.le.dat │ │ ├── in.0.txt │ │ ├── in.1.le.dat │ │ ├── in.1.txt │ │ ├── in.2.le.dat │ │ ├── in.2.txt │ │ ├── out.0.le.dat │ │ ├── out.1.le.dat │ │ └── out.2.le.dat │ ├── polynomial.c │ └── polynomial.h └── vol_3_sorting_and_searching_chap_5_sorting ├── sec_5.2.1_sorting_by_insertion ├── CMakeLists.txt ├── algorithm_d_shellsort.c ├── algorithm_l_list_insertion.c ├── algorithm_m_multiple_list_insertion.c ├── algorithm_s_straight_insertion_sort.c └── data │ ├── algorithm_d_shellsort │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ ├── in.1.txt │ ├── out.0.le.dat │ └── out.1.le.dat │ ├── algorithm_l_list_insertion │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ └── in.1.txt │ ├── algorithm_m_multiple_list_insertion │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ └── in.1.txt │ └── algorithm_s_straight_insertion_sort │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ └── in.1.txt ├── sec_5.2.2_sorting_by_exchanging ├── CMakeLists.txt ├── algorithm_b_bubble_sort.c ├── algorithm_m_merge_exchange.c ├── algorithm_q_quicksort.c ├── algorithm_q_quicksort.recursive.c ├── algorithm_r_radix_exchange_sort.c ├── algorithm_r_radix_exchange_sort.recursive.c └── data │ ├── algorithm_b_bubble_sort │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ ├── in.1.txt │ ├── in.2.le.dat │ ├── in.2.txt │ ├── in.3.le.dat │ ├── in.3.txt │ ├── in.4.le.dat │ ├── in.4.txt │ ├── out.0.le.dat │ ├── out.1.le.dat │ ├── out.2.le.dat │ ├── out.3.le.dat │ └── out.4.le.dat │ ├── algorithm_m_merge_exchange │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ ├── in.1.txt │ ├── in.2.le.dat │ ├── in.2.txt │ ├── in.3.le.dat │ ├── in.3.txt │ ├── out.0.le.dat │ ├── out.1.le.dat │ ├── out.2.le.dat │ └── out.3.le.dat │ ├── algorithm_q_quicksort.recursive │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ ├── in.1.txt │ ├── in.2.le.dat │ ├── in.2.txt │ ├── in.3.le.dat │ ├── in.3.txt │ ├── in.4.le.dat │ ├── in.4.txt │ ├── out.0.le.dat │ ├── out.1.le.dat │ ├── out.2.le.dat │ ├── out.3.le.dat │ └── out.4.le.dat │ ├── algorithm_q_quicksort │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ ├── in.1.txt │ ├── in.2.le.dat │ ├── in.2.txt │ ├── in.3.le.dat │ ├── in.3.txt │ ├── in.4.le.dat │ ├── in.4.txt │ ├── out.0.le.dat │ ├── out.1.le.dat │ ├── out.2.le.dat │ ├── out.3.le.dat │ └── out.4.le.dat │ ├── algorithm_r_radix_exchange_sort.recursive │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ ├── in.1.txt │ ├── in.2.le.dat │ ├── in.2.txt │ ├── in.3.le.dat │ ├── in.3.txt │ ├── in.4.le.dat │ ├── in.4.txt │ ├── out.0.le.dat │ ├── out.1.le.dat │ ├── out.2.le.dat │ ├── out.3.le.dat │ └── out.4.le.dat │ └── algorithm_r_radix_exchange_sort │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ ├── in.1.txt │ ├── in.2.le.dat │ ├── in.2.txt │ ├── in.3.le.dat │ ├── in.3.txt │ ├── in.4.le.dat │ ├── in.4.txt │ ├── out.0.le.dat │ ├── out.1.le.dat │ ├── out.2.le.dat │ ├── out.3.le.dat │ └── out.4.le.dat ├── sec_5.2.3_sorting_by_selection ├── CMakeLists.txt ├── algorithm_s_straight_selection_sort.c └── data │ └── algorithm_s_straight_selection_sort │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ ├── in.1.txt │ ├── in.2.le.dat │ ├── in.2.txt │ ├── in.3.le.dat │ ├── in.3.txt │ ├── in.4.le.dat │ ├── in.4.txt │ ├── in.5.le.dat │ ├── in.5.txt │ ├── in.6.le.dat │ ├── in.6.txt │ ├── out.0.le.dat │ ├── out.1.le.dat │ ├── out.2.le.dat │ ├── out.3.le.dat │ ├── out.4.le.dat │ ├── out.5.dat │ └── out.6.dat ├── sec_5.2.4_sorting_by_merging ├── CMakeLists.txt ├── algorithm_l_list_merge_sort.c ├── algorithm_l_list_merge_sort.signbit.c ├── algorithm_m_two_way_merge.c ├── algorithm_n_natural_two_way_merge_sort.c ├── algorithm_s_straight_two_way_merge_sort.c └── data │ ├── algorithm_l_list_merge_sort.signbit │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ ├── in.1.txt │ ├── in.2.le.dat │ ├── in.2.txt │ ├── in.3.le.dat │ ├── in.3.txt │ ├── in.4.le.dat │ ├── in.4.txt │ ├── in.5.le.dat │ ├── in.5.txt │ ├── in.6.le.dat │ ├── in.6.txt │ ├── out.0.le.dat │ ├── out.1.le.dat │ ├── out.2.le.dat │ ├── out.3.le.dat │ ├── out.4.le.dat │ ├── out.5.dat │ └── out.6.dat │ ├── algorithm_l_list_merge_sort │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ ├── in.1.txt │ ├── in.2.le.dat │ ├── in.2.txt │ ├── in.3.le.dat │ ├── in.3.txt │ ├── in.4.le.dat │ ├── in.4.txt │ ├── in.5.le.dat │ ├── in.5.txt │ ├── in.6.le.dat │ ├── in.6.txt │ ├── out.0.le.dat │ ├── out.1.le.dat │ ├── out.2.le.dat │ ├── out.3.le.dat │ ├── out.4.le.dat │ ├── out.5.dat │ └── out.6.dat │ ├── algorithm_m_two_way_merge │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ ├── in.1.txt │ ├── in.2.le.dat │ ├── in.2.txt │ ├── in.3.le.dat │ ├── in.3.txt │ ├── in.4.le.dat │ ├── in.4.txt │ ├── in.5.le.dat │ ├── in.5.txt │ ├── in.6.le.dat │ └── in.6.txt │ ├── algorithm_n_natural_two_way_merge_sort │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ ├── in.1.txt │ ├── in.2.le.dat │ ├── in.2.txt │ ├── in.3.le.dat │ ├── in.3.txt │ ├── in.4.le.dat │ ├── in.4.txt │ ├── in.5.le.dat │ ├── in.5.txt │ ├── in.6.le.dat │ ├── in.6.txt │ ├── out.0.le.dat │ ├── out.1.le.dat │ ├── out.2.le.dat │ ├── out.3.le.dat │ ├── out.4.le.dat │ ├── out.5.dat │ └── out.6.dat │ └── algorithm_s_straight_two_way_merge_sort │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ ├── in.1.txt │ ├── in.2.le.dat │ ├── in.2.txt │ ├── in.3.le.dat │ ├── in.3.txt │ ├── in.4.le.dat │ ├── in.4.txt │ ├── in.5.le.dat │ ├── in.5.txt │ ├── in.6.le.dat │ ├── in.6.txt │ ├── out.0.le.dat │ ├── out.1.le.dat │ ├── out.2.le.dat │ ├── out.3.le.dat │ ├── out.4.le.dat │ ├── out.5.dat │ └── out.6.dat ├── sec_5.2.5_sorting_by_distribution ├── CMakeLists.txt ├── algorithm_r_radix_list_sort.c └── data │ └── algorithm_r_radix_list_sort │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ ├── in.1.txt │ ├── in.2.le.dat │ ├── in.2.txt │ ├── in.3.le.dat │ ├── in.3.txt │ ├── in.4.le.dat │ ├── in.4.txt │ ├── in.5.le.dat │ ├── in.5.txt │ ├── in.6.le.dat │ ├── in.6.txt │ ├── in.7.le.dat │ ├── in.7.txt │ ├── in.8.le.dat │ ├── in.8.txt │ ├── out.0.le.dat │ ├── out.1.le.dat │ ├── out.2.le.dat │ ├── out.3.le.dat │ ├── out.4.le.dat │ ├── out.5.le.dat │ ├── out.6.le.dat │ ├── out.7.le.dat │ └── out.8.le.dat └── sec_5.2_internal_sorting ├── CMakeLists.txt ├── algorithm_c_comparison_counting.c ├── algorithm_c_comparison_counting.h ├── data ├── algorithm_c_comparison_counting │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ ├── in.1.txt │ ├── out.0.le.dat │ └── out.1.le.dat └── exercise_5.2.4 │ ├── in.0.le.dat │ ├── in.0.txt │ ├── in.1.le.dat │ ├── in.1.txt │ ├── out.0.le.dat │ └── out.1.le.dat └── exercise_5.2.4.c /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020-2022 Zartaj Majeed 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # programs 2 | Algorithms and programs from The Art of Computer Programming written in C 3 | 4 | This is a collection of C programs for material from TAOCP. The goal is to provide aids to facilitate the understanding of content from TAOCP. To this end, the C code tries to mirror the structure and notation of the algorithms described in TAOCP without overly sacrificing high-level language constructs. For example, if an algorithm uses an array indexed from 1, so does the C program. Also any input and output of a C program should be compatible with the equivalent MMIX program should there be one. For example, if the MMIX program for an algorithm reads and writes binary data in bigendian format, so does the C program. 5 | 6 | Code is not limited to just implementing the named algorithms in TAOCP. Exercise solutions or MMIX programs without algorithms are also fair candidates for versions in C. 7 | -------------------------------------------------------------------------------- /fasc_1_mmix_chap_1_basic_concepts/sec_1.3.2_the_mmix_assembly_language/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # sec_1.3.2_the_mmix_assembly_language/CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.17) 4 | 5 | get_filename_component(COMPNAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) 6 | project(${COMPNAME}) 7 | 8 | add_executable(program_m_find_the_maximum program_m_find_the_maximum.c) 9 | add_executable(program_h_hail_the_world program_h_hail_the_world.c) 10 | add_executable(program_p_print_table_of_500_primes program_p_print_table_of_500_primes.c) 11 | 12 | if(CMAKE_C_COMPILER_ID MATCHES GNU) 13 | 14 | target_compile_definitions(program_m_find_the_maximum PRIVATE _DEFAULT_SOURCE) 15 | target_compile_options(program_m_find_the_maximum PRIVATE -g -Wall -Werror -O0 -std=c18) 16 | 17 | target_compile_options(program_h_hail_the_world PRIVATE -g -Wall -Werror -O0 -std=c18) 18 | 19 | target_compile_options(program_p_print_table_of_500_primes PRIVATE -g -Wall -Werror -O0 -std=c18) 20 | 21 | elseif(CMAKE_C_COMPILER_ID MATCHES MSVC) 22 | 23 | target_compile_options(program_m_find_the_maximum PRIVATE -Wall -WX -Od) 24 | 25 | elseif(CMAKE_C_COMPILER_ID MATCHES Clang) 26 | 27 | target_compile_options(program_m_find_the_maximum PRIVATE -g -Wall -Werror -O0 -std=c18) 28 | 29 | endif() 30 | 31 | -------------------------------------------------------------------------------- /fasc_1_mmix_chap_1_basic_concepts/sec_1.3.2_the_mmix_assembly_language/program_h_hail_the_world.c: -------------------------------------------------------------------------------- 1 | // program_h_hail_the_world.c 2 | 3 | // Program H (Hail the world) 4 | // Section 1.3.2' The MMIX Assembly Language, Fascicle 1, MMIX 5 | 6 | #include 7 | 8 | int main(int argc, char* argv[]) 9 | { 10 | 11 | const char* String = ", world\n"; 12 | 13 | fputs(argv[0], stdout); 14 | fputs(String, stdout); 15 | 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /fasc_1_mmix_chap_1_basic_concepts/sec_1.3.2_the_mmix_assembly_language/program_m_find_the_maximum.c: -------------------------------------------------------------------------------- 1 | // program_m_find_the_maximum.c 2 | 3 | // Algorithm M (Find the maximum) 4 | // Section 1.2.10 Analysis of an Algorithm, Volume 1, Fundamental Algorithms 5 | // Section 1.3.2' The MMIX Assembly Language, Fascicle 1, MMIX 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | void usage() 13 | { 14 | printf("program_m_find_the_maximum n\n"); 15 | printf("prints the index and value of the maximum of 64-bit integers as binary data\n"); 16 | printf("n: number of 64-bit integers in bigendian binary format to read from stdin\n"); 17 | printf("Examples:\n"); 18 | printf("program_m_find_the_maximum 100 255)); then 13 | echo >&2 "error: n $n does not fit in one byte" 14 | exit 1 15 | fi 16 | 17 | outfile=testdata.$$.dat 18 | 19 | echo "test pid $$ program_m_find_the_maximum $n" 20 | seq $n | 21 | sort -R | 22 | while IFS= read -r i; do 23 | printf -vhex "%.2x" $i 24 | printf "\x00\x00\x00\x00\x00\x00\x00\x$hex" 25 | done | 26 | tee $outfile | 27 | program_m_find_the_maximum $n | 28 | od -An -td8 -w8 --endian=big | 29 | tr -d ' ' 30 | 31 | es=$? 32 | echo "test run es $es" 33 | ((es)) && exit $es 34 | 35 | echo -n "$outfile: " 36 | od -An -td8 -w8 --endian=big $outfile | 37 | tr -d ' ' | 38 | nl | 39 | sed -nE '/\t'$n'$/ { s/^\s+//; s/\t/ /; p; q }' 40 | 41 | if ((es == 0)); then 42 | rm $outfile 43 | fi 44 | -------------------------------------------------------------------------------- /fasc_1_mmix_chap_1_basic_concepts/sec_1.3.2_the_mmix_assembly_language/program_p_print_table_of_500_primes.c: -------------------------------------------------------------------------------- 1 | // program_p_print_table_of_500_primes.c 2 | 3 | // Algorithm P (Print table of 500 primes) 4 | // Section 1.3.2' The MMIX Assembly Language, Fascicle 1, MMIX 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | int main(int argc, char* argv[]) 12 | { 13 | 14 | const unsigned L = 500; 15 | 16 | // one more than L to start index at 1 17 | uint16_t PRIME[L+1]; 18 | 19 | PRIME[1] = 2; // P1 [Start table] PRIME[1] <- 2 20 | uint16_t n = 3; // n <- 3 21 | unsigned j = 1; // j <- 1 22 | 23 | for( 24 | PRIME[++j] = n; // P2a [n is prime] j <- j+1, PRIME[j] <- n 25 | j != 500; // P3 [500 found] 26 | PRIME[++j] = n // P2 [n is prime] j <- j+1, PRIME[j] <- n 27 | ) { 28 | 29 | n += 2; // P4a [Advance n] n <- n + 2 30 | 31 | for( 32 | bool prime=false; 33 | ; 34 | n += 2 // P4 [Advance n] n <- n + 2 35 | ) { 36 | 37 | for( 38 | unsigned k = 2; // P5 [k <- 2] 39 | ; 40 | ++k // P8 [Advance k] k <- k + 1 41 | ) { 42 | 43 | uint16_t q = n / PRIME[k]; // q <- floor(n / PRIME[k]) 44 | uint16_t r = n % PRIME[k]; // r <- n mod PRIME[k] 45 | 46 | if(r == 0) { // P6 [PRIME[k]\n?] 47 | break; // To P4 if r = 0 48 | } 49 | 50 | if(q <= PRIME[k]) { // P7 [PRIMEk] large?] 51 | prime = true; // see exercise 1.3.2.11 52 | break; // To P2 if q <= PRIME[k] 53 | } 54 | } 55 | 56 | if(prime) 57 | break; // To P2 58 | } 59 | 60 | } 61 | 62 | puts("First Five Hundred Primes"); // P9 [Print title] 63 | 64 | for( 65 | unsigned m = 1; // m <- 1 66 | m <= 50; // P11 [500 printed?] To p10 if m <= 50 67 | ++m // m <- m + 1 68 | ) { 69 | // P10 [Print line] 70 | fputs(" ", stdout); 71 | for(unsigned i = 0; i < 10; ++i) { 72 | printf(" %.4u", PRIME[50*i + m]); 73 | } 74 | puts(""); 75 | 76 | } 77 | 78 | return 0; 79 | } 80 | 81 | -------------------------------------------------------------------------------- /fasc_1_mmix_chap_1_basic_concepts/sec_1.4.2_coroutines/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # sec_1.4.2_coroutines/CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.20) 4 | 5 | get_filename_component(COMPNAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) 6 | project(${COMPNAME}) 7 | 8 | add_executable(inout_c.coroutinestruct inout_c.coroutinestruct.c) 9 | add_executable(inout_c.onefunc inout_c.onefunc.c) 10 | add_executable(inout_cplusplus inout_cplusplus.cpp) 11 | 12 | if(CMAKE_C_COMPILER_ID MATCHES GNU) 13 | 14 | target_compile_options(inout_cplusplus PRIVATE -ggdb3 -Wall -Werror -Wextra -O0 -std=gnu++2b) 15 | target_compile_options(inout_c.coroutinestruct PRIVATE -ggdb3 -Wall -Werror -Wextra -O0 -std=c18) 16 | target_compile_options(inout_c.onefunc PRIVATE -ggdb3 -Wall -Werror -Wextra -O0 -std=c18) 17 | 18 | elseif(CMAKE_C_COMPILER_ID MATCHES MSVC) 19 | 20 | target_compile_options(inout_c.coroutinestruct PRIVATE -Wall -WX -Od -std:c17) 21 | target_compile_options(inout_c.onefunc PRIVATE -Wall -WX -Od -std:c17) 22 | # disable vc++ warning 5045 for index range check and spectre mitigation 23 | target_compile_options(inout_cplusplus PRIVATE -Wall -WX -Od -std:c++latest -wd5045) 24 | 25 | elseif(CMAKE_C_COMPILER_ID MATCHES Clang) 26 | 27 | target_compile_options(inout_cplusplus PRIVATE -ggdb3 -Wall -Wextra -O0 -std=gnu++2b -stdlib=libc++ -fcoroutines-ts) 28 | target_compile_options(inout_c.coroutinestruct PRIVATE -ggdb3 -Wall -Werror -Wextra -O0 -std=c18) 29 | target_compile_options(inout_c.onefunc PRIVATE -ggdb3 -Wall -Werror -Wextra -O0 -std=c18) 30 | 31 | endif() 32 | 33 | -------------------------------------------------------------------------------- /fasc_1_mmix_chap_1_basic_concepts/sec_1.4.2_coroutines/README.md: -------------------------------------------------------------------------------- 1 | # Section 1.4.2' Coroutines 2 | 3 | Knuth poses the decoding problem below and provides an MMIX program to explain coroutines. The MMIX program is available at https://github.com/theartofcomputerprogramming/mmixsamples/blob/master/inout.mms 4 | 5 | This directory contains three programs in C and C++ for the decoding problem. 6 | 7 | [**`inout_c.onefunc.c`**](inout_c.onefunc.c) is a straightforward attempt in C to solve the decoding problem. It does not use coroutines. The entire program is essentially one function with one loop that reads input, decodes characters and prints output chracters as determined. 8 | 9 | Even in such a short and simple program, one can see multiple kinds of processing all mixed in with each other. 10 | 11 | [**`inout_c.coroutinestruct.c`**](inout_c.coroutinestruct.c) is a C program with a coroutine-based design. There is no coroutine support in C. Instead this program simulates coroutines with a custom data type and a big switch containing all the states where the coroutines transfer control among each other. 12 | 13 | Unlike other descriptions of how to support coroutines in C, this approach does not rely on `setjmp` or `longjmp`. It does not manipulate the call stack with functions like `getcontext` and `savecontext` whose use is no longer encouraged. It does not make use of obscure techniques similar to Duff's device. 14 | 15 | The data structure representing coroutines and their control is based on `coroutine_struct` from [`mmix-pipe.w`](https://github.com/theartofcomputerprogramming/mmixware/blob/githubmaster/mmix-pipe.w#L423) - the instruction pipeline component of the MMIX simulator written by Knuth. 16 | 17 | This C implementation of coroutines is instructive for understanding what is involved in making two loosely-coupled functions successfully collaborate to solve a problem. 18 | 19 | [**`inout_cplusplus.cpp`**](inout_cplusplus.cpp) is a C++ program that uses C++20 coroutines to implement Knuth's solution. 20 | 21 | ## The decoding problem (Fascicle 1 MMIX, TAOCP, p.67) 22 | 23 | In order to study coroutines in action, let us consider a contrived example. Suppose we want to write a program that translates one code into another. The input code to be translated is a sequence of 8-bit characters terminated by a period, such as 24 | 25 | a2b5e3426fg0zyw3210pq89r. (1) 26 | 27 | This code appears on the standard input file, interspersed with whitespace characters in an arbitrary fashion. For our purposes a "whitespace character" will be any byte whose value is less than or equal to #20, the ASCII code for ' '. All whitespace characters in the input are ignored; the other characters should be interpreted as follows, when they are read in sequence: 28 | 29 | (1) If the next character is one of the decimal digits 0 or 1 or ... or 9, say n, it indicates (n + 1) repetitions of the following character, whether the following character is a digit or not. 30 | 31 | (2) A nondigit simply denotes itself. The output of our program is to consist of the resulting sequence separated into groups of three characters each, until a period appears; the last group may have fewer than three characters. For example, (1) should be translated into 32 | 33 | abb bee eee e44 446 66f gzy w22 220 0pq 999 999 999 r. (2) 34 | 35 | Notice that 3426f does not mean 3427 repetitions of the letter f; it means 4 fours and 3 sixes followed by f. If the input sequence is '1.', the output is simply '.', not '..', because the first period terminates the output. The goal of our program is to produce a sequence of lines on the standard output file, with 16 three-character groups per line (except, of course, that the final line might be shorter). The three-character groups should be separated by blank spaces, and each line should end as usual with the ASCII newline character #a. 36 | 37 | ## Programs 38 | 39 | [**`inout_c.onefunc.c:`**](inout_c.onefunc.c) A non-coroutine solution to the problem in C. 40 | 41 | [**`inout_c.coroutinestruct.c:`**](inout_c.coroutinestruct.c) A C program that simulates coroutines using a structure based on `coroutine_struct` from Knuth's MMIX simulator. 42 | 43 | [**`inout_cplusplus.cpp:`**](inout_cplusplus.cpp) A C++ program that uses C++20 coroutines. 44 | 45 | ## Test data 46 | 47 | Test data is in [`data/inout`](data/inout) 48 | -------------------------------------------------------------------------------- /fasc_1_mmix_chap_1_basic_concepts/sec_1.4.2_coroutines/data/inout/in.0.txt: -------------------------------------------------------------------------------- 1 | a2b5e3426fg0zyw3210pq89r. 2 | -------------------------------------------------------------------------------- /fasc_1_mmix_chap_1_basic_concepts/sec_1.4.2_coroutines/data/inout/in.1.txt: -------------------------------------------------------------------------------- 1 | a 2b 5e3426fg0zyw3210pq89r 2 | a2b5e3426fg0zyw3210pq89r a 2b 5e3426fg0zyw3210pq89r 3 | a2b5e3426fg0zyw3210pq89r.. 4 | -------------------------------------------------------------------------------- /fasc_1_mmix_chap_1_basic_concepts/sec_1.4.2_coroutines/data/inout/in.2.txt: -------------------------------------------------------------------------------- 1 | a2b5e3426fg0zyw3210pq89r1a. 2 | -------------------------------------------------------------------------------- /fasc_1_mmix_chap_1_basic_concepts/sec_1.4.2_coroutines/data/inout/in.3.txt: -------------------------------------------------------------------------------- 1 | a2b5e3426fg0zyw3210pq89r1a2b2d. 2 | -------------------------------------------------------------------------------- /fasc_1_mmix_chap_1_basic_concepts/sec_1.4.2_coroutines/data/inout/out.0.txt: -------------------------------------------------------------------------------- 1 | abb bee eee e44 446 66f gzy w22 220 0pq 999 999 999 r. 2 | -------------------------------------------------------------------------------- /fasc_1_mmix_chap_1_basic_concepts/sec_1.4.2_coroutines/data/inout/out.1.txt: -------------------------------------------------------------------------------- 1 | abb bee eee e44 446 66f gzy w22 220 0pq 999 999 999 rab bbe eee 2 | ee4 444 666 fgz yw2 222 00p q99 999 999 9ra bbb eee eee 444 466 3 | 6fg zyw 222 200 pq9 999 999 99r abb bee eee e44 446 66f gzy w22 4 | 220 0pq 999 999 999 r. 5 | -------------------------------------------------------------------------------- /fasc_1_mmix_chap_1_basic_concepts/sec_1.4.2_coroutines/data/inout/out.2.txt: -------------------------------------------------------------------------------- 1 | abb bee eee e44 446 66f gzy w22 220 0pq 999 999 999 raa . 2 | -------------------------------------------------------------------------------- /fasc_1_mmix_chap_1_basic_concepts/sec_1.4.2_coroutines/data/inout/out.3.txt: -------------------------------------------------------------------------------- 1 | abb bee eee e44 446 66f gzy w22 220 0pq 999 999 999 raa bbb ddd 2 | . 3 | -------------------------------------------------------------------------------- /fasc_1_mmix_chap_1_basic_concepts/sec_1.4.2_coroutines/inout_c.onefunc.c: -------------------------------------------------------------------------------- 1 | // inout_c.onefunc.c 2 | 3 | //////////////////////////////////////////////////////////////////////// 4 | // MIT License 5 | // 6 | // Copyright (c) 2020-2022 Zartaj Majeed 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in all 16 | // copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | // SOFTWARE. 25 | //////////////////////////////////////////////////////////////////////// 26 | 27 | // Section 1.4.2' Coroutines, Fascicle 1, MMIX 28 | 29 | // sample input on stdin: a2b5e3426fg0zyw3210pq89r. 30 | // sample output on stdout: abb bee eee e44 446 66f gzy w22 220 0pq 999 999 999 r. 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | static void usage() 41 | { 42 | puts("usage: inout_c.onefunc out.txt"); 43 | } 44 | 45 | // print groups of 3 characters separated by space 46 | // print newline after 16 groups 47 | void print_char(char chr, unsigned count) 48 | { 49 | if(count > 0 && count % 3 == 0) { 50 | char separator = count % 48 == 0? '\n': ' '; 51 | putchar(separator); 52 | } 53 | putchar(chr); 54 | } 55 | 56 | static void InNOut() 57 | { 58 | char InBuf[1000] = ""; 59 | char* inptr = InBuf; 60 | const char Period = '.'; 61 | char inchar; 62 | 63 | int repeatCount = -1; 64 | unsigned outCount = 0; 65 | 66 | for(;;) { 67 | 68 | // fill buffer 69 | if(*inptr == '\0') { 70 | inptr = InBuf; 71 | if(fgets(InBuf, sizeof InBuf, stdin) == NULL) 72 | *inptr = Period; 73 | } 74 | 75 | inchar = *inptr; 76 | ++inptr; 77 | 78 | // skip whitespace and control characters 79 | if(isspace(inchar) || iscntrl(inchar)) 80 | continue; 81 | 82 | // finish processing on input terminator 83 | if(inchar == Period) { 84 | print_char(inchar, outCount); 85 | puts(""); 86 | break; 87 | } 88 | 89 | // single digit is repeat count of following character 90 | if(isdigit(inchar) && repeatCount < 0) { 91 | sscanf(&inchar, "%1d", &repeatCount); 92 | continue; 93 | } 94 | 95 | // emit repeats 96 | for(; repeatCount > 0; --repeatCount) { 97 | print_char(inchar, outCount); 98 | ++outCount; 99 | } 100 | 101 | // emit the character 102 | print_char(inchar, outCount); 103 | ++outCount; 104 | 105 | // reset repeat count without checking for unrepeated case 106 | repeatCount = -1; 107 | } 108 | 109 | } 110 | 111 | int main(int argc, char* argv[]) 112 | { 113 | (void)argv; 114 | 115 | if(argc > 1) { 116 | usage(); 117 | exit(0); 118 | } 119 | 120 | InNOut(); 121 | 122 | return 0; 123 | } 124 | -------------------------------------------------------------------------------- /tools/README.md: -------------------------------------------------------------------------------- 1 | # Tools for TAOCP programs 2 | 3 | [**`texttobinary.sh:`**](texttobinary.sh) Converts a list of numbers to binary data. Many MMIX programs and their equivalents in C only take binary input since MMIX has no builtin library for string processing. This tool allows convenient readable data to be used with these programs. 4 | 5 | [**`gendata.sh:`**](gendata.sh) Generates data used for N=1000 column in Table 1 of section 5.5 of TAOCP Vol 3. Two versions of the dataset are available - the one used in the original TAOCP table and the one used in the updated table from The MMIX Supplement. 6 | -------------------------------------------------------------------------------- /tools/gendata.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # gendata.sh 4 | 5 | # numbers are too big for bash, use bc or awk 6 | 7 | function usage { 8 | echo "usage: gendata.sh -d dataset [-n count] [-h]" 9 | echo "generates various datasets by name" 10 | echo 11 | echo "-d: dataset name" 12 | echo "-c: number of values to generate" 13 | echo "supported datasets" 14 | echo "taocp_table_551_mmix, default count 1000" 15 | echo "taocp_table_551_original, default count 1000" 16 | } 17 | 18 | # generates data of 1000 values used in table 1 from section 5.5 of the mmix supplement for taocp vol 3 19 | # The case N = 1000 refers to the sequence K_1, K_2,..., K_1000 of 32-bit keys defined by 20 | # X_0 = 0 21 | # X_(n+1) = (6364136223846793005 * X_n + 9754186451795953191) mod 2^64 22 | # K_n = floor(X_n/2^32) 23 | # For the multiplier, see Section 3.3.4, page 108; 9754186451795953191 is some random increment value 24 | 25 | function taocp_table_551_mmix { 26 | 27 | local n=$1 28 | bc <= 2; --n) { 67 | k[n - 1] = (3141592621 * k[n] + 2113148651) % ten_10 68 | } 69 | 70 | for(i = 1; i <= $count; ++i) { 71 | print k[i], "\n" 72 | } 73 | 74 | EOF 75 | 76 | } 77 | 78 | # main 79 | 80 | while getopts "d:h" opt; do 81 | case $opt in 82 | d) dataset=$OPTARG;; 83 | n) n=$OPTARG;; 84 | h) usage; exit 0;; 85 | *) usage; exit 1 86 | esac 87 | done 88 | shift $((OPTIND-1)) 89 | 90 | [[ -z $dataset ]] && usage && exit 1 91 | 92 | case $dataset in 93 | taocp_table_551_mmix) 94 | taocp_table_551_mmix ${n:=1000} 95 | ;; 96 | taocp_table_551_original) 97 | taocp_table_551_original ${n:=1000} 98 | esac 99 | 100 | -------------------------------------------------------------------------------- /tools/texttobinary.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # texttobinary.sh 4 | 5 | ######################################################################## 6 | # MIT License 7 | # 8 | # Copyright (c) 2020-2022 Zartaj Majeed 9 | # 10 | # Permission is hereby granted, free of charge, to any person obtaining a copy 11 | # of this software and associated documentation files (the "Software"), to deal 12 | # in the Software without restriction, including without limitation the rights 13 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | # copies of the Software, and to permit persons to whom the Software is 15 | # furnished to do so, subject to the following conditions: 16 | # 17 | # The above copyright notice and this permission notice shall be included in all 18 | # copies or substantial portions of the Software. 19 | # 20 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | # SOFTWARE. 27 | ######################################################################## 28 | 29 | function usage { 30 | echo "usage: texttobinary.sh [-d] [-w width] [-e endian] < in.txt > out.dat" 31 | echo "converts comma- or whitespace-separated numbers on stdin to binary data on stdout" 32 | echo "-d: debug print string of hex escapes" 33 | echo "-w: width in bytes of binary word, default is 4" 34 | echo "-e: endian format big|little, default is little" 35 | echo "in.txt: numbers separated by commas or whitespace, numbers can be decimal or hex with 0x prefix" 36 | echo "examples:" 37 | echo 38 | echo "texttobinary.sh -d < <(echo -e \"9, 2, 3, -7\n-5 100 1024\")" 39 | echo "\x09\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\xf9\xff\xff\xff\xfb\xff\xff\xff\x64\x00\x00\x00\x00\x04\x00\x00" 40 | echo 41 | echo "echo -e \"9, 2, 3, -7\n-5 100 1024\" | texttobinary.sh | od -An -td4 -w4 -v" 42 | } 43 | 44 | while getopts "de:hw:" opt; do 45 | case $opt in 46 | d) debug=true;; 47 | e) endian=$OPTARG;; 48 | h) usage; exit 0;; 49 | w) width=$OPTARG;; 50 | *) usage; exit 1 51 | esac 52 | done 53 | shift $((OPTIND-1)) 54 | 55 | : ${width:=4} ${endian:=little} ${debug:=false} 56 | 57 | [[ $endian != big && $endian != little ]] && usage && exit 1 58 | 59 | let len=2*width 60 | 61 | while IFS+=, read -a vals; do 62 | for((i=0; i < ${#vals[*]}; ++i)); do 63 | val=${vals[i]} 64 | 65 | # printf output is 64-bit bigendian 66 | # printf "0x%.2x" -7 prints 0xfffffffffffffff9 67 | printf -vhex "%.${len}x" $val 68 | 69 | # truncate sign extension from right for 64-bit negative numbers past maximum length eg -7 should be 0xfffffff9 for 4 bytes 70 | if ((val < 0)); then 71 | hex=${hex: -len} 72 | fi 73 | 74 | # reverse bytes for littleendian 75 | if [[ $endian == little ]]; then 76 | for((j=0; j < ${#hex}; j+=2)); do 77 | str+="\\x${hex: -j-2:2}" 78 | done 79 | elif [[ $endian == big ]]; then 80 | for((j=0; j < ${#hex}; j+=2)); do 81 | str+="\\x${hex:j:2}" 82 | done 83 | fi 84 | 85 | done 86 | done 87 | 88 | # prints hex escaped string for debugging 89 | if $debug; then 90 | echo "$str" 91 | exit 0 92 | fi 93 | 94 | # print binary data 95 | echo -en "$str" 96 | 97 | -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.3_linked_allocation/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # sec_2.2.3_linked_allocation/CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.20) 4 | 5 | get_filename_component(COMPNAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) 6 | project(${COMPNAME}) 7 | 8 | add_executable(algorithm_t_topological_sort algorithm_t_topological_sort.c) 9 | 10 | if(CMAKE_C_COMPILER_ID MATCHES GNU) 11 | 12 | target_compile_definitions(algorithm_t_topological_sort PRIVATE _DEFAULT_SOURCE) 13 | target_compile_options(algorithm_t_topological_sort PRIVATE -g -Wall -Werror -O0 -std=c18) 14 | 15 | elseif(CMAKE_C_COMPILER_ID MATCHES MSVC) 16 | 17 | # -wd5045 suppress warning C5045 for spectre mitigation 18 | target_compile_options(algorithm_t_topological_sort PRIVATE -Wall -WX -Od -std:c17 -wd5045) 19 | 20 | elseif(CMAKE_C_COMPILER_ID MATCHES Clang) 21 | 22 | target_compile_options(algorithm_t_topological_sort PRIVATE -g -Wall -Werror -O0 -std=c18) 23 | 24 | endif() 25 | 26 | -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.3_linked_allocation/data/algorithm_t_topological_sort/in.0.le.dat: -------------------------------------------------------------------------------- 1 |   -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.3_linked_allocation/data/algorithm_t_topological_sort/in.0.txt: -------------------------------------------------------------------------------- 1 | 0, 9 2 | 9, 2 3 | 3, 7 4 | 7, 5 5 | 5, 8 6 | 8, 6 7 | 4, 6 8 | 1, 3 9 | 7, 4 10 | 9, 5 11 | 2, 8 12 | 0, 0 13 | -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.3_linked_allocation/data/algorithm_t_topological_sort/in.1.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.3_linked_allocation/data/algorithm_t_topological_sort/in.1.txt: -------------------------------------------------------------------------------- 1 | 0, 3 2 | 1, 2 3 | 1, 3 4 | 0, 0 5 | -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.3_linked_allocation/data/algorithm_t_topological_sort/in.2.le.dat: -------------------------------------------------------------------------------- 1 |   2 | 3 |   -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.3_linked_allocation/data/algorithm_t_topological_sort/in.2.txt: -------------------------------------------------------------------------------- 1 | 0, 15 2 | 1, 2 3 | 2, 3 4 | 4, 5 5 | 5, 1 6 | 5, 12 7 | 5, 6 8 | 7, 6 9 | 8, 9 10 | 10, 11 11 | 12, 10 12 | 12, 13 13 | 13, 14 14 | 13, 9 15 | 14, 15 16 | 0, 0 17 | -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.3_linked_allocation/data/algorithm_t_topological_sort/out.0.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.3_linked_allocation/data/algorithm_t_topological_sort/out.1.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.3_linked_allocation/data/algorithm_t_topological_sort/out.2.le.dat: -------------------------------------------------------------------------------- 1 |   2 |    -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.3_linked_allocation/data/algorithm_t_topological_sort/out.2.txt: -------------------------------------------------------------------------------- 1 | 8 2 | 7 3 | 4 4 | 5 5 | 6 6 | 12 7 | 1 8 | 13 9 | 10 10 | 2 11 | 9 12 | 14 13 | 11 14 | 3 15 | 15 16 | 0 17 | -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # sec_2.2.4_circular_lists/CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.17) 4 | 5 | get_filename_component(COMPNAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) 6 | project(${COMPNAME}) 7 | 8 | add_library(polynomial STATIC polynomial.c) 9 | target_include_directories(polynomial PRIVATE .) 10 | 11 | add_executable(algorithm_a_addition_of_polynomials algorithm_a_addition_of_polynomials.c) 12 | target_link_libraries(algorithm_a_addition_of_polynomials PRIVATE polynomial) 13 | 14 | add_executable(algorithm_m_multiplication_of_polynomials algorithm_m_multiplication_of_polynomials.c) 15 | target_link_libraries(algorithm_m_multiplication_of_polynomials PRIVATE polynomial) 16 | 17 | if(CMAKE_C_COMPILER_ID MATCHES GNU) 18 | 19 | target_compile_definitions(algorithm_a_addition_of_polynomials PRIVATE _DEFAULT_SOURCE) 20 | target_compile_options(algorithm_a_addition_of_polynomials PRIVATE -g -Wall -Werror -O0 -std=c18) 21 | 22 | elseif(CMAKE_C_COMPILER_ID MATCHES MSVC) 23 | 24 | target_compile_options(algorithm_a_addition_of_polynomials PRIVATE -Wall -WX -Od) 25 | 26 | elseif(CMAKE_C_COMPILER_ID MATCHES Clang) 27 | 28 | target_compile_options(algorithm_a_addition_of_polynomials PRIVATE -g -Wall -Werror -O0 -std=c18) 29 | 30 | endif() 31 | 32 | -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/algorithm_a_addition_of_polynomials.c: -------------------------------------------------------------------------------- 1 | // algorithm_a_addition_of_polynomials.c 2 | 3 | // Algorithm A (Addition of polynomials), 2.2.4 Circular Lists, 4 | // The Art of Computer Programming, Donald Knuth 5 | 6 | // Program A (Addition of polynomials), 2.2.4 Circular Lists, 7 | // The MMIX Supplement, Martin Ruckert 8 | 9 | // usage: algorithm_a_addition_of_polynomials out.dat 10 | // reads two polynomials as binary data on stdin 11 | // format for each polynomial is sequence of terms terminated by single int64_t zero 12 | // terms are in decreasing order by exponent field 13 | // each term is int64_t coefficient followed by single int64_t for the exponents of the four variables in the term 14 | // each exponent uses 2 bytes except the first exponent which is allowed only 15 bits of the 2 high bytes excluding the high sign bit 15 | // examples: 16 | // algorithm_a_addition_of_polynomials 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "polynomial.h" 26 | 27 | int Add(const struct Term* P, struct Term* Q) 28 | { 29 | struct Term* Q1; 30 | struct Term* Q2; 31 | 32 | for( 33 | // A1 [Initialize] P <- LINK(P), Q1 <- Q, Q <- LINK(Q) 34 | Q1 = Q, P = P->LINK, Q = Q->LINK; 35 | ; 36 | ) { 37 | 38 | // A2 [ABC(P):ABC(Q)] Repeat Q1 <- Q, Q <- LINK(Q) if ABC(P) < ABC(Q) 39 | while(P->ABC < Q->ABC) { 40 | Q1 = Q; 41 | Q = Q->LINK; 42 | } 43 | 44 | // A3 [Add coefficients] Found terms with equal exponents 45 | if(P->ABC == Q->ABC) { 46 | 47 | // A3 [Add coefficients] Terminate if ABC(P) < 0 48 | // I'd prefer this to be the loop guard for nicer structure but it deviates from the algorithm and adds an unnecessary check for all cases 49 | if(P->ABC < 0) 50 | break; 51 | 52 | // A3 [Add coefficients] COEF(Q) <- COEF(Q) + COEF(P) 53 | Q->COEF += P->COEF; 54 | 55 | // A4 [Delete zero term] 56 | if(Q->COEF == 0) { 57 | Q2 = Q; 58 | Q1->LINK = Q = Q->LINK; 59 | free(Q2); 60 | P = P->LINK; 61 | // A4 [Delete zero term] To A2 62 | continue; 63 | } 64 | 65 | // A3 [Add coefficients] P <- LINK(P), Q1 <- Q, Q <- LINK(Q) 66 | P = P->LINK; 67 | Q1 = Q; 68 | Q = Q->LINK; 69 | // A3 [Add coefficients] To A2 70 | continue; 71 | } 72 | 73 | // A5 [Insert new term] Q2 <= AVAIL 74 | Q2 = malloc(sizeof *Q2); 75 | if(!Q2) { 76 | return -1; 77 | } 78 | 79 | // A5 [Insert new term] COEF(Q2) <- COEF(P), ABC(Q2) <- ABC(P), ... 80 | Q2->COEF = P->COEF; 81 | Q2->ABC = P->ABC; 82 | Q2->LINK = Q; 83 | Q1->LINK = Q2; 84 | Q1 = Q2; 85 | P = P->LINK; 86 | // A5 [Insert new term] To A2 87 | } 88 | 89 | return 0; 90 | 91 | } 92 | 93 | void cleanup(struct Term* P, struct Term* Q) 94 | { 95 | polynomial_clear(P); 96 | polynomial_clear(Q); 97 | } 98 | 99 | int main(int argc, char* argv[]) 100 | { 101 | 102 | struct Term P = {NULL, -1, 0}; 103 | struct Term Q = {NULL, -1, 0}; 104 | 105 | struct Term* polys[] = {&P, &Q}; 106 | if(get_polynomials(stdin, polys, 2) != 0) { 107 | cleanup(&P, &Q); 108 | return 1; 109 | } 110 | 111 | if(Add(&P, &Q) != 0) { 112 | cleanup(&P, &Q); 113 | return 2; 114 | } 115 | 116 | polynomial_write(&Q, stdout); 117 | 118 | cleanup(&P, &Q); 119 | 120 | return 0; 121 | } 122 | 123 | -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/algorithm_m_multiplication_of_polynomials.c: -------------------------------------------------------------------------------- 1 | // algorithm_m_multiplication_of_polynomials.c 2 | 3 | // Algorithm M (Multiplication of polynomials), 2.2.4 Circular Lists, 4 | // The Art of Computer Programming, Donald Knuth 5 | 6 | // usage: algorithm_m_multiplication_of_polynomials out.dat 7 | // reads three polynomials as binary data on stdin 8 | // format for each polynomial is sequence of terms terminated by single int64_t zero 9 | // terms are in decreasing order by exponent field 10 | // each term is int64_t coefficient followed by single int64_t for the exponents of the four variables in the term 11 | // each exponent uses 2 bytes except the first exponent which is allowed only 15 bits of the 2 high bytes excluding the high sign bit 12 | // examples: 13 | // algorithm_m_multiplication_of_polynomials 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | #include "polynomial.h" 41 | 42 | int Mult(struct Term* Q, const struct Term* M, const struct Term* P) 43 | { 44 | for( 45 | // M1 [Next multiplier] M <- LINK(M) 46 | M = M->LINK; 47 | 48 | // M1 [Next multiplier] Terminate if ABC(M) < 0 49 | M->ABC >= 0; 50 | 51 | // M2 [Multiply cycle] To M1 52 | M = M->LINK 53 | ) { 54 | 55 | // M2 [Multiply cycle] Perform Algorithm A 56 | 57 | struct Term* Q1; 58 | struct Term* Q2; 59 | 60 | for( 61 | // A1 [Initialize] P <- LINK(P), Q1 <- Q, Q <- LINK(Q) 62 | Q1 = Q, P = P->LINK, Q = Q->LINK; 63 | ; 64 | ) { 65 | 66 | // M2 [Multiply cycle] replace "ABC(P)" with "(if ABC(P) < 0 then -1, otherwise ABC(P) + ABC(M))" 67 | // A2 [ABC(P):ABC(Q)] Repeat Q1 <- Q, Q <- LINK(Q) if ABC(P) < ABC(Q) 68 | while((P->ABC < 0? -1: P->ABC + M->ABC) < Q->ABC) { 69 | Q1 = Q; 70 | Q = Q->LINK; 71 | } 72 | 73 | // M2 [Multiply cycle] replace "ABC(P)" with "(if ABC(P) < 0 then -1, otherwise ABC(P) + ABC(M))" 74 | // A3 [Add coefficients] Found terms with equal exponents 75 | if((P->ABC < 0? -1: P->ABC + M->ABC) == Q->ABC) { 76 | 77 | // A3 [Add coefficients] Terminate if ABC(P) < 0 78 | if(P->ABC < 0) 79 | break; 80 | 81 | // M2 [Multiply cycle] replace "COEF(P)" with "COEF(P) x COEF(M)" 82 | // A3 [Add coefficients] COEF(Q) <- COEF(Q) + COEF(P) 83 | Q->COEF += P->COEF * M->COEF; 84 | 85 | // A4 [Delete zero term] 86 | if(Q->COEF == 0) { 87 | Q2 = Q; 88 | Q1->LINK = Q = Q->LINK; 89 | free(Q2); 90 | P = P->LINK; 91 | // A4 [Delete zero term] To A2 92 | continue; 93 | } 94 | 95 | // A3 [Add coefficients] P <- LINK(P), Q1 <- Q, Q <- LINK(Q) 96 | P = P->LINK; 97 | Q1 = Q; 98 | Q = Q->LINK; 99 | // A3 [Add coefficients] To A2 100 | continue; 101 | } 102 | 103 | // A5 [Insert new term] Q2 <= AVAIL 104 | Q2 = malloc(sizeof *Q2); 105 | if(!Q2) { 106 | return -1; 107 | } 108 | 109 | // M2 [Multiply cycle] replace "COEF(P)" with "COEF(P) x COEF(M)" 110 | // A5 [Insert new term] COEF(Q2) <- COEF(P), ABC(Q2) <- ABC(P), ... 111 | Q2->COEF = P->COEF * M->COEF; 112 | 113 | // M2 [Multiply cycle] replace "ABC(P)" with "(if ABC(P) < 0 then -1, otherwise ABC(P) + ABC(M))" 114 | Q2->ABC = (P->ABC < 0? -1: P->ABC + M->ABC); 115 | 116 | Q2->LINK = Q; 117 | Q1->LINK = Q2; 118 | Q1 = Q2; 119 | P = P->LINK; 120 | // A5 [Insert new term] To A2 121 | } 122 | 123 | } 124 | 125 | return 0; 126 | 127 | } 128 | 129 | void cleanup(struct Term* Q, struct Term* M, struct Term* P) 130 | { 131 | polynomial_clear(P); 132 | polynomial_clear(M); 133 | polynomial_clear(Q); 134 | } 135 | 136 | int main(int argc, char* argv[]) 137 | { 138 | 139 | struct Term Q = {NULL, -1, 0}; 140 | struct Term M = {NULL, -1, 0}; 141 | struct Term P = {NULL, -1, 0}; 142 | 143 | struct Term* polys[] = {&Q, &M, &P}; 144 | if(get_polynomials(stdin, polys, 3) != 0) { 145 | cleanup(&Q, &M, &P); 146 | return 1; 147 | } 148 | 149 | if(Mult(&Q, &M, &P) != 0) { 150 | cleanup(&Q, &M, &P); 151 | return 2; 152 | } 153 | 154 | polynomial_write(&Q, stdout); 155 | 156 | cleanup(&Q, &M, &P); 157 | 158 | return 0; 159 | } 160 | 161 | -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_a_addition_of_polynomials/in.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_a_addition_of_polynomials/in.0.le.dat -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_a_addition_of_polynomials/in.0.txt: -------------------------------------------------------------------------------- 1 | 5, 0x0001000100000000, 7, 0x0001000000000000, 7, 0x0000000100010000, 20, 0, 0 2 | 3, 0x0001000100000000, -7, 0x0000000100010000, 3, 0x0000000100000000, -20, 0, 0 3 | -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_a_addition_of_polynomials/in.1.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_a_addition_of_polynomials/in.1.txt: -------------------------------------------------------------------------------- 1 | 5, 0x0001000100000000, 7, 0x0001000000000000, 7, 0x0000000100010000, 20, 0, 0 2 | 0 3 | -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_a_addition_of_polynomials/out.0.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_a_addition_of_polynomials/out.1.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_m_multiplication_of_polynomials/in.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_m_multiplication_of_polynomials/in.0.le.dat -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_m_multiplication_of_polynomials/in.0.txt: -------------------------------------------------------------------------------- 1 | 0 2 | -3, 0x0001000100000000, 14, 0x0001000000010000, 2, 0, 0 3 | -1, 0x0001000100000000, 1, 0x0001000000010000, 1, 0, 0 4 | -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_m_multiplication_of_polynomials/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_m_multiplication_of_polynomials/in.1.le.dat -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_m_multiplication_of_polynomials/in.1.txt: -------------------------------------------------------------------------------- 1 | 33, 0x0001000000000000, 72, 0, 0 2 | 1, 0x0002000000000000, 2, 0x0001000000000000, -14, 0, 0 3 | 1, 0x0002000000000000, 3, 0x0001000000000000, 5, 0, 0 4 | -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_m_multiplication_of_polynomials/in.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_m_multiplication_of_polynomials/in.2.le.dat -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_m_multiplication_of_polynomials/in.2.txt: -------------------------------------------------------------------------------- 1 | -1, 0x0000000100010000, 4, 0x0000000100000000, 0 2 | 1, 0x0001000000000000, -2, 0, 0 3 | 1, 0x0002000000000000, -1, 0x0000000100010000, 2, 0x0000000100000000, 0 4 | -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_m_multiplication_of_polynomials/out.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_m_multiplication_of_polynomials/out.0.le.dat -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_m_multiplication_of_polynomials/out.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_m_multiplication_of_polynomials/out.1.le.dat -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_m_multiplication_of_polynomials/out.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/data/algorithm_m_multiplication_of_polynomials/out.2.le.dat -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/polynomial.c: -------------------------------------------------------------------------------- 1 | // polynomial.c 2 | 3 | // Algorithm A (Addition of polynomials), 2.2.4 Circular Lists, 4 | // The Art of Computer Programming, Donald Knuth 5 | 6 | // Program A (Addition of polynomials), 2.2.4 Circular Lists, 7 | // The MMIX Supplement, Martin Ruckert 8 | 9 | // input format for each polynomial is sequence of terms terminated by single int64_t zero 10 | // each term is int64_t coefficient followed by single int64_t for the exponents of the four variables in the term 11 | // each exponent uses 2 bytes except the first exponent which is allowed only 15 bits of the 2 high bytes excluding the high sign bit 12 | 13 | #include "polynomial.h" 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | void polynomial_init(struct Term* p) 22 | { 23 | p->ABC = -1; 24 | p->COEF = 0; 25 | p->LINK = NULL; 26 | } 27 | 28 | // not as nice as the simple erase described in TAOCP 2.2.4 because each node is separately malloced 29 | void polynomial_clear(struct Term* P) 30 | { 31 | 32 | for(struct Term* p = P->LINK; p->ABC != -1;) { 33 | struct Term* prev = p; 34 | p = p->LINK; 35 | free(prev); 36 | } 37 | 38 | } 39 | 40 | void polynomial_write(const struct Term* P, FILE* out) 41 | { 42 | for(const struct Term* p = P->LINK; p->ABC != -1; p = p->LINK) { 43 | fwrite(&p->COEF, sizeof(p->COEF), 1, out); 44 | fwrite(&p->ABC, sizeof(p->ABC), 1, out); 45 | } 46 | 47 | uint64_t zero = 0; 48 | fwrite(&zero, sizeof(zero), 1, out); 49 | } 50 | 51 | // each polynomial is sequence of pairs of int64_t, first is coefficient, second is exponents of the variables, sequence is terminated with single zero 52 | // count is buffer size, returned with number of items processed 53 | // complete is true when terminating zero for complete polynomial is found 54 | // new terms are appended to tail, and tail is updated 55 | static int load_polynomial(const int64_t* buffer, size_t* count, struct Term** tail, bool* complete) 56 | { 57 | if(!buffer || !tail || !*tail || !count || !complete) 58 | return -1; 59 | 60 | const uint32_t n = *count; 61 | *complete = false; 62 | 63 | if(n == 0) { 64 | return 0; 65 | } 66 | 67 | uint32_t i; 68 | for(i = 0; i < n; i += 2) { 69 | 70 | // found polynomial terminator 71 | if(buffer[i] == 0) { 72 | *count = i + 1; 73 | *complete = true; 74 | return 0; 75 | } 76 | 77 | // need pair of values for new term 78 | if(i + 1 == n) { 79 | *count = i; 80 | return 0; 81 | } 82 | 83 | struct Term* term = malloc(sizeof(*term)); 84 | if(!term) { 85 | *count = i; 86 | fprintf(stderr, "error: malloc failure\n"); 87 | return -1; 88 | } 89 | 90 | // set coefficient and exponents fields of new term 91 | // setting the link is not necessary here since only caller can complete the circular list 92 | term->COEF = buffer[i]; 93 | term->ABC = buffer[i + 1]; 94 | term->LINK = NULL; 95 | 96 | // append to tail and update tail 97 | (*tail)->LINK = term; 98 | *tail = term; 99 | 100 | } 101 | 102 | *count = i; 103 | 104 | return 0; 105 | } 106 | 107 | int get_polynomials(FILE* in, struct Term* polys[], uint32_t npolys) 108 | { 109 | 110 | if(!in) 111 | return -1; 112 | 113 | #define BUFSZ 64 114 | int64_t buffer[BUFSZ]; 115 | 116 | ssize_t unprocessed_vals = 0; 117 | size_t processed_vals = 0; 118 | size_t copied_vals = 0; 119 | 120 | for(int i = 0; i < npolys; ++i) { 121 | 122 | struct Term* tail = polys[i]; 123 | bool complete = false; 124 | 125 | while(!complete) { 126 | 127 | // refill buffer 128 | if(unprocessed_vals == 0) { 129 | 130 | if(feof(in)) { 131 | fprintf(stderr, "error: unexpected EOF\n"); 132 | return -2; 133 | } 134 | 135 | const size_t nread = fread(buffer + copied_vals, sizeof(*buffer), BUFSZ - copied_vals, in); 136 | 137 | if(ferror(in)) { 138 | fprintf(stderr, "error: read error\n"); 139 | return -3; 140 | } 141 | 142 | unprocessed_vals = copied_vals + nread; 143 | processed_vals = 0; 144 | copied_vals = 0; 145 | } 146 | 147 | size_t n = unprocessed_vals; 148 | 149 | // n is passed number of unprocessed values and holds processed count on return 150 | if(load_polynomial(buffer + processed_vals, &n, &tail, &complete) != 0) 151 | return -4; 152 | 153 | processed_vals += n; 154 | unprocessed_vals -= n; 155 | 156 | // move unprocessed values to start of buffer to make room for more data because polynomial is still incomplete 157 | if(!complete && unprocessed_vals != 0) { 158 | memcpy(buffer, buffer + processed_vals, unprocessed_vals * sizeof(*buffer)); 159 | copied_vals = unprocessed_vals; 160 | unprocessed_vals = 0; 161 | } 162 | 163 | } 164 | 165 | // last term should point to sentinel node 166 | if(tail->LINK != polys[i]) 167 | tail->LINK = polys[i]; 168 | 169 | } 170 | 171 | return 0; 172 | } 173 | 174 | -------------------------------------------------------------------------------- /vol_1_fundamental_algorithms_chap_2_information_structures/sec_2.2.4_circular_lists/polynomial.h: -------------------------------------------------------------------------------- 1 | #ifndef POLYNOMIAL_H 2 | #define POLYNOMIAL_H 3 | // polynomial.h 4 | 5 | // Algorithm A (Addition of polynomials), 2.2.4 Circular Lists, 6 | // The Art of Computer Programming, Donald Knuth 7 | 8 | // Program A (Addition of polynomials), 2.2.4 Circular Lists, 9 | // The MMIX Supplement, Martin Ruckert 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | struct Term { 16 | struct Term* LINK; 17 | int64_t ABC; 18 | int64_t COEF; 19 | }; 20 | 21 | int get_polynomials(FILE* in, struct Term* polys[], uint32_t npolys); 22 | void polynomial_write(const struct Term* P, FILE* out); 23 | void polynomial_clear(struct Term* P); 24 | 25 | #endif 26 | 27 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # sec_5.2.1_sorting_by_insertion/CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.17) 4 | 5 | get_filename_component(COMPNAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) 6 | project(${COMPNAME}) 7 | 8 | add_executable(algorithm_s_straight_insertion_sort algorithm_s_straight_insertion_sort.c) 9 | add_executable(algorithm_d_shellsort algorithm_d_shellsort.c) 10 | add_executable(algorithm_l_list_insertion algorithm_l_list_insertion.c) 11 | add_executable(algorithm_m_multiple_list_insertion algorithm_m_multiple_list_insertion.c) 12 | 13 | if(CMAKE_C_COMPILER_ID MATCHES GNU) 14 | 15 | target_compile_options(algorithm_s_straight_insertion_sort PRIVATE -g -Wall -Werror -O0 -std=c18) 16 | target_compile_options(algorithm_d_shellsort PRIVATE -g -Wall -Werror -O0 -std=c18) 17 | target_compile_options(algorithm_l_list_insertion PRIVATE -g -Wall -Werror -O0 -std=c18) 18 | target_compile_options(algorithm_m_multiple_list_insertion PRIVATE -g -Wall -Werror -O0 -std=c18) 19 | 20 | 21 | elseif(CMAKE_C_COMPILER_ID MATCHES MSVC) 22 | 23 | target_compile_options(algorithm_s_straight_insertion_sort PRIVATE -Wall -WX -Od) 24 | target_compile_options(algorithm_d_shellsort PRIVATE -Wall -WX -Od) 25 | target_compile_options(algorithm_l_list_insertion PRIVATE -Wall -WX -Od) 26 | target_compile_options(algorithm_m_multiple_list_insertion PRIVATE -Wall -WX -Od) 27 | 28 | elseif(CMAKE_C_COMPILER_ID MATCHES Clang) 29 | 30 | target_compile_options(algorithm_s_straight_insertion_sort PRIVATE -g -Wall -Werror -O0 -std=c18) 31 | target_compile_options(algorithm_d_shellsort PRIVATE -g -Wall -Werror -O0 -std=c18) 32 | target_compile_options(algorithm_l_list_insertion PRIVATE -g -Wall -Werror -O0 -std=c18) 33 | target_compile_options(algorithm_m_multiple_list_insertion PRIVATE -g -Wall -Werror -O0 -std=c18) 34 | 35 | endif() 36 | 37 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/algorithm_d_shellsort.c: -------------------------------------------------------------------------------- 1 | // algorithm_d_shellsort.c 2 | 3 | // Algorithm D (Shellsort) 4 | // 5.2.1 Sorting by Insertion 5 | // The Art of Computer Programming, Donald Knuth 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | static void usage() 14 | { 15 | puts("usage:algorithm_d_shellsort out.dat"); 16 | 17 | puts("reads 64-bit values as binary data to sort, outputs sorted 64-bit values as binary data"); 18 | 19 | puts("first uint64_t is number of values to sort"); 20 | puts("next that many int64_t is data to sort"); 21 | puts("then uint64_t is number of increments to use"); 22 | puts("next that many uint64_t are values of increment"); 23 | puts("first increment value must be 1"); 24 | 25 | puts(""); 26 | puts("binary input data format"); 27 | puts("uint64_t N"); 28 | puts("int64_t[N] data"); 29 | puts("uint64_t t"); 30 | puts("uint64_t[t] increments"); 31 | 32 | puts(""); 33 | puts("binary output data format"); 34 | puts("uint64_t N"); 35 | puts("int64_t[N] sorted data"); 36 | 37 | puts(""); 38 | puts("examples:"); 39 | puts("algorithm_d_shellsort = 0; --s) { 55 | 56 | // D2 [Loop on j] h <- h_s, perform D3 through D6 for h < j <= N 57 | for(uint64_t h = H[s], j = h + 1; j <= N; ++j) { 58 | 59 | int64_t K; 60 | int64_t i; 61 | 62 | for( 63 | 64 | // D3 [Set up i, K, R] i <- j - 1, K <- K_j 65 | i = j - h, K = K_[j]; 66 | 67 | // D5 [Move R_i, decrease i] To D4 if i > 0 68 | i > 0; 69 | 70 | // D5 [Move R_i, decrease i] i <- i - h 71 | i -= h 72 | ) { 73 | 74 | // D4 [Compare K:K_i] To D6 if K >= K_i 75 | if(K >= K_[i]) 76 | break; 77 | 78 | // D5 [Move R_i, decrease i] R_{i+h} <- R_i 79 | K_[i + h] = K_[i]; 80 | 81 | } 82 | 83 | // D6 [R into R_{i+h}] R_{i+h} <- R 84 | K_[i + h] = K; 85 | 86 | } 87 | } 88 | 89 | } 90 | 91 | int main(int argc, char* argv[]) 92 | { 93 | 94 | if(argc > 1) { 95 | usage(); 96 | exit(0); 97 | } 98 | 99 | // read 64-bit size of data array as binary data 100 | uint64_t N; 101 | fread(&N, sizeof N, 1, stdin); 102 | 103 | // read array R of records as binary data 104 | // allocate N+1 entries to use array indexing from 1 instead of 0 105 | int64_t R[N + 1]; 106 | fread(&R[1], sizeof(*R), N, stdin); 107 | 108 | // read 64-bit size of increments array as binary data 109 | uint64_t t; 110 | fread(&t, sizeof t, 1, stdin); 111 | 112 | // read array of 64-bit increments as binary data 113 | uint64_t H[t]; 114 | fread(H, sizeof(*H), t, stdin); 115 | 116 | if(H[0] != 1) { 117 | usage(); 118 | exit(1); 119 | } 120 | 121 | Sort(N, R, t, H); 122 | 123 | // write number of values to follow 124 | fwrite(&N, sizeof N, 1, stdout); 125 | 126 | // print sorted array as binary data 127 | fwrite(&R[1], sizeof(*R), N, stdout); 128 | 129 | return 0; 130 | } 131 | 132 | 133 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/algorithm_l_list_insertion.c: -------------------------------------------------------------------------------- 1 | // algorithm_l_list_insertion.c 2 | 3 | // Algorithm L (List insertion) 4 | // 5.2.1 Sorting by Insertion 5 | // The Art of Computer Programming, Donald Knuth 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | static void usage() 14 | { 15 | puts("usage:algorithm_l_list_insertion out.dat"); 16 | puts("Implements Algorithm L (List insertion), 5.2.1 Sorting by Insertion, The Art of Computer Programming Volume 3, Sorting and Searching by Donald Knuth"); 17 | 18 | puts("reads 64-bit values as binary data to sort, outputs sorted 64-bit values as binary data"); 19 | 20 | puts("first uint64_t is number of values to sort"); 21 | puts("next that many int64_t is data to sort"); 22 | 23 | puts(""); 24 | puts("binary input data format"); 25 | puts("uint64_t N"); 26 | puts("int64_t[N] data"); 27 | 28 | puts(""); 29 | puts("binary output data format"); 30 | puts("uint64_t N"); 31 | puts("int64_t[N] sorted data"); 32 | 33 | puts(""); 34 | puts("examples:"); 35 | puts("algorithm_l_list_insertion = 1; --j) { 54 | 55 | uint64_t p, q; 56 | int64_t K; 57 | 58 | for( 59 | 60 | // L2 [Set up p, q, K] p <- L_0, q <- 0, K <- K_j 61 | p = R[0].LINK, q = 0, K = R[j].KEY; 62 | 63 | // L4 [Bump p, q] To L3 if p > 0 64 | p != 0; 65 | 66 | // L4 [Bump p, q] q <- p, p <- L_q 67 | q = p, p = R[q].LINK 68 | ) { 69 | 70 | // L3 [Compare K:K_p] To L5 if K <= K_p 71 | if(K <= R[p].KEY) 72 | break; 73 | 74 | } 75 | 76 | // L5 [Insert into list] L_q <- j, L_j <- p 77 | R[q].LINK = j; 78 | R[j].LINK = p; 79 | 80 | } 81 | 82 | } 83 | 84 | int main(int argc, char* argv[]) 85 | { 86 | 87 | if(argc > 1) { 88 | usage(); 89 | exit(0); 90 | } 91 | 92 | // read 64-bit size of data array as binary data 93 | uint64_t N; 94 | fread(&N, sizeof N, 1, stdin); 95 | 96 | // read array R of records as binary data 97 | // allocate N+1 entries with special node at index 0 98 | struct Record R[N + 1]; 99 | 100 | // special record at index 0 for head of linked list of sorted nodes 101 | R[0].LINK = 0; 102 | R[0].KEY = 0; 103 | 104 | for(uint64_t i = 1; i <= N; ++i) { 105 | fread(&R[i].KEY, sizeof(R[i].KEY), 1, stdin); 106 | } 107 | 108 | Sort(N, R); 109 | 110 | // write number of values to follow 111 | fwrite(&N, sizeof N, 1, stdout); 112 | 113 | // print linked list of sorted keys as binary data 114 | for(uint64_t i = R[0].LINK; i != 0; i = R[i].LINK) { 115 | fwrite(&R[i].KEY, sizeof(R[i].KEY), 1, stdout); 116 | } 117 | 118 | 119 | return 0; 120 | } 121 | 122 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/algorithm_m_multiple_list_insertion.c: -------------------------------------------------------------------------------- 1 | // algorithm_m_multiple_list_insertion.c 2 | 3 | // Algorithm M (Multiple list insertion) 4 | // 5.2.1 Sorting by Insertion 5 | // The Art of Computer Programming, Donald Knuth 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | static void usage() 14 | { 15 | puts("usage:algorithm_m_multiple_list_insertion out.dat"); 16 | puts("Implements Algorithm M (Multiple list insertion), 5.2.1 Sorting by Insertion, The Art of Computer Programming Volume 3, Sorting and Searching by Donald Knuth"); 17 | 18 | puts("reads 64-bit values as binary data to sort, outputs sorted 64-bit values as binary data"); 19 | 20 | puts("first uint64_t is number of values to sort"); 21 | puts("next that many int64_t is data to sort"); 22 | 23 | puts(""); 24 | puts("binary input data format"); 25 | puts("uint64_t e"); 26 | puts("uint64_t M"); 27 | puts("uint64_t N"); 28 | puts("uint64_t[N] data"); 29 | 30 | puts(""); 31 | puts("binary output data format"); 32 | puts("uint64_t N"); 33 | puts("uint64_t[N] sorted data"); 34 | 35 | puts(""); 36 | puts("examples:"); 37 | puts("algorithm_m_multiple_list_insertion 0; --j) { 58 | 59 | // index of next sorted node to compare 60 | // chose to keep this as index instead of making it a pointer like q since 61 | // indexes are nicer to work with than pointers 62 | uint64_t p; 63 | 64 | // trailing pointer to index of next sorted node 65 | // this cannot easily be an offset like in mmix program because R and Heads are arrays of different types 66 | uint64_t* q; 67 | 68 | // current unsorted key 69 | int64_t K; 70 | 71 | for( 72 | 73 | // get new unsorted key, map new key to its list head, get index of first sorted key in list 74 | K = R[j].KEY, q = &Heads[(M * K) >> e], p = *q; 75 | 76 | // stop when end of sorted list is reached 77 | p != 0; 78 | 79 | // update q and p to next nodes in sorted list 80 | q = &R[p].LINK, p = *q 81 | ) { 82 | 83 | // compare new unsorted key K to current sorted key K_p 84 | if(K <= R[p].KEY) 85 | break; 86 | 87 | } 88 | 89 | // insert new unsorted node before current sorted node 90 | *q = j; 91 | R[j].LINK = p; 92 | 93 | } 94 | 95 | } 96 | 97 | int main(int argc, char* argv[]) 98 | { 99 | 100 | if(argc > 1) { 101 | usage(); 102 | exit(0); 103 | } 104 | 105 | uint64_t e; 106 | fread(&e, sizeof e, 1, stdin); 107 | 108 | uint64_t M; 109 | fread(&M, sizeof M, 1, stdin); 110 | 111 | // read 64-bit size of data array as binary data 112 | uint64_t N; 113 | fread(&N, sizeof N, 1, stdin); 114 | 115 | // read array R of records as binary data 116 | // allocate N+1 entries with special node at index 0 117 | struct Record R[N + 1]; 118 | 119 | // special record at index 0 for head of linked list of sorted nodes 120 | R[0].LINK = 0; 121 | R[0].KEY = 0; 122 | 123 | for(uint64_t i = 1; i <= N; ++i) { 124 | fread(&R[i].KEY, sizeof(R[i].KEY), 1, stdin); 125 | } 126 | 127 | uint64_t Heads[M]; 128 | 129 | Sort(N, R, M, Heads, e); 130 | 131 | // write number of values to follow 132 | fwrite(&N, sizeof N, 1, stdout); 133 | 134 | // print linked list of sorted keys as binary data 135 | for(uint64_t j = 0; j < M; ++j) { 136 | for(uint64_t i = Heads[j]; i != 0; i = R[i].LINK) { 137 | fwrite(&R[i].KEY, sizeof(R[i].KEY), 1, stdout); 138 | } 139 | } 140 | 141 | return 0; 142 | } 143 | 144 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/algorithm_s_straight_insertion_sort.c: -------------------------------------------------------------------------------- 1 | // algorithm_s_straight_insertion_sort.c 2 | 3 | // Algorithm S (Straight insertion sort) 4 | // 5.2.1 Sorting by Insertion 5 | // The Art of Computer Programming, Donald Knuth 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | static void usage() 14 | { 15 | puts("usage:algorithm_s_straight_insertion_sort out.dat"); 16 | 17 | puts("reads 64-bit values as binary data to sort, outputs sorted 64-bit values as binary data"); 18 | 19 | puts("first uint64_t is number of values to sort"); 20 | puts("next that many int64_t is data to sort"); 21 | 22 | puts(""); 23 | puts("binary input data format"); 24 | puts("uint64_t N"); 25 | puts("int64_t[N] data"); 26 | 27 | puts(""); 28 | puts("binary output data format"); 29 | puts("uint64_t N"); 30 | puts("int64_t[N] sorted data"); 31 | 32 | puts(""); 33 | puts("examples:"); 34 | puts("algorithm_s_straight_insertion_sort 0 56 | i > 0; 57 | 58 | // S4 [Move R_i, decrease i] i <- i - 1 59 | --i 60 | ) { 61 | 62 | // S3 [Compare K:K_i] To S5 if K >= K_i 63 | if(K >= K_[i]) 64 | break; 65 | 66 | // S4 [Move R_i, decrease i] R_{i+1} <- R_i 67 | K_[i + 1] = K_[i]; 68 | 69 | } 70 | 71 | // S5 [R into R_{i+1}] R_{i+1} <- R 72 | K_[i + 1] = K; 73 | 74 | } 75 | 76 | } 77 | 78 | int main(int argc, char* argv[]) 79 | { 80 | 81 | if(argc > 1) { 82 | usage(); 83 | exit(0); 84 | } 85 | 86 | // read 64-bit size of data array as binary data 87 | uint64_t N; 88 | fread(&N, sizeof N, 1, stdin); 89 | 90 | // read array R of records as binary data 91 | // allocate N+1 entries to use array indexing from 1 instead of 0 92 | int64_t R[N + 1]; 93 | fread(&R[1], sizeof(*R), N, stdin); 94 | 95 | Sort(N, R); 96 | 97 | // write number of values to follow 98 | fwrite(&N, sizeof N, 1, stdout); 99 | 100 | // print sorted array as binary data 101 | fwrite(&R[1], sizeof(*R), N, stdout); 102 | 103 | return 0; 104 | } 105 | 106 | 107 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_d_shellsort/in.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_d_shellsort/in.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_d_shellsort/in.0.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5 3 | 3 4 | 1,3,5 5 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_d_shellsort/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_d_shellsort/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_d_shellsort/in.1.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 3 | 4 4 | 1,3,5,9 5 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_d_shellsort/out.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_d_shellsort/out.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_d_shellsort/out.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_d_shellsort/out.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_l_list_insertion/in.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_l_list_insertion/in.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_l_list_insertion/in.0.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_l_list_insertion/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_l_list_insertion/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_l_list_insertion/in.1.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_m_multiple_list_insertion/in.0.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  c -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_m_multiple_list_insertion/in.0.txt: -------------------------------------------------------------------------------- 1 | 7 2 | 4 3 | 10 4 | 5, 3, 2, 5, 7, 11, 0, 2, 99, 5 5 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_m_multiple_list_insertion/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_m_multiple_list_insertion/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_m_multiple_list_insertion/in.1.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 4 3 | 16 4 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 5 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_s_straight_insertion_sort/in.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_s_straight_insertion_sort/in.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_s_straight_insertion_sort/in.0.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_s_straight_insertion_sort/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_s_straight_insertion_sort/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.1_sorting_by_insertion/data/algorithm_s_straight_insertion_sort/in.1.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # sec_5.2.2_sorting_by_exchanging/CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.17) 4 | 5 | get_filename_component(COMPNAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) 6 | project(${COMPNAME}) 7 | 8 | add_executable(algorithm_b_bubble_sort algorithm_b_bubble_sort.c) 9 | add_executable(algorithm_m_merge_exchange algorithm_m_merge_exchange.c) 10 | 11 | add_executable(algorithm_q_quicksort algorithm_q_quicksort.c) 12 | add_executable(algorithm_q_quicksort.recursive algorithm_q_quicksort.recursive.c) 13 | add_executable(algorithm_r_radix_exchange_sort algorithm_r_radix_exchange_sort.c) 14 | add_executable(algorithm_r_radix_exchange_sort.recursive algorithm_r_radix_exchange_sort.recursive.c) 15 | 16 | if(CMAKE_C_COMPILER_ID MATCHES GNU) 17 | 18 | target_compile_options(algorithm_b_bubble_sort PRIVATE -g -Wall -Werror -O0 -std=c18) 19 | target_compile_options(algorithm_m_merge_exchange PRIVATE -g -Wall -Werror -O0 -std=c18) 20 | target_compile_options(algorithm_q_quicksort PRIVATE -g -Wall -Werror -O0 -std=c18) 21 | target_compile_options(algorithm_q_quicksort.recursive PRIVATE -g -Wall -Werror -O0 -std=c18) 22 | target_compile_options(algorithm_r_radix_exchange_sort PRIVATE -g -Wall -Werror -O0 -std=c18) 23 | target_compile_options(algorithm_r_radix_exchange_sort.recursive PRIVATE -g -Wall -Werror -O0 -std=c18) 24 | 25 | elseif(CMAKE_C_COMPILER_ID MATCHES MSVC) 26 | 27 | target_compile_options(algorithm_b_bubble_sort PRIVATE -Wall -WX -Od) 28 | target_compile_options(algorithm_m_merge_exchange PRIVATE -Wall -WX -Od) 29 | 30 | elseif(CMAKE_C_COMPILER_ID MATCHES Clang) 31 | 32 | target_compile_options(algorithm_b_bubble_sort PRIVATE -g -Wall -Werror -O0 -std=c18) 33 | target_compile_options(algorithm_m_merge_exchange PRIVATE -g -Wall -Werror -O0 -std=c18) 34 | 35 | endif() 36 | 37 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/algorithm_b_bubble_sort.c: -------------------------------------------------------------------------------- 1 | // algorithm_b_bubble_sort.c 2 | 3 | // Algorithm B (Bubble sort) 4 | // 5.2.2 Sorting by Exchanging 5 | // The Art of Computer Programming, Donald Knuth 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | static void usage() 14 | { 15 | puts("usage:algorithm_b_bubble_sort out.dat"); 16 | puts("Implements Algorithm B (Bubble sort), 5.2.2 Sorting by Exchanging, The Art of Computer Programming Volume 3, Sorting and Searching by Donald Knuth"); 17 | 18 | puts("reads 64-bit values as binary data to sort, outputs sorted 64-bit values as binary data"); 19 | 20 | puts("first uint64_t is number of values to sort"); 21 | puts("next that many int64_t is data to sort"); 22 | 23 | puts(""); 24 | puts("binary input data format"); 25 | puts("uint64_t N"); 26 | puts("int64_t[N] data"); 27 | 28 | puts(""); 29 | puts("binary output data format"); 30 | puts("uint64_t N"); 31 | puts("int64_t[N] sorted data"); 32 | 33 | puts(""); 34 | puts("examples:"); 35 | puts("algorithm_b_bubble_sort K_(j+1) 63 | if(K[j] <= K[j + 1]) 64 | continue; 65 | 66 | // B3 [Compare/exchange R_j : R_(j+1)] R_j <-> R_(j+1) 67 | uint64_t tmp = K[j]; 68 | K[j] = K[j + 1]; 69 | K[j + 1] = tmp; 70 | 71 | // B3 [Compare/exchange R_j : R_(j+1)] t <- j 72 | t = j; 73 | 74 | } 75 | 76 | // B4 [Any exchanges?] Stop if t = 0 77 | if(t == 0) 78 | break; 79 | } 80 | 81 | } 82 | 83 | int main(int argc, char* argv[]) 84 | { 85 | 86 | if(argc > 1) { 87 | usage(); 88 | exit(0); 89 | } 90 | 91 | // read 64-bit size of data array as binary data 92 | uint64_t N; 93 | fread(&N, sizeof N, 1, stdin); 94 | 95 | // read array R of records as binary data 96 | // allocate N+1 entries to use array indexing from 1 instead of 0 97 | int64_t R[N + 1]; 98 | fread(&R[1], sizeof(*R), N, stdin); 99 | 100 | Sort(N, R); 101 | 102 | // write number of values to follow 103 | fwrite(&N, sizeof N, 1, stdout); 104 | 105 | // print sorted array as binary data 106 | fwrite(&R[1], sizeof(*R), N, stdout); 107 | 108 | return 0; 109 | } 110 | 111 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/algorithm_m_merge_exchange.c: -------------------------------------------------------------------------------- 1 | // algorithm_m_merge_exchange.c 2 | 3 | // Algorithm M (Merge exchange sort) 4 | // Also solution to exercise 5.2.2.12 5 | // 5.2.2 Sorting by Exchanging 6 | // The Art of Computer Programming, Donald Knuth 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | static void usage() 16 | { 17 | puts("usage:algorithm_m_merge_exchange out.dat"); 18 | 19 | puts("reads 64-bit values as binary data to sort, outputs sorted 64-bit values as binary data"); 20 | 21 | puts("first uint64_t is number of values to sort"); 22 | puts("next that many int64_t is data to sort"); 23 | 24 | puts(""); 25 | puts("binary input data format"); 26 | puts("uint64_t N"); 27 | puts("int64_t[N] data"); 28 | 29 | puts(""); 30 | puts("binary output data format"); 31 | puts("uint64_t N"); 32 | puts("int64_t[N] sorted data"); 33 | 34 | puts(""); 35 | puts("examples:"); 36 | puts("algorithm_m_merge_exchange = 2 46 | if(N < 2) { 47 | return; 48 | } 49 | 50 | // M1 [Initialize p] t <- ceil(lg N) 51 | uint64_t t = ceil(log2(N)); 52 | 53 | // loop on p over powers of 2 from 2^(t - 1) down to 1 54 | for( 55 | 56 | // M1 [Initialize p] p <- 2^(t - 1) 57 | uint64_t p = pow(2.0, t - 1); 58 | 59 | // M6 [Loop on p] To M2 if p > 0 60 | p > 0; 61 | 62 | // M6 [Loop on p] p <- floor(p / 2) 63 | p /= 2 64 | ) { 65 | 66 | // loop on q also on powers of 2 from 2^(t - 1) down to p 67 | for( 68 | 69 | // d is step size for comparing pairs of keys 70 | // M2 [Initialize q, r, d] q <- 2^(t - 1), r <- 0, d <- p 71 | uint64_t q = pow(2.0, t - 1), r = 0, d = p; 72 | 73 | // loop runs while q != p after first iteration 74 | // could be structured as do-while or unrolled into first pass and loop with guard 75 | // chose to terminate with break below 76 | ; 77 | 78 | // M5 [Loop on q] To M3 after d <- q - p, q <- q / 2, r <- p 79 | d = q - p, q /= 2, r = p 80 | ) { 81 | 82 | // loop on i from start of array upto index that allows comparisons of pairs that are d-apart 83 | 84 | // M3 [Loop on i] Perform M4 for 0 <= i < N - d and i & p = r 85 | for(uint64_t i = 0; i < N - d; ++i) { 86 | 87 | // M3 [Loop on i] Perform M4 if i & p = r 88 | if((i & p) != r) 89 | continue; 90 | 91 | // M4 [Compare/exchange R_(i + 1) : R_i(i + d +1)] Swap if K_(i + 1) > K_(i + d + 1) 92 | if(K[i + 1] <= K[i + d + 1]) 93 | continue; 94 | 95 | // M4 [Compare/exchange R_(i + 1) : R_i(i + d +1)] R_(i + 1) <-> R_i(i + d +1) 96 | uint64_t tmp = K[i + 1]; 97 | K[i + 1] = K[i + d + 1]; 98 | K[i + d + 1] = tmp; 99 | 100 | } 101 | 102 | // M3 [Loop on i] To M5 103 | 104 | // M5 [Loop on q] Continue if q != p 105 | if(q == p) 106 | break; 107 | 108 | } 109 | 110 | } 111 | 112 | } 113 | 114 | int main(int argc, char* argv[]) 115 | { 116 | 117 | if(argc > 1) { 118 | usage(); 119 | exit(0); 120 | } 121 | 122 | // read 64-bit size of data array as binary data 123 | uint64_t N; 124 | fread(&N, sizeof N, 1, stdin); 125 | 126 | // read array R of records as binary data 127 | // allocate N+1 entries to use array indexing from 1 instead of 0 128 | int64_t R[N + 1]; 129 | fread(&R[1], sizeof(*R), N, stdin); 130 | 131 | Sort(N, R); 132 | 133 | // write number of values to follow 134 | fwrite(&N, sizeof N, 1, stdout); 135 | 136 | // print sorted array as binary data 137 | fwrite(&R[1], sizeof(*R), N, stdout); 138 | 139 | return 0; 140 | } 141 | 142 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/algorithm_r_radix_exchange_sort.c: -------------------------------------------------------------------------------- 1 | // algorithm_r_radix_exchange_sort.c 2 | 3 | // Algorithm R (Radix exchange sort) 4 | // 5.2.2 Sorting by Exchanging 5 | // The Art of Computer Programming, Donald Knuth 6 | // uses stack as described in algorithm 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | // algorithm uses a stack to accumulate right partitions and defer their 16 | // processing till a left partition is completely processed 17 | // this is why there is no need to keep the left boundary of a partition 18 | // in the entry stored on the stack 19 | 20 | // one stage of the algorithm scans a partition first rightward from the left 21 | // then leftward from the right looking for pairs of keys to swap 22 | // each key is tested for a specific bit number in one stage 23 | // a key with a 1 bit found in the scan from the left is swapped with a key 24 | // with a 0 bit found in the scan from the right 25 | // i.e. goal is to keep smaller keys (0 bit) on the left and bigger keys (1 bit) 26 | // on the right splitting the partition into two sections 27 | // so a stage corresponds to a current partition and specific bit number to test 28 | 29 | static void usage() 30 | { 31 | puts("usage:algorithm_r_radix_exchange_sort out.dat"); 32 | 33 | puts("reads nonegative 64-bit values as binary data to sort, outputs sorted 64-bit values as binary data"); 34 | 35 | puts("first uint64_t is max number of bits needed for values"); 36 | puts("second uint64_t is number of values to sort"); 37 | puts("next that many uint64_t is data to sort"); 38 | 39 | puts(""); 40 | puts("binary input data format"); 41 | puts("uint64_t m"); 42 | puts("uint64_t N"); 43 | puts("uint64_t[N] data"); 44 | 45 | puts(""); 46 | puts("binary output data format"); 47 | puts("uint64_t N"); 48 | puts("uint64_t[N] sorted data"); 49 | 50 | puts(""); 51 | puts("examples:"); 52 | puts("algorithm_r_radix_exchange_sort j 128 | if(!found_swap_pair) { 129 | break; 130 | } 131 | 132 | // R7 [Exchange R_i, R_(j + 1)] 133 | uint64_t tmp = K[i]; 134 | K[i] = K[j + 1]; 135 | K[j + 1] = tmp; 136 | 137 | found_swap_pair = false; 138 | 139 | } 140 | 141 | // one partitioning stage has completed 142 | 143 | // R8 [Test special cases] b <- b + 1 144 | b >>= 1; 145 | 146 | // R8 [Test special cases] To R10 if b > m where m is number of bits in keys 147 | if(b == 0) { 148 | break; 149 | } 150 | 151 | // R8 [Test special cases] To R2 if j < l or j = r 152 | if(j < l || j == r) { 153 | continue; 154 | } 155 | 156 | // R8 [Test special cases] To R2 if j = l, with l <- l + 1 157 | if(j == l) { 158 | ++l; 159 | continue; 160 | } 161 | 162 | // R9 [Put on stack] (r, b) => stack, to R2 with r <- j 163 | // abort on stack overflow 164 | if(++STACK_SIZE >= STACK_MAX) { 165 | fprintf(stderr, "Unexpected stack overflow, is input data valid? Or there's a serious bug in the program!\n"); 166 | abort(); 167 | } 168 | stack[STACK_SIZE - 1] = (struct entry_t){r, b}; 169 | 170 | r = j; 171 | 172 | } 173 | 174 | // R10 [Take off stack] Done if stack is empty 175 | if(STACK_SIZE == 0) { 176 | break; 177 | } 178 | 179 | // R10 [Take off stack] To R2, l <- r + 1, (r', b') <= stack, r <- r', b <- b' 180 | l = r + 1; 181 | 182 | struct entry_t top = stack[STACK_SIZE - 1]; 183 | --STACK_SIZE; 184 | 185 | r = top.r; 186 | b = top.b; 187 | 188 | } 189 | 190 | } 191 | 192 | int main(int argc, char* argv[]) 193 | { 194 | 195 | if(argc > 1) { 196 | usage(); 197 | exit(0); 198 | } 199 | 200 | // read 64-bit max number of bits as binary data 201 | uint64_t m; 202 | fread(&m, sizeof m, 1, stdin); 203 | 204 | // read 64-bit size of data array as binary data 205 | uint64_t N; 206 | fread(&N, sizeof N, 1, stdin); 207 | 208 | // read array R of records as binary data 209 | uint64_t R[N + 1]; 210 | 211 | fread(&R[1], sizeof(*R), N, stdin); 212 | 213 | Sort(N, R, m); 214 | 215 | // write number of values to follow 216 | fwrite(&N, sizeof N, 1, stdout); 217 | 218 | // print sorted array as binary data 219 | fwrite(&R[1], sizeof(*R), N, stdout); 220 | 221 | return 0; 222 | } 223 | 224 | 225 | 226 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/algorithm_r_radix_exchange_sort.recursive.c: -------------------------------------------------------------------------------- 1 | // algorithm_r_radix_exchange_sort.recursive.c 2 | 3 | 4 | // Algorithm R (Radix exchange sort) 5 | // 5.2.2 Sorting by Exchanging 6 | // The Art of Computer Programming, Donald Knuth 7 | // uses recursion instead of explicit stack in algorithm 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | static void usage() 17 | { 18 | puts("usage:algorithm_r_radix_exchange_sort.recursive out.dat"); 19 | 20 | puts("reads nonnegative 64-bit values as binary data to sort, outputs sorted 64-bit values as binary data"); 21 | 22 | puts("first uint64_t is max number of bits needed for values"); 23 | puts("second uint64_t is number of values to sort"); 24 | puts("next that many uint64_t is data to sort"); 25 | 26 | puts(""); 27 | puts("binary input data format"); 28 | puts("uint64_t m"); 29 | puts("uint64_t N"); 30 | puts("uint64_t[N] data"); 31 | 32 | puts(""); 33 | puts("binary output data format"); 34 | puts("uint64_t N"); 35 | puts("uint64_t[N] sorted data"); 36 | 37 | puts(""); 38 | puts("examples:"); 39 | puts("algorithm_r_radix_exchange_sort.recursive j 91 | if(!found_swap_pair) { 92 | break; 93 | } 94 | 95 | // R7 [Exchange R_i, R_(j + 1)] 96 | uint64_t tmp = K[i]; 97 | K[i] = K[j + 1]; 98 | K[j + 1] = tmp; 99 | 100 | found_swap_pair = false; 101 | 102 | } 103 | 104 | // one partitioning stage has completed 105 | 106 | // R8 [Test special cases] 107 | b >>= 1; 108 | 109 | // R8 [Test special cases] To R10 if b > m where m is number of bits in keys 110 | if(b == 0) { 111 | break; 112 | } 113 | 114 | // R8 [Test special cases] To R2 if j < l or j = r 115 | if(j < l || j == r) { 116 | continue; 117 | } 118 | 119 | if(j == l) { 120 | ++l; 121 | continue; 122 | } 123 | 124 | // R9 [Put on stack] (r, b) => stack, to R2 with r <- j 125 | uint64_t result = R2Stage(l, j, b, N, K); 126 | 127 | // R10 [Take off stack] To R2, l <- r + 1, (r, b) <= stack 128 | l = result + 1; 129 | 130 | } 131 | 132 | // R10 [Take off stack] 133 | return r; 134 | 135 | } 136 | 137 | // Sort takes array K of N unsigned keys beginning at K[1] 138 | // Sort implements Algorithm R (Radix exchange sort) 139 | // K is sorted in place 140 | // m is max number of bits needed for range of keys 141 | void Sort(const uint64_t N, uint64_t K[N + 1], const uint64_t m) 142 | { 143 | 144 | // R1 [initialize] Set the stack empty, l <- 1, r <- N, b <- 1 145 | // recursion stack is empty 146 | const uint64_t l = 1; 147 | const uint64_t r = N; 148 | const uint64_t b = 1ul << (m - 1); 149 | 150 | // R2 [Begin new stage] 151 | R2Stage(l, r, b, N, K); 152 | 153 | } 154 | 155 | int main(int argc, char* argv[]) 156 | { 157 | 158 | if(argc > 1) { 159 | usage(); 160 | exit(0); 161 | } 162 | 163 | // read 64-bit max number of bits as binary data 164 | uint64_t m; 165 | fread(&m, sizeof m, 1, stdin); 166 | 167 | // read 64-bit size of data array as binary data 168 | uint64_t N; 169 | fread(&N, sizeof N, 1, stdin); 170 | 171 | // read array R of records as binary data 172 | uint64_t R[N + 1]; 173 | 174 | fread(&R[1], sizeof(*R), N, stdin); 175 | 176 | Sort(N, R, m); 177 | 178 | // write number of values to follow 179 | fwrite(&N, sizeof N, 1, stdout); 180 | 181 | // print sorted array as binary data 182 | fwrite(&R[1], sizeof(*R), N, stdout); 183 | 184 | return 0; 185 | } 186 | 187 | 188 | 189 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/in.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/in.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/in.0.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/in.1.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/in.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/in.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/in.2.txt: -------------------------------------------------------------------------------- 1 | 17 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5, 0, 2, 2, 2, 3, 3, 4 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/in.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |     -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/in.3.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 1, 3, 2, 4, 10, 5, 11, 6, 13, 7, 14, 8, 15, 9, 16, 12 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/in.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/in.4.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 5, 1, 4, 2, 8 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/out.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/out.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/out.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/out.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/out.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/out.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/out.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_b_bubble_sort/out.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/in.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/in.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/in.0.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/in.1.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/in.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/in.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/in.2.txt: -------------------------------------------------------------------------------- 1 | 17 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5, 0, 2, 2, 2, 3, 3, 4 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/in.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |     -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/in.3.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 1, 3, 2, 4, 10, 5, 11, 6, 13, 7, 14, 8, 15, 9, 16, 12 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/out.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/out.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/out.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/out.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/out.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/out.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_m_merge_exchange/out.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/in.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/in.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/in.0.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/in.1.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/in.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/in.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/in.2.txt: -------------------------------------------------------------------------------- 1 | 17 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5, 0, 2, 2, 2, 3, 3, 4 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/in.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |     -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/in.3.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 1, 3, 2, 4, 10, 5, 11, 6, 13, 7, 14, 8, 15, 9, 16, 12 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/in.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/in.4.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 5, 1, 4, 2, 8 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/out.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/out.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/out.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/out.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/out.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/out.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/out.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort.recursive/out.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/in.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/in.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/in.0.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/in.1.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/in.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/in.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/in.2.txt: -------------------------------------------------------------------------------- 1 | 17 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5, 0, 2, 2, 2, 3, 3, 4 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/in.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |     -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/in.3.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 1, 3, 2, 4, 10, 5, 11, 6, 13, 7, 14, 8, 15, 9, 16, 12 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/in.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/in.4.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 5, 1, 4, 2, 8 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/out.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/out.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/out.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/out.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/out.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/out.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/out.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_q_quicksort/out.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort.recursive/in.0.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  c -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort.recursive/in.0.txt: -------------------------------------------------------------------------------- 1 | 7 2 | 10 3 | 5, 3, 2, 5, 7, 11, 0, 2, 99, 5 4 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort.recursive/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort.recursive/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort.recursive/in.1.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 16 3 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 4 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort.recursive/in.2.le.dat: -------------------------------------------------------------------------------- 1 |  c -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort.recursive/in.2.txt: -------------------------------------------------------------------------------- 1 | 7 2 | 17 3 | 5, 3, 2, 5, 7, 11, 0, 2, 99, 5, 0, 2, 2, 2, 3, 3, 4 4 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort.recursive/in.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |     -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort.recursive/in.3.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 16 3 | 1, 3, 2, 4, 10, 5, 11, 6, 13, 7, 14, 8, 15, 9, 16, 12 4 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort.recursive/in.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort.recursive/in.4.txt: -------------------------------------------------------------------------------- 1 | 4 2 | 5 3 | 5, 1, 4, 2, 8 4 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort.recursive/out.0.le.dat: -------------------------------------------------------------------------------- 1 | 2 |  c -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort.recursive/out.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort.recursive/out.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort.recursive/out.2.le.dat: -------------------------------------------------------------------------------- 1 |  c -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort.recursive/out.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort.recursive/out.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort/in.0.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  c -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort/in.0.txt: -------------------------------------------------------------------------------- 1 | 7 2 | 10 3 | 5, 3, 2, 5, 7, 11, 0, 2, 99, 5 4 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort/in.1.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 16 3 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 4 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort/in.2.le.dat: -------------------------------------------------------------------------------- 1 |  c -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort/in.2.txt: -------------------------------------------------------------------------------- 1 | 7 2 | 17 3 | 5, 3, 2, 5, 7, 11, 0, 2, 99, 5, 0, 2, 2, 2, 3, 3, 4 4 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort/in.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |     -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort/in.3.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 16 3 | 1, 3, 2, 4, 10, 5, 11, 6, 13, 7, 14, 8, 15, 9, 16, 12 4 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort/in.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort/in.4.txt: -------------------------------------------------------------------------------- 1 | 4 2 | 5 3 | 5, 1, 4, 2, 8 4 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort/out.0.le.dat: -------------------------------------------------------------------------------- 1 | 2 |  c -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort/out.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort/out.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort/out.2.le.dat: -------------------------------------------------------------------------------- 1 |  c -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort/out.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.2_sorting_by_exchanging/data/algorithm_r_radix_exchange_sort/out.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # sec_5.2.3_sorting_by_selection/CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.17) 4 | 5 | get_filename_component(COMPNAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) 6 | project(${COMPNAME}) 7 | 8 | add_executable(algorithm_s_straight_selection_sort algorithm_s_straight_selection_sort.c) 9 | 10 | if(CMAKE_C_COMPILER_ID MATCHES GNU) 11 | 12 | target_compile_options(algorithm_s_straight_selection_sort PRIVATE -g -Wall -Werror -O0 -std=c18) 13 | 14 | elseif(CMAKE_C_COMPILER_ID MATCHES MSVC) 15 | 16 | target_compile_options(algorithm_s_straight_selection_sort PRIVATE -Wall -WX -Od) 17 | 18 | elseif(CMAKE_C_COMPILER_ID MATCHES Clang) 19 | 20 | target_compile_options(algorithm_s_straight_selection_sort PRIVATE -g -Wall -Werror -O0 -std=c18) 21 | 22 | endif() 23 | 24 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/algorithm_s_straight_selection_sort.c: -------------------------------------------------------------------------------- 1 | // algorithm_s_straight_selection_sort.c 2 | 3 | // Algorithm S (Straight selection sort) 4 | // 5.2.3 Sorting by Selection 5 | // The Art of Computer Programming, Donald Knuth 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | static void usage() 15 | { 16 | puts("usage:algorithm_s_straight_selection_sort out.dat"); 17 | 18 | puts("reads 64-bit values as binary data to sort, outputs sorted 64-bit values as binary data"); 19 | 20 | puts("first uint64_t is number of values to sort"); 21 | puts("next that many uint64_t is data to sort"); 22 | 23 | puts(""); 24 | puts("binary input data format"); 25 | puts("uint64_t N"); 26 | puts("int64_t[N] data"); 27 | 28 | puts(""); 29 | puts("binary output data format"); 30 | puts("uint64_t N"); 31 | puts("int64_t[N] sorted data"); 32 | 33 | puts(""); 34 | puts("examples:"); 35 | puts("algorithm_s_straight_selection_sort = 2; --j) { 46 | 47 | // S2 [Find max(K_1,...,K_j)] Find maximal K_i among K_j, K_(j-1),...,K_1 where i is as large as possible 48 | uint64_t i = j; 49 | 50 | for(uint64_t l = j - 1; l >= 1; --l) { 51 | if(K[l] > K[i]) { 52 | i = l; 53 | } 54 | } 55 | 56 | // S3 [Exchange with R_j] R_i <-> R_j 57 | int64_t tmp = K[j]; 58 | K[j] = K[i]; 59 | K[i] = tmp; 60 | } 61 | 62 | } 63 | 64 | int main(int argc, char* argv[]) 65 | { 66 | 67 | if(argc > 1) { 68 | usage(); 69 | exit(0); 70 | } 71 | 72 | // read 64-bit size of data array as binary data 73 | uint64_t N; 74 | fread(&N, sizeof N, 1, stdin); 75 | 76 | // read array R of records as binary data 77 | int64_t R[N + 1]; 78 | 79 | fread(&R[1], sizeof(*R), N, stdin); 80 | 81 | Sort(N, R); 82 | 83 | // write number of values to follow 84 | fwrite(&N, sizeof N, 1, stdout); 85 | 86 | // print sorted array as binary data 87 | fwrite(&R[1], sizeof(*R), N, stdout); 88 | 89 | return 0; 90 | } 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/in.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/in.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/in.0.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/in.1.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/in.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/in.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/in.2.txt: -------------------------------------------------------------------------------- 1 | 17 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5, 0, 2, 2, 2, 3, 3, 4 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/in.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |     -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/in.3.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 1, 3, 2, 4, 10, 5, 11, 6, 13, 7, 14, 8, 15, 9, 16, 12 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/in.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/in.4.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 5, 1, 4, 2, 8 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/in.5.le.dat: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/in.5.txt: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/in.6.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/in.6.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 13 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/out.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/out.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/out.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/out.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/out.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/out.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/out.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/out.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/out.5.dat: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.3_sorting_by_selection/data/algorithm_s_straight_selection_sort/out.6.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # sec_5.2.4_sorting_by_merging/CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.17) 4 | 5 | get_filename_component(COMPNAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) 6 | project(${COMPNAME}) 7 | 8 | add_executable(algorithm_m_two_way_merge algorithm_m_two_way_merge.c) 9 | add_executable(algorithm_n_natural_two_way_merge_sort algorithm_n_natural_two_way_merge_sort.c) 10 | add_executable(algorithm_s_straight_two_way_merge_sort algorithm_s_straight_two_way_merge_sort.c) 11 | add_executable(algorithm_l_list_merge_sort algorithm_l_list_merge_sort.c) 12 | 13 | if(CMAKE_C_COMPILER_ID MATCHES GNU) 14 | 15 | target_compile_options(algorithm_m_two_way_merge PRIVATE -g -Wall -Werror -O0 -std=c18) 16 | target_compile_options(algorithm_n_natural_two_way_merge_sort PRIVATE -g -Wall -Werror -O0 -std=c18) 17 | target_compile_options(algorithm_s_straight_two_way_merge_sort PRIVATE -g -Wall -Werror -Wextra -O0 -std=c18) 18 | target_compile_options(algorithm_l_list_merge_sort PRIVATE -g -Wall -Werror -Wextra -O0 -std=c18) 19 | 20 | elseif(CMAKE_C_COMPILER_ID MATCHES MSVC) 21 | 22 | target_compile_options(algorithm_m_two_way_merge PRIVATE -Wall -WX -Od) 23 | target_compile_options(algorithm_n_natural_two_way_merge_sort PRIVATE -Wall -WX -Od) 24 | 25 | elseif(CMAKE_C_COMPILER_ID MATCHES Clang) 26 | 27 | target_compile_options(algorithm_m_two_way_merge PRIVATE -g -Wall -Werror -O0 -std=c18) 28 | target_compile_options(algorithm_n_natural_two_way_merge_sort PRIVATE -g -Wall -Werror -O0 -std=c18) 29 | 30 | endif() 31 | 32 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/algorithm_l_list_merge_sort.c: -------------------------------------------------------------------------------- 1 | // algorithm_l_list_merge_sort.c 2 | 3 | // Algorithm L (List merge sort) 4 | // 5.2.4 Sorting by Merging 5 | // The Art of Computer Programming, Donald Knuth 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | static void usage() 16 | { 17 | puts("usage:algorithm_l_list_merge_sort out.dat"); 18 | puts("Implements Algorithm L (List merge sort), 5.2.4 Sorting by Merging, The Art of Computer Programming Volume 3, Sorting and Searching by Donald Knuth"); 19 | 20 | puts("reads 64-bit values as binary data to sort, outputs sorted 64-bit values as binary data"); 21 | 22 | puts("first uint64_t is number of values to sort"); 23 | puts("next that many int64_t is data to sort"); 24 | 25 | puts(""); 26 | puts("binary input data format"); 27 | puts("uint64_t N"); 28 | puts("int64_t[N] data"); 29 | 30 | puts(""); 31 | puts("binary output data format"); 32 | puts("uint64_t N"); 33 | puts("int64_t[N] sorted data"); 34 | 35 | puts(""); 36 | puts("examples:"); 37 | puts("algorithm_l_list_merge_sort K_q 97 | if(R[p].K > R[q].K) { 98 | 99 | // L6 [Advance q] |L_s| <- q, s <- q, q <- L_q 100 | R[s].L = copysign(q, R[s].L); 101 | s = q; 102 | q = R[q].L; 103 | 104 | // L6 [Advance q] To L3 if q > 0 105 | if(q > 0) 106 | continue; 107 | 108 | // L7 [Complete the sublist] L_s <- p, s <- t 109 | R[s].L = p; 110 | s = t; 111 | 112 | // L7 [Complete the sublist] t <- p, p <- L_p till p <= 0 113 | do { 114 | t = p; 115 | p = R[p].L; 116 | } while(p > 0); 117 | 118 | } else { 119 | 120 | // L4 [Advance p] |L_s| <- p, s <- p, p <- L_p 121 | R[s].L = copysign(p, R[s].L); 122 | s = p; 123 | p = R[p].L; 124 | 125 | // L4 [Advance p] To L3 if p > 0 126 | if(p > 0) 127 | continue; 128 | 129 | // L5 [Complete the sublist] L_s <- q, s <- t 130 | R[s].L = q; 131 | s = t; 132 | 133 | // L5 [Complete the sublist] t <- q, q <- L_q till q <= 0 134 | do { 135 | t = q; 136 | q = R[q].L; 137 | } while(q > 0); 138 | // L5 [Complete the sublist] To L8 139 | } 140 | 141 | // L8 [End of pass?] p <- -p, q <- -q 142 | p = -p; 143 | q = -q; 144 | 145 | // L8 [End of pass?] |L_s| <- p, |L_t| <- 0 146 | if(q == 0) { 147 | R[s].L = copysign(p, R[s].L); 148 | R[t].L = 0; 149 | // L8 [End of pass?] To L2 150 | break; 151 | } 152 | 153 | // L8 [End of pass?] To L3 154 | 155 | } 156 | 157 | } 158 | 159 | } 160 | 161 | int main(int argc, char* argv[]) 162 | { 163 | 164 | (void)argv; 165 | 166 | if(argc > 1) { 167 | usage(); 168 | exit(0); 169 | } 170 | 171 | // read 64-bit size of data array as binary data 172 | uint64_t N; 173 | fread(&N, sizeof N, 1, stdin); 174 | 175 | // read array R of records as binary data 176 | // allocate N+2 entries for extra artificial records at index 0 and N+1 177 | struct Record R[N + 2]; 178 | 179 | // fill key field of each record 180 | for(uint64_t i = 1; i <= N; ++i) { 181 | fread(&R[i].K, sizeof(R[i].K), 1, stdin); 182 | } 183 | 184 | Sort(R, N); 185 | 186 | // write number of values to follow 187 | fwrite(&N, sizeof N, 1, stdout); 188 | 189 | 190 | // traverse linked list to print sorted keys as binary data 191 | for(uint64_t i = R[0].L; i != 0; i = R[i].L) { 192 | fwrite(&R[i].K, sizeof(R[i].K), 1, stdout); 193 | } 194 | 195 | return 0; 196 | } 197 | 198 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/algorithm_l_list_merge_sort.signbit.c: -------------------------------------------------------------------------------- 1 | // algorithm_l_list_merge_sort.signbit.c 2 | 3 | // Algorithm L (List merge sort) 4 | // 5.2.4 Sorting by Merging 5 | // The Art of Computer Programming, Donald Knuth 6 | // The MMIX Supplement, Martin Ruckert 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | 16 | static void usage() 17 | { 18 | puts("usage:algorithm_l_list_merge_sort.signbit out.dat"); 19 | puts("Implements Algorithm L (List merge sort), 5.2.4 Sorting by Merging, The Art of Computer Programming Volume 3, Sorting and Searching by Donald Knuth"); 20 | 21 | puts("reads 64-bit values as binary data to sort, outputs sorted 64-bit values as binary data"); 22 | 23 | puts("first uint64_t is number of values to sort"); 24 | puts("next that many int64_t is data to sort"); 25 | 26 | puts(""); 27 | puts("binary input data format"); 28 | puts("uint64_t N"); 29 | puts("int64_t[N] data"); 30 | 31 | puts(""); 32 | puts("binary output data format"); 33 | puts("uint64_t N"); 34 | puts("int64_t[N] sorted data"); 35 | 36 | puts(""); 37 | puts("examples:"); 38 | puts("algorithm_l_list_merge_sort.signbit = 2 67 | 68 | // L1 [Prepare two lists] L_0 <- 1, L_(N + 1) <- 2 69 | // L_0 is head of list with odd indexes, R_1, R_3, R_5, ... 70 | R[0].L = 1; 71 | 72 | // L_(N + 1) is head of list with even indexes, R_2, R_4, R_6, ... 73 | R[N + 1].L = 2; 74 | 75 | // L1 [Prepare two lists] L_i <- -(i + 2) for 1 <= i <= N - 2 76 | for(int64_t i = 1; i + 2 <= (int64_t)N; ++i) { 77 | R[i].L = -(i + 2); 78 | } 79 | 80 | // L1 [Prepare two lists] L_(N - 1) <- L_N <- 0 81 | // R_(N - 1) and R_N are the end of the two lists 82 | R[N - 1].L = R[N].L = 0; 83 | 84 | for(;;) { 85 | 86 | // L2 [Begin new pass] s <- 0, t <- N + 1, p <- L_s, q <- L_t 87 | // the heads of the two lists 88 | uint64_t s = 0; 89 | uint64_t t = N + 1; 90 | 91 | int64_t p = R[s].L; 92 | 93 | int64_t q = R[t].L; 94 | 95 | // L2 [Begin new pass] Terminate if q = 0 96 | if(q == 0) 97 | return; 98 | 99 | for(;;) { 100 | 101 | // L3 [Compare K_p:K_q] To L6 if K_p > K_q 102 | if(R[p].K > R[q].K) { 103 | 104 | // L6 [Advance q] |L_s| <- q, s <- q, q <- L_q 105 | R[s].L = copysign(q, R[s].L); 106 | s = q; 107 | q = R[q].L; 108 | 109 | // L6 [Advance q] To L3 if q > 0 110 | if(q > 0) 111 | continue; 112 | 113 | // L7 [Complete the sublist] L_s <- p, s <- t 114 | R[s].L = p; 115 | s = t; 116 | 117 | // L7 [Complete the sublist] t <- p, p <- L_p till p <= 0 118 | do { 119 | t = p; 120 | p = R[p].L; 121 | } while(p > 0); 122 | 123 | } else { 124 | 125 | // L4 [Advance p] |L_s| <- p, s <- p, p <- L_p 126 | R[s].L = copysign(p, R[s].L); 127 | s = p; 128 | p = R[p].L; 129 | 130 | // L4 [Advance p] To L3 if p > 0 131 | if(p > 0) 132 | continue; 133 | 134 | // L5 [Complete the sublist] L_s <- q, s <- t 135 | R[s].L = q; 136 | s = t; 137 | 138 | // L5 [Complete the sublist] t <- q, q <- L_q till q <= 0 139 | do { 140 | t = q; 141 | q = R[q].L; 142 | } while(q > 0); 143 | 144 | // L5 [Complete the sublist] To L8 145 | } 146 | 147 | // L8 [End of pass?] p <- -p, q <- -q 148 | p = -p; 149 | q = -q; 150 | 151 | // L8 [End of pass?] |L_s| <- p, |L_t| <- 0 152 | if(q == 0) { 153 | R[s].L = copysign(p, R[s].L); 154 | //R[t].L = 0; 155 | R[t].L = copysign(0, R[t].L); 156 | // L8 [End of pass?] To L2 157 | break; 158 | } 159 | 160 | // L8 [End of pass?] To L3 161 | 162 | } 163 | 164 | } 165 | 166 | } 167 | 168 | int main(int argc, char* argv[]) 169 | { 170 | 171 | (void)argv; 172 | 173 | if(argc > 1) { 174 | usage(); 175 | exit(0); 176 | } 177 | 178 | // read 64-bit size of data array as binary data 179 | uint64_t N; 180 | fread(&N, sizeof N, 1, stdin); 181 | 182 | // read array R of records as binary data 183 | // allocate N+2 entries for extra artificial records at index 0 and N+1 184 | struct Record R[N + 2]; 185 | 186 | // fill key field of each record 187 | for(uint64_t i = 1; i <= N; ++i) { 188 | fread(&R[i].K, sizeof(R[i].K), 1, stdin); 189 | } 190 | 191 | Sort(R, N); 192 | 193 | // write number of values to follow 194 | fwrite(&N, sizeof N, 1, stdout); 195 | 196 | 197 | // traverse linked list to print sorted keys as binary data 198 | for(uint64_t i = R[0].L; i != 0; i = R[i].L) { 199 | fwrite(&R[i].K, sizeof(R[i].K), 1, stdout); 200 | } 201 | 202 | return 0; 203 | } 204 | 205 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/algorithm_m_two_way_merge.c: -------------------------------------------------------------------------------- 1 | // algorithm_m_two_way_merge.c 2 | 3 | // Algorithm M (Two-way merge) 4 | // 5.2.4 Sorting by Merging 5 | // The Art of Computer Programming, Donald Knuth 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | static void usage() 15 | { 16 | puts("usage:algorithm_m_two_way_merge out.dat"); 17 | 18 | puts("reads 64-bit values as binary data to sort, outputs sorted 64-bit values as binary data"); 19 | 20 | puts("first uint64_t is number of values to sort"); 21 | puts("next that many int64_t is data to sort"); 22 | 23 | puts(""); 24 | puts("binary input data format"); 25 | puts("uint64_t m, can be 0 for empty array"); 26 | puts("int64_t[m] data"); 27 | puts("uint64_t n, can be 0 for empty array"); 28 | puts("int64_t[n] data"); 29 | 30 | puts(""); 31 | puts("binary output data format"); 32 | puts("uint64_t N"); 33 | puts("int64_t[N] sorted data"); 34 | 35 | puts(""); 36 | puts("examples:"); 37 | puts("algorithm_m_two_way_merge 1) { 110 | usage(); 111 | exit(0); 112 | } 113 | 114 | uint64_t m; 115 | fread(&m, sizeof m, 1, stdin); 116 | 117 | // read array X of values as binary data 118 | int64_t X[m + 1]; 119 | 120 | fread(&X[1], sizeof(*X), m, stdin); 121 | 122 | // read 64-bit size of Y array as binary data 123 | uint64_t n; 124 | fread(&n, sizeof n, 1, stdin); 125 | 126 | // read array Y of values as binary data 127 | int64_t Y[n + 1]; 128 | 129 | fread(&Y[1], sizeof(*Y), n, stdin); 130 | 131 | int64_t Z[m + n + 1]; 132 | 133 | merge(X, m, Y, n, Z); 134 | 135 | // write number of values to follow 136 | const uint64_t zsize = m + n; 137 | fwrite(&zsize, sizeof zsize, 1, stdout); 138 | 139 | // print sorted array as binary data 140 | fwrite(&Z[1], sizeof(*Z), zsize, stdout); 141 | 142 | return 0; 143 | } 144 | 145 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/in.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/in.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/in.0.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/in.1.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/in.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/in.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/in.2.txt: -------------------------------------------------------------------------------- 1 | 17 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5, 0, 2, 2, 2, 3, 3, 4 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/in.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |     -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/in.3.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 1, 3, 2, 4, 10, 5, 11, 6, 13, 7, 14, 8, 15, 9, 16, 12 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/in.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/in.4.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 5, 1, 4, 2, 8 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/in.5.le.dat: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/in.5.txt: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/in.6.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/in.6.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 13 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/out.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/out.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/out.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/out.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/out.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/out.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/out.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/out.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/out.5.dat: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort.signbit/out.6.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/in.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/in.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/in.0.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/in.1.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/in.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/in.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/in.2.txt: -------------------------------------------------------------------------------- 1 | 17 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5, 0, 2, 2, 2, 3, 3, 4 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/in.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |     -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/in.3.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 1, 3, 2, 4, 10, 5, 11, 6, 13, 7, 14, 8, 15, 9, 16, 12 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/in.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/in.4.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 5, 1, 4, 2, 8 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/in.5.le.dat: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/in.5.txt: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/in.6.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/in.6.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 13 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/out.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/out.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/out.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/out.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/out.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/out.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/out.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/out.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/out.5.dat: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_l_list_merge_sort/out.6.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.0.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 503, 703, 765 3 | 3 4 | 87, 512, 677 5 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.1.txt: -------------------------------------------------------------------------------- 1 | 10 2 | -3, 2, 2, 3, 5, 5, 5, 7, 11, 99 3 | 16 4 | 61, 87, 154, 170, 275, 426, 503, 509, 512, 612, 653, 677, 703, 765, 897, 908 5 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.2.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 61, 87, 154, 170, 275, 426, 503, 509, 512, 612, 653, 677, 703, 765, 897, 908 3 | 17 4 | -3, 0, 2, 2, 2, 2, 2, 3, 3, 3, 4, 5, 5, 5, 7, 11, 99 5 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.3.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 1, 2, 4, 5, 8 3 | 16 4 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 5 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.4.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.4.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.4.txt: -------------------------------------------------------------------------------- 1 | 17 2 | -3, 0, 2, 2, 2, 2, 2, 3, 3, 3, 4, 5, 5, 5, 7, 11, 99 3 | 0 4 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.5.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.5.txt: -------------------------------------------------------------------------------- 1 | 0 2 | 5 3 | 1, 2, 4, 5, 8 4 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.6.le.dat: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_m_two_way_merge/in.6.txt: -------------------------------------------------------------------------------- 1 | 0 2 | 0 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/in.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/in.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/in.0.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/in.1.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/in.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/in.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/in.2.txt: -------------------------------------------------------------------------------- 1 | 17 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5, 0, 2, 2, 2, 3, 3, 4 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/in.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |     -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/in.3.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 1, 3, 2, 4, 10, 5, 11, 6, 13, 7, 14, 8, 15, 9, 16, 12 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/in.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/in.4.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 5, 1, 4, 2, 8 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/in.5.le.dat: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/in.5.txt: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/in.6.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/in.6.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 13 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/out.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/out.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/out.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/out.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/out.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/out.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/out.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/out.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/out.5.dat: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_n_natural_two_way_merge_sort/out.6.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/in.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/in.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/in.0.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/in.1.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/in.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/in.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/in.2.txt: -------------------------------------------------------------------------------- 1 | 17 2 | 5, 3, 2, 5, 7, 11, -3, 2, 99, 5, 0, 2, 2, 2, 3, 3, 4 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/in.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |     -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/in.3.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 1, 3, 2, 4, 10, 5, 11, 6, 13, 7, 14, 8, 15, 9, 16, 12 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/in.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/in.4.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 5, 1, 4, 2, 8 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/in.5.le.dat: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/in.5.txt: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/in.6.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/in.6.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 13 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/out.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/out.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/out.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/out.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/out.2.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/out.2.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/out.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/out.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/out.5.dat: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.4_sorting_by_merging/data/algorithm_s_straight_two_way_merge_sort/out.6.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # sec_5.2.5_sorting_by_distribution/CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.17) 4 | 5 | get_filename_component(COMPNAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) 6 | project(${COMPNAME}) 7 | 8 | add_executable(algorithm_r_radix_list_sort algorithm_r_radix_list_sort.c) 9 | 10 | if(CMAKE_C_COMPILER_ID MATCHES GNU) 11 | 12 | target_compile_options(algorithm_r_radix_list_sort PRIVATE -g -Wall -Werror -O0 -std=c18) 13 | 14 | elseif(CMAKE_C_COMPILER_ID MATCHES MSVC) 15 | 16 | target_compile_options(algorithm_r_radix_list_sort PRIVATE -Wall -WX -Od) 17 | 18 | elseif(CMAKE_C_COMPILER_ID MATCHES Clang) 19 | 20 | target_compile_options(algorithm_r_radix_list_sort PRIVATE -g -Wall -Werror -O0 -std=c18) 21 | 22 | endif() 23 | 24 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/algorithm_r_radix_list_sort.c: -------------------------------------------------------------------------------- 1 | // algorithm_r_radix_list_sort.c 2 | 3 | // Algorithm R (Radix list sort) 4 | // 5.2.5 Sorting by Distribution 5 | // The Art of Computer Programming, Donald Knuth 6 | // The MMIX Supplement, Martin Ruckert 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | 16 | static void usage() 17 | { 18 | puts("usage:algorithm_r_radix_list_sort out.dat"); 19 | puts("Implements Algorithm R (Radix list sort), 5.2.5 Sorting by Distribution, The Art of Computer Programming Volume 3, Sorting and Searching by Donald Knuth"); 20 | 21 | puts("reads 64-bit values as binary data to sort, outputs sorted 64-bit values as binary data"); 22 | 23 | puts("first uint64_t is base or radix M to use, currently only 256 is supported"); 24 | puts("second uint64_t is length p of values in base M, e.g. p = 2 means values fit into 2 bytes"); 25 | puts("third uint64_t is number of values to sort"); 26 | puts("next that many int64_t is data to sort"); 27 | 28 | puts(""); 29 | puts("binary input data format"); 30 | puts("uint64_t M"); 31 | puts("uint64_t p"); 32 | puts("uint64_t N"); 33 | puts("int64_t[N] data"); 34 | 35 | puts(""); 36 | puts("binary output data format"); 37 | puts("uint64_t N"); 38 | puts("int64_t[N] sorted data"); 39 | 40 | puts(""); 41 | puts("examples:"); 42 | puts("algorithm_r_radix_list_sort i is the next nonempty queue 57 | // the case of empty queue 0 is tricky and relies on the special way front and rear pointers are defined for these queues 58 | static void Hook( const uint64_t M; struct Record* TOP[M], struct Record* BOTM[M], const uint64_t M) 59 | { 60 | 61 | // H1 [Initialize] i <- 0 62 | uint64_t i = 0; 63 | 64 | // loop over M queues starting with queue 0 65 | // 66 | for(;;) { 67 | 68 | // H2 [Point to top of pile] P <- TOP[i] 69 | // tail of current queue 70 | struct Record* P = TOP[i]; 71 | 72 | // skip empty queues 73 | do { 74 | 75 | // H3 [Next pile] i <- i + 1 76 | ++i; 77 | 78 | // H3 [Next pile] LINK(P) <- lambda and terminate if i = M 79 | if(i == M) { 80 | P->LINK = NULL; 81 | return; 82 | } 83 | 84 | // H4 [Is pile empty?] To H3 if BOTM[i] = lambda 85 | } while(BOTM[i] == NULL); 86 | 87 | // H5 [Tie pile together] LINK(P) <- BOTM[i] 88 | // makes tail node of current queue point to head node of higher indexed nonempty queue 89 | P->LINK = BOTM[i]; 90 | 91 | // H5 [Tie pile together] To H2 92 | } 93 | 94 | } 95 | 96 | // Sort takes array R of N records 97 | // M is radix or base 98 | // p is length of key in base M 99 | // Sort implements Algorithm R (Radix list sort) 100 | // R is sorted in place 101 | struct Record* Sort(const uint64_t N; struct Record R[N + 1], const uint64_t N, const uint64_t M, const uint64_t p) 102 | { 103 | 104 | if(N == 0) 105 | return NULL; 106 | 107 | // R1 [Loop on k] P <- LOC(R_N) 108 | struct Record* P = &R[N]; 109 | 110 | // R1 [Loop on k] Perform R2-R6 for k = 1,2,...,p 111 | for(uint64_t k = 1; k <= p; ++k) { 112 | 113 | // R2 [Set piles empty] 114 | 115 | // queue design follows section 2.2.3 linked allocation pp.259-261 116 | // node links point from head of queue to rear of queue 117 | // crucial aspect of empty queue: "the value of variable F must be stored in the LINK field of its location if this idea is to work" 118 | // this means F or front pointer to an empty queue is used as if it were a node with a LINK field, i.e. cast as struct Record* in our case 119 | 120 | // head of queue 121 | struct Record* BOTM[M]; 122 | 123 | // rear of queue where new node enters 124 | struct Record* TOP[M]; 125 | 126 | // R2 [Set piles empty] TOP[i] <- LOC(BOTM[i]), BOTM[i] <- lambda for 0 <= i < M 127 | for(uint64_t i = 0; i < M; ++i) { 128 | 129 | // empty queue needs BOTM[i] treated as if it were a record node 130 | // see note above about empty queue design 131 | TOP[i] = (void*)&BOTM[i]; 132 | BOTM[i] = NULL; 133 | } 134 | 135 | for(;;) { 136 | 137 | // R3 [Extract kth digit of key] i <- kth least significant digit of key 138 | // this assumes base 256 following the mmix implementation, may generalize later 139 | const uint8_t i = (P->KEY >> (8 * (k - 1))) & 0xffu; 140 | 141 | // R4 [Adjust links] LINK(TOP[i] <- P, TOP[i] <- P 142 | // note TOP[i] is BOTM[i] when queue is empty 143 | // so both TOP[i] and BOTM[i] point to the same first record node when it's inserted 144 | TOP[i]->LINK = P; 145 | TOP[i] = P; 146 | 147 | // R5 [Step to next record] P <- LOC(R_(j - 1) if k = 1 and P = LOC(R_j) for j != 1 148 | if(k == 1) { 149 | ptrdiff_t j = P - R; 150 | if(j != 1) { 151 | P = &R[j - 1]; 152 | // R5 [Step to next record] To R3 153 | continue; 154 | } 155 | } 156 | 157 | // R5 [Step to next record] P <- LINK(P) if k > 1 158 | if(k > 1) { 159 | P = P->LINK; 160 | 161 | // R5 [Step to next record] To R3 if P != lambda 162 | if(P != NULL) 163 | continue; 164 | } 165 | 166 | break; 167 | } 168 | 169 | // R6 [Do Algorithm H] Perform Algorithm H 170 | Hook(TOP, BOTM, M); 171 | 172 | // R6 [Do Algorithm H] P <- BOTM[0] 173 | P = BOTM[0]; 174 | 175 | } 176 | 177 | return P; 178 | 179 | } 180 | 181 | int main(int argc, char* argv[]) 182 | { 183 | 184 | (void)argv; 185 | 186 | if(argc > 1) { 187 | usage(); 188 | exit(0); 189 | } 190 | 191 | // M is radix 192 | uint64_t M; 193 | fread(&M, sizeof M, 1, stdin); 194 | 195 | if(M != 256) { 196 | fprintf(stderr, "Invalid input data: radix M must be 256\n"); 197 | usage(); 198 | exit(1); 199 | } 200 | 201 | // p is key length in base M 202 | uint64_t p; 203 | fread(&p, sizeof p, 1, stdin); 204 | 205 | // read 64-bit size of data array as binary data 206 | uint64_t N; 207 | fread(&N, sizeof N, 1, stdin); 208 | 209 | // read array R of records as binary data 210 | struct Record R[N + 1]; 211 | 212 | // fill key field of each record 213 | for(uint64_t i = 1; i <= N; ++i) { 214 | fread(&R[i].KEY, sizeof(R[i].KEY), 1, stdin); 215 | } 216 | 217 | const struct Record* const sorted = Sort(R, N, M, p); 218 | 219 | // write number of values to follow 220 | fwrite(&N, sizeof N, 1, stdout); 221 | 222 | 223 | // traverse linked list to print sorted keys as binary data 224 | for(const struct Record* p = sorted; p != NULL; p = p->LINK) { 225 | fwrite(&p->KEY, sizeof(p->KEY), 1, stdout); 226 | } 227 | 228 | return 0; 229 | } 230 | 231 | 232 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.0.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  c -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.0.txt: -------------------------------------------------------------------------------- 1 | 256 2 | 1 3 | 10 4 | 5, 3, 2, 5, 7, 11, 0, 2, 99, 5 5 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.1.txt: -------------------------------------------------------------------------------- 1 | 256 2 | 2 3 | 16 4 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 5 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.2.le.dat: -------------------------------------------------------------------------------- 1 |  c -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.2.txt: -------------------------------------------------------------------------------- 1 | 256 2 | 1 3 | 17 4 | 5, 3, 2, 5, 7, 11, 0, 2, 99, 5, 0, 2, 2, 2, 3, 3, 4 5 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |     -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.3.txt: -------------------------------------------------------------------------------- 1 | 256 2 | 1 3 | 16 4 | 1, 3, 2, 4, 10, 5, 11, 6, 13, 7, 14, 8, 15, 9, 16, 12 5 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.4.txt: -------------------------------------------------------------------------------- 1 | 256 2 | 1 3 | 5 4 | 5, 1, 4, 2, 8 5 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.5.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.5.txt: -------------------------------------------------------------------------------- 1 | 256 2 | 1 3 | 0 4 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.6.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.6.txt: -------------------------------------------------------------------------------- 1 | 256 2 | 1 3 | 1 4 | 13 5 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.7.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.7.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.8.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/in.8.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/out.0.le.dat: -------------------------------------------------------------------------------- 1 | 2 |  c -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/out.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/out.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/out.2.le.dat: -------------------------------------------------------------------------------- 1 |  c -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/out.3.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/out.4.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/out.5.le.dat: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/out.6.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/out.7.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/out.7.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/out.8.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2.5_sorting_by_distribution/data/algorithm_r_radix_list_sort/out.8.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # sec_5.2_internal_sorting/CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.17) 4 | 5 | get_filename_component(COMPNAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) 6 | project(${COMPNAME}) 7 | 8 | add_executable(algorithm_c_comparison_counting algorithm_c_comparison_counting.c) 9 | add_executable(exercise_5.2.4 exercise_5.2.4.c algorithm_c_comparison_counting.c) 10 | 11 | if(CMAKE_C_COMPILER_ID MATCHES GNU) 12 | 13 | target_compile_definitions(algorithm_c_comparison_counting PRIVATE ALGORITHM_C_COMPARISON_COUNTING_BUILD_MAIN) 14 | target_compile_options(algorithm_c_comparison_counting PRIVATE -g -Wall -Werror -O0 -std=c18) 15 | 16 | target_compile_options(exercise_5.2.4 PRIVATE -g -Wall -Werror -O0 -std=c18) 17 | 18 | elseif(CMAKE_C_COMPILER_ID MATCHES MSVC) 19 | 20 | target_compile_options(algorithm_c_comparison_counting PRIVATE -Wall -WX -Od) 21 | 22 | elseif(CMAKE_C_COMPILER_ID MATCHES Clang) 23 | 24 | target_compile_options(algorithm_c_comparison_counting PRIVATE -g -Wall -Werror -O0 -std=c18) 25 | 26 | endif() 27 | 28 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/algorithm_c_comparison_counting.c: -------------------------------------------------------------------------------- 1 | // algorithm_c_comparison_counting.c 2 | 3 | // Algorithm C (Comparison counting) 4 | // 5.2 Internal sorting 5 | // The Art of Computer Programming, Donald Knuth 6 | 7 | #include "algorithm_c_comparison_counting.h" 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #ifdef ALGORITHM_C_COMPARISON_COUNTING_BUILD_MAIN 16 | static void usage() 17 | { 18 | puts("usage: algorithm_c_comparison_counting out.dat"); 19 | 20 | puts("reads 64-bit values as binary data to sort, outputs 64-bit counts/ranks as binary data"); 21 | 22 | puts("first uint64_t is number of values to sort"); 23 | puts("next that many int64_t is data to sort"); 24 | 25 | puts(""); 26 | puts("binary input data format"); 27 | puts("uint64_t N"); 28 | puts("int64_t[N] data"); 29 | 30 | puts(""); 31 | puts("binary output data format"); 32 | puts("uint64_t N"); 33 | puts("int64_t[N] sorted data"); 34 | 35 | puts(""); 36 | puts("examples:"); 37 | puts("algorithm_c_comparison_counting = 2; --i) { 53 | 54 | // C3 [Loop on j] Perform C4 for j = i-1, ..., 1 55 | for(uint64_t j = i - 1; j >= 1; --j) { 56 | 57 | if(K[i] < K[j]) { 58 | 59 | // C4 [Compare K_i:K_j] COUNT[j] <- COUNT[j] + 1 if K_i < K_j 60 | ++COUNT[j]; 61 | 62 | } else { 63 | 64 | // C4 [Compare K_i:K_j] COUNT[i] <- COUNT[i] + 1 otherwise 65 | ++COUNT[i]; 66 | } 67 | } 68 | } 69 | } 70 | 71 | #ifdef ALGORITHM_C_COMPARISON_COUNTING_BUILD_MAIN 72 | 73 | int main(int argc, char* argv[]) 74 | { 75 | if(argc > 1) { 76 | usage(); 77 | exit(0); 78 | } 79 | 80 | // read 64-bit size of data array as binary data 81 | uint64_t N; 82 | fread(&N, sizeof N, 1, stdin); 83 | 84 | // read array K as binary data 85 | // allocate N+1 entries to use array indexing from 1 instead of 0 86 | int64_t K[N + 1]; 87 | fread(&K[1], sizeof(*K), N, stdin); 88 | 89 | // allocate COUNT array, extra entry again for 1-indexing 90 | uint64_t COUNT[N + 1]; 91 | 92 | // fill COUNT array 93 | Sort(K, COUNT, N); 94 | 95 | // write number of values to follow 96 | fwrite(&N, sizeof N, 1, stdout); 97 | 98 | // print COUNT array as binary data 99 | fwrite(&COUNT[1], sizeof(*COUNT), N, stdout); 100 | 101 | return 0; 102 | } 103 | 104 | #endif 105 | 106 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/algorithm_c_comparison_counting.h: -------------------------------------------------------------------------------- 1 | #ifndef ALGORITHM_C_COMPARISON_COUNTING_H 2 | #define ALGORITHM_C_COMPARISON_COUNTING_H 3 | 4 | // Algorithm C (Comparison counting) 5 | // 5.2 Internal sorting 6 | // The Art of Computer Programming, Donald Knuth 7 | 8 | #include 9 | 10 | void Sort(const int64_t K[], uint64_t COUNT[], const uint64_t N); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/algorithm_c_comparison_counting/in.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/algorithm_c_comparison_counting/in.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/algorithm_c_comparison_counting/in.0.txt: -------------------------------------------------------------------------------- 1 | 4 2 | 5, -2, -3, 6 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/algorithm_c_comparison_counting/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/algorithm_c_comparison_counting/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/algorithm_c_comparison_counting/in.1.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/algorithm_c_comparison_counting/out.0.le.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/algorithm_c_comparison_counting/out.1.le.dat: -------------------------------------------------------------------------------- 1 |  2 |  -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/exercise_5.2.4/in.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/exercise_5.2.4/in.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/exercise_5.2.4/in.0.txt: -------------------------------------------------------------------------------- 1 | 4 2 | 5, -2, -3, 6 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/exercise_5.2.4/in.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/exercise_5.2.4/in.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/exercise_5.2.4/in.1.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 3 | -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/exercise_5.2.4/out.0.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/exercise_5.2.4/out.0.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/exercise_5.2.4/out.1.le.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theartofcomputerprogramming/programs/7a264f2107c59afe99e6fc0357bcad62c4094b38/vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/data/exercise_5.2.4/out.1.le.dat -------------------------------------------------------------------------------- /vol_3_sorting_and_searching_chap_5_sorting/sec_5.2_internal_sorting/exercise_5.2.4.c: -------------------------------------------------------------------------------- 1 | // exercise_5.2.4.c 2 | 3 | // Exercise 5.2.4 4 | // 5.2 Internal sorting 5 | // The Art of Computer Programming, Donald Knuth 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "algorithm_c_comparison_counting.h" 14 | 15 | static void usage() 16 | { 17 | puts("usage: exercise_5.2.4 out.dat"); 18 | 19 | puts("reads 64-bit values as binary data to sort, outputs sorted 64-bit values as binary data"); 20 | 21 | puts("first uint64_t is number of values to sort"); 22 | puts("next that many int64_t is data to sort"); 23 | 24 | puts(""); 25 | puts("binary input data format"); 26 | puts("uint64_t N"); 27 | puts("int64_t[N] data"); 28 | 29 | puts(""); 30 | puts("binary output data format"); 31 | puts("uint64_t N"); 32 | puts("int64_t[N] sorted data"); 33 | 34 | puts(""); 35 | puts("examples:"); 36 | puts("exercise_5.2.4 1) { 54 | usage(); 55 | exit(0); 56 | } 57 | 58 | // read 64-bit size of data array as binary data 59 | uint64_t N; 60 | fread(&N, sizeof N, 1, stdin); 61 | 62 | // read array K as binary data 63 | // allocate N+1 entries to use array indexing from 1 instead of 0 64 | int64_t K[N + 1]; 65 | fread(&K[1], sizeof(*K), N, stdin); 66 | 67 | // allocate COUNT array, extra entry again for 1-indexing 68 | uint64_t COUNT[N + 1]; 69 | 70 | // fill COUNT array 71 | Sort(K, COUNT, N); 72 | 73 | // use COUNT array to fill sorted array OUTPUT 74 | int64_t OUTPUT[N + 1]; 75 | Finish(K, COUNT, N, OUTPUT); 76 | 77 | // write number of values to follow 78 | fwrite(&N, sizeof N, 1, stdout); 79 | 80 | // print OUTPUT array as binary data 81 | fwrite(&OUTPUT[1], sizeof(*OUTPUT), N, stdout); 82 | 83 | return 0; 84 | } 85 | 86 | --------------------------------------------------------------------------------