From f7b209a8448d11f18b6a1122b818dc37569e7ea0 Mon Sep 17 00:00:00 2001 From: Yuri Tatishchev Date: Mon, 24 Feb 2025 12:09:51 -0800 Subject: [PATCH] Assignment 5: heap sort init --- 05-labHeapSort/.idea/.gitignore | 8 + .../inspectionProfiles/Project_Default.xml | 6 + 05-labHeapSort/.idea/misc.xml | 6 + 05-labHeapSort/.idea/modules.xml | 8 + 05-labHeapSort/.idea/vcs.xml | 6 + 05-labHeapSort/05-labHeapSort.iml | 11 + 05-labHeapSort/Assignment.java | 42 +++ 05-labHeapSort/TestAll.java | 103 ++++++ 05-labHeapSort/inputs/test01.txt | 7 + 05-labHeapSort/inputs/test02.txt | 151 +++++++++ 05-labHeapSort/inputs/test03.txt | 201 ++++++++++++ 05-labHeapSort/inputs/test04.txt | 251 +++++++++++++++ 05-labHeapSort/inputs/test05.txt | 301 ++++++++++++++++++ 05-labHeapSort/outputs/test01.txt | 1 + 05-labHeapSort/outputs/test02.txt | 1 + 05-labHeapSort/outputs/test03.txt | 1 + 05-labHeapSort/outputs/test04.txt | 1 + 05-labHeapSort/outputs/test05.txt | 1 + 18 files changed, 1106 insertions(+) create mode 100644 05-labHeapSort/.idea/.gitignore create mode 100644 05-labHeapSort/.idea/inspectionProfiles/Project_Default.xml create mode 100644 05-labHeapSort/.idea/misc.xml create mode 100644 05-labHeapSort/.idea/modules.xml create mode 100644 05-labHeapSort/.idea/vcs.xml create mode 100644 05-labHeapSort/05-labHeapSort.iml create mode 100644 05-labHeapSort/Assignment.java create mode 100644 05-labHeapSort/TestAll.java create mode 100644 05-labHeapSort/inputs/test01.txt create mode 100644 05-labHeapSort/inputs/test02.txt create mode 100644 05-labHeapSort/inputs/test03.txt create mode 100644 05-labHeapSort/inputs/test04.txt create mode 100644 05-labHeapSort/inputs/test05.txt create mode 100644 05-labHeapSort/outputs/test01.txt create mode 100644 05-labHeapSort/outputs/test02.txt create mode 100644 05-labHeapSort/outputs/test03.txt create mode 100644 05-labHeapSort/outputs/test04.txt create mode 100644 05-labHeapSort/outputs/test05.txt diff --git a/05-labHeapSort/.idea/.gitignore b/05-labHeapSort/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/05-labHeapSort/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/05-labHeapSort/.idea/inspectionProfiles/Project_Default.xml b/05-labHeapSort/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..03d9549 --- /dev/null +++ b/05-labHeapSort/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/05-labHeapSort/.idea/misc.xml b/05-labHeapSort/.idea/misc.xml new file mode 100644 index 0000000..6da441e --- /dev/null +++ b/05-labHeapSort/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/05-labHeapSort/.idea/modules.xml b/05-labHeapSort/.idea/modules.xml new file mode 100644 index 0000000..cd17fdb --- /dev/null +++ b/05-labHeapSort/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/05-labHeapSort/.idea/vcs.xml b/05-labHeapSort/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/05-labHeapSort/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/05-labHeapSort/05-labHeapSort.iml b/05-labHeapSort/05-labHeapSort.iml new file mode 100644 index 0000000..5c0ce42 --- /dev/null +++ b/05-labHeapSort/05-labHeapSort.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/05-labHeapSort/Assignment.java b/05-labHeapSort/Assignment.java new file mode 100644 index 0000000..13af5ec --- /dev/null +++ b/05-labHeapSort/Assignment.java @@ -0,0 +1,42 @@ +package labHeapSort; + +import java.io.*; +import java.util.*; + +public class Assignment { + + public static int[] heapify(int[] heap, int sizeA, int index) { + // Fill in here + return heap; + } + + public static int[] buildHeap(int[] arr, int sizeA) { + // Fill in here + return heapify(arr, sizeA, 1); + } + + public static int[] heapSort(int[] arr, int sizeA) { + // Fill in here + return buildHeap(arr, sizeA); + } + + public static void run(String inputPath) { + try (BufferedReader br = new BufferedReader(new FileReader(inputPath))) { + int size = Integer.parseInt(br.readLine()); + int[] A = new int[size]; + + for (int i = 0; i < size; i++) { + A[i] = Integer.parseInt(br.readLine()); + } + + int[] heap = heapSort(A, size); + + for (int i = 0; i < size; i++) { + System.out.print(heap[i] + ";"); + } + System.out.println(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/05-labHeapSort/TestAll.java b/05-labHeapSort/TestAll.java new file mode 100644 index 0000000..dd3a97a --- /dev/null +++ b/05-labHeapSort/TestAll.java @@ -0,0 +1,103 @@ +package labHeapSort; + +import java.io.File; +import java.net.URL; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.PrintStream; +import java.io.ByteArrayOutputStream; + +public class TestAll { + public static void main(String[] args) { + processInputFiles(); + } + + public static void processInputFiles() { + try { + // Get the inputs and outputs directories + URL input_url = TestAll.class.getResource("inputs"); + URL output_url = TestAll.class.getResource("outputs"); + if (input_url == null) { + System.out.println("Cannot find folder \"inputs\""); + return; + } + if (output_url == null) { + System.out.println("Cannot find folder \"outputs\""); + return; + } + + // Get the names of all the files in either directory + File input_folder = new File(input_url.getPath()); + File[] input_files = input_folder.listFiles(); + File output_folder = new File(output_url.getPath()); + if (input_files == null) { + System.out.printf("No files in folder: %s\n", input_folder.getPath()); + return; + } + // No need to check if output files exist, can still run inputs + + // Capture the print statements from the assignment into printstream + ByteArrayOutputStream captured_bytestream = new ByteArrayOutputStream(); + PrintStream captured_printstream = new PrintStream(captured_bytestream); + // Replace System.out with a local variable to capture prints + PrintStream stdout = System.out; + System.setOut(captured_printstream); + Integer total_tests = 0, passed_tests = 0; + + for (File input_file : input_files) { + if (!input_file.isFile()) { + continue; + } + // Print which test is currently being run + stdout.println("Processing file: " + input_file.getName()); + // Run the assignment on this input file + Assignment.run(input_file.getAbsolutePath()); + // Ensure all the output from assignment goes to local variable + System.out.flush(); + // Get the output from assignment into a String + String assignment_output = captured_bytestream.toString(); + captured_bytestream.reset(); + // Get the file contents for comparison + File output_file = new File(output_folder, input_file.getName()); + // Check if a corresponding output file exists + if (!output_file.exists()) { + // Print the output to the console without grading + stdout.println("No corresponding output, result shown:"); + stdout.println(assignment_output); + continue; + } + // Read the contents of the corresponding output file + total_tests++; + BufferedReader buffered_reader = new BufferedReader(new FileReader(output_file.getAbsolutePath())); + StringBuilder string_builder = new StringBuilder(); + String line; + while ((line = buffered_reader.readLine()) != null) { + string_builder.append(line).append(System.lineSeparator()); + } + buffered_reader.close(); + + String correct_output = string_builder.toString(); + if (assignment_output.equals(correct_output)) { + // This test passes + passed_tests++; + stdout.println("Test Pass"); + } else { + // This test does not pass + stdout.println("Test Fail:"); + stdout.println("Expected:"); + stdout.println(correct_output); + stdout.println("Actual:"); + stdout.println(assignment_output); + } + + // Indicate this test is done + stdout.println("====="); + } + stdout.printf("Result: %d/%d Tests Passed\n", passed_tests, total_tests); + System.setOut(stdout); + + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/05-labHeapSort/inputs/test01.txt b/05-labHeapSort/inputs/test01.txt new file mode 100644 index 0000000..62d77ba --- /dev/null +++ b/05-labHeapSort/inputs/test01.txt @@ -0,0 +1,7 @@ +6 +5 +3 +2 +1 +6 +4 diff --git a/05-labHeapSort/inputs/test02.txt b/05-labHeapSort/inputs/test02.txt new file mode 100644 index 0000000..d0938ea --- /dev/null +++ b/05-labHeapSort/inputs/test02.txt @@ -0,0 +1,151 @@ +150 +8 +50 +24 +9 +131 +123 +145 +129 +24 +110 +91 +66 +43 +93 +88 +54 +128 +80 +41 +113 +4 +20 +10 +108 +11 +34 +100 +79 +117 +86 +98 +27 +63 +68 +111 +34 +80 +50 +130 +22 +68 +123 +44 +37 +36 +96 +129 +92 +145 +8 +102 +54 +59 +95 +119 +141 +25 +147 +31 +104 +23 +17 +100 +75 +102 +104 +28 +109 +79 +134 +149 +32 +86 +14 +16 +65 +114 +37 +126 +120 +16 +95 +130 +2 +18 +96 +106 +105 +52 +49 +139 +24 +6 +83 +103 +117 +17 +88 +39 +145 +52 +148 +72 +129 +138 +9 +128 +148 +145 +55 +60 +132 +96 +76 +136 +99 +143 +50 +69 +63 +61 +8 +145 +59 +146 +19 +114 +131 +107 +13 +143 +116 +83 +53 +68 +22 +46 +113 +22 +102 +141 +82 +39 +58 +17 +141 +141 +80 +36 +107 diff --git a/05-labHeapSort/inputs/test03.txt b/05-labHeapSort/inputs/test03.txt new file mode 100644 index 0000000..fd65859 --- /dev/null +++ b/05-labHeapSort/inputs/test03.txt @@ -0,0 +1,201 @@ +200 +8 +50 +74 +59 +131 +73 +145 +79 +124 +110 +41 +166 +93 +43 +188 +104 +128 +130 +41 +13 +104 +170 +110 +158 +161 +134 +100 +79 +17 +136 +98 +27 +113 +68 +11 +34 +180 +150 +180 +22 +168 +73 +194 +137 +86 +146 +29 +92 +195 +158 +2 +154 +109 +145 +69 +91 +125 +197 +131 +104 +123 +67 +150 +25 +2 +54 +178 +9 +29 +134 +99 +182 +36 +14 +66 +15 +64 +137 +26 +70 +116 +95 +30 +102 +118 +196 +106 +5 +52 +99 +189 +124 +106 +83 +153 +167 +117 +138 +39 +145 +102 +98 +72 +29 +138 +159 +178 +198 +95 +105 +10 +32 +146 +176 +36 +99 +143 +200 +169 +13 +61 +158 +195 +9 +196 +169 +114 +131 +7 +163 +143 +166 +83 +53 +168 +22 +96 +113 +72 +102 +191 +32 +139 +58 +17 +191 +41 +80 +136 +7 +173 +99 +96 +120 +55 +24 +90 +161 +106 +127 +124 +107 +14 +171 +39 +95 +21 +45 +167 +135 +27 +195 +164 +139 +45 +191 +151 +160 +24 +148 +186 +18 +73 +140 +48 +186 +97 +86 +24 +21 +145 +169 +136 +116 +26 +135 +43 +12 +80 +153 diff --git a/05-labHeapSort/inputs/test04.txt b/05-labHeapSort/inputs/test04.txt new file mode 100644 index 0000000..b3fd0b5 --- /dev/null +++ b/05-labHeapSort/inputs/test04.txt @@ -0,0 +1,251 @@ +250 +58 +250 +74 +159 +181 +23 +45 +129 +174 +210 +191 +166 +243 +43 +238 +4 +78 +230 +91 +113 +54 +170 +210 +158 +61 +184 +100 +29 +67 +86 +98 +77 +13 +18 +61 +134 +230 +150 +80 +72 +218 +173 +144 +87 +236 +246 +229 +92 +195 +108 +2 +154 +209 +195 +169 +241 +125 +197 +31 +154 +223 +167 +50 +25 +52 +104 +228 +159 +229 +184 +49 +232 +136 +14 +116 +65 +64 +37 +176 +170 +116 +95 +130 +2 +18 +196 +106 +155 +202 +49 +189 +124 +6 +133 +3 +67 +217 +88 +189 +145 +2 +148 +122 +79 +138 +109 +178 +148 +45 +155 +110 +232 +246 +176 +136 +49 +143 +150 +219 +163 +11 +58 +95 +9 +146 +219 +114 +31 +7 +213 +193 +116 +83 +103 +18 +72 +196 +213 +172 +152 +91 +82 +239 +58 +117 +41 +141 +180 +86 +7 +223 +99 +96 +70 +205 +224 +40 +11 +156 +127 +124 +7 +64 +21 +239 +95 +221 +95 +117 +35 +227 +145 +114 +239 +95 +91 +51 +160 +124 +198 +136 +218 +23 +40 +248 +136 +247 +136 +124 +171 +145 +219 +236 +166 +126 +35 +43 +12 +180 +153 +245 +46 +69 +147 +193 +66 +242 +34 +170 +148 +54 +48 +76 +211 +163 +212 +59 +128 +12 +126 +86 +119 +246 +27 +18 +190 +125 +82 +57 +52 +123 +61 +95 +235 +6 +40 +158 +16 +194 +20 +181 +206 +106 +7 +214 +3 +177 +109 +250 +82 diff --git a/05-labHeapSort/inputs/test05.txt b/05-labHeapSort/inputs/test05.txt new file mode 100644 index 0000000..c35c153 --- /dev/null +++ b/05-labHeapSort/inputs/test05.txt @@ -0,0 +1,301 @@ +300 +8 +50 +174 +159 +131 +273 +145 +279 +24 +110 +241 +66 +193 +243 +88 +204 +128 +230 +41 +113 +4 +170 +10 +258 +161 +34 +100 +79 +117 +236 +98 +27 +213 +68 +111 +34 +80 +50 +280 +22 +68 +273 +194 +37 +186 +246 +129 +92 +295 +158 +102 +54 +209 +245 +269 +291 +25 +297 +31 +104 +23 +167 +250 +225 +102 +254 +178 +109 +229 +134 +299 +182 +236 +14 +166 +215 +264 +37 +126 +270 +16 +95 +130 +2 +18 +96 +106 +105 +52 +199 +289 +24 +6 +83 +253 +267 +17 +238 +39 +145 +202 +298 +72 +129 +138 +159 +278 +298 +295 +205 +210 +132 +246 +76 +136 +99 +143 +200 +69 +213 +61 +158 +295 +209 +296 +169 +114 +131 +107 +163 +143 +266 +83 +53 +68 +22 +196 +113 +172 +102 +291 +232 +39 +58 +17 +291 +141 +80 +36 +107 +273 +299 +196 +220 +55 +24 +290 +261 +206 +127 +124 +207 +114 +171 +139 +95 +121 +45 +167 +35 +27 +295 +164 +139 +245 +191 +51 +160 +224 +148 +86 +218 +273 +40 +48 +286 +197 +86 +224 +221 +45 +69 +236 +16 +226 +135 +143 +12 +280 +253 +145 +296 +219 +297 +193 +16 +92 +234 +270 +98 +154 +248 +126 +111 +63 +112 +109 +278 +62 +226 +236 +69 +96 +277 +268 +140 +275 +232 +157 +2 +173 +261 +195 +185 +156 +190 +8 +16 +194 +270 +81 +56 +256 +7 +64 +203 +177 +209 +250 +232 +145 +39 +109 +198 +52 +250 +4 +132 +232 +215 +120 +176 +210 +181 +230 +224 +155 +161 +238 +46 +118 +226 +201 +57 +165 +298 +49 +205 +51 +151 +77 +213 +155 +298 +105 +282 +149 +166 +279 +300 +10 +230 +154 +284 +48 +208 +174 +123 +106 +277 diff --git a/05-labHeapSort/outputs/test01.txt b/05-labHeapSort/outputs/test01.txt new file mode 100644 index 0000000..3d7ef7c --- /dev/null +++ b/05-labHeapSort/outputs/test01.txt @@ -0,0 +1 @@ +1;2;3;4;5;6; \ No newline at end of file diff --git a/05-labHeapSort/outputs/test02.txt b/05-labHeapSort/outputs/test02.txt new file mode 100644 index 0000000..51ed593 --- /dev/null +++ b/05-labHeapSort/outputs/test02.txt @@ -0,0 +1 @@ +2;4;6;8;8;8;9;9;10;11;13;14;16;16;17;17;17;18;19;20;22;22;22;23;24;24;24;25;27;28;31;32;34;34;36;36;37;37;39;39;41;43;44;46;49;50;50;50;52;52;53;54;54;55;58;59;59;60;61;63;63;65;66;68;68;68;69;72;75;76;79;79;80;80;80;82;83;83;86;86;88;88;91;92;93;95;95;96;96;96;98;99;100;100;102;102;102;103;104;104;105;106;107;107;108;109;110;111;113;113;114;114;116;117;117;119;120;123;123;126;128;128;129;129;129;130;130;131;131;132;134;136;138;139;141;141;141;141;143;143;145;145;145;145;145;146;147;148;148;149; \ No newline at end of file diff --git a/05-labHeapSort/outputs/test03.txt b/05-labHeapSort/outputs/test03.txt new file mode 100644 index 0000000..d596725 --- /dev/null +++ b/05-labHeapSort/outputs/test03.txt @@ -0,0 +1 @@ +2;2;5;7;7;8;9;9;10;11;12;13;13;14;14;15;17;17;18;21;21;22;22;24;24;24;25;26;26;27;27;29;29;29;30;32;32;34;36;36;39;39;41;41;41;43;43;45;45;48;50;52;53;54;55;58;59;61;64;66;67;68;69;70;72;72;73;73;73;74;79;79;80;80;83;83;86;86;90;91;92;93;95;95;95;96;96;97;98;98;99;99;99;99;100;102;102;102;104;104;104;105;106;106;106;107;109;110;110;113;113;114;116;116;117;118;120;123;124;124;124;125;127;128;130;131;131;131;134;134;135;135;136;136;136;137;137;138;138;139;139;140;143;143;145;145;145;145;146;146;148;150;150;151;153;153;154;158;158;158;159;160;161;161;163;164;166;166;167;167;168;168;169;169;169;170;171;173;176;178;178;180;180;182;186;186;188;189;191;191;191;194;195;195;195;196;196;197;198;200; \ No newline at end of file diff --git a/05-labHeapSort/outputs/test04.txt b/05-labHeapSort/outputs/test04.txt new file mode 100644 index 0000000..40d02dd --- /dev/null +++ b/05-labHeapSort/outputs/test04.txt @@ -0,0 +1 @@ +2;2;2;3;3;4;6;6;7;7;7;7;9;11;11;12;12;13;14;16;18;18;18;18;20;21;23;23;25;27;29;31;31;34;35;35;37;40;40;40;41;43;43;45;45;46;48;49;49;49;50;51;52;52;54;54;57;58;58;58;59;61;61;61;64;64;65;66;67;67;69;70;72;72;74;76;77;78;79;80;82;82;82;83;86;86;86;87;88;91;91;91;92;95;95;95;95;95;95;96;98;99;100;103;104;106;106;108;109;109;110;113;114;114;116;116;116;117;117;119;122;123;124;124;124;124;125;125;126;126;127;128;129;130;133;134;136;136;136;136;136;138;141;143;144;145;145;145;146;147;148;148;148;150;150;152;153;154;154;155;155;156;158;158;159;159;160;163;163;166;166;167;169;170;170;170;171;172;173;174;176;176;177;178;180;180;181;181;184;184;189;189;190;191;193;193;194;195;195;196;196;197;198;202;205;206;209;210;210;211;212;213;213;214;217;218;218;219;219;219;221;223;223;224;227;228;229;229;230;230;232;232;235;236;236;238;239;239;239;241;242;243;245;246;246;246;247;248;250;250; \ No newline at end of file diff --git a/05-labHeapSort/outputs/test05.txt b/05-labHeapSort/outputs/test05.txt new file mode 100644 index 0000000..9625854 --- /dev/null +++ b/05-labHeapSort/outputs/test05.txt @@ -0,0 +1 @@ +2;2;4;4;6;7;8;8;10;10;12;14;16;16;16;16;17;17;18;22;22;23;24;24;24;25;27;27;31;34;34;35;36;37;37;39;39;39;40;41;45;45;46;48;48;49;50;50;51;51;52;52;53;54;55;56;57;58;61;62;63;64;66;68;68;68;69;69;69;72;76;77;79;80;80;81;83;83;86;86;88;92;92;95;95;96;96;98;98;99;100;102;102;102;104;105;105;106;106;107;107;109;109;109;110;111;111;112;113;113;114;114;117;118;120;121;123;124;126;126;127;128;129;129;130;131;131;132;132;134;135;136;138;139;139;140;141;143;143;143;145;145;145;145;148;149;151;154;154;155;155;156;157;158;158;159;159;160;161;161;163;164;165;166;166;167;167;169;170;171;172;173;174;174;176;177;178;181;182;185;186;190;191;193;193;194;194;195;196;196;197;198;199;200;201;202;203;204;205;205;206;207;208;209;209;209;210;210;213;213;213;215;215;218;219;220;221;224;224;224;225;226;226;226;229;230;230;230;232;232;232;232;234;236;236;236;236;238;238;241;243;245;245;246;246;248;250;250;250;253;253;254;256;258;261;261;264;266;267;268;269;270;270;270;273;273;273;273;275;277;277;278;278;279;279;280;280;282;284;286;289;290;291;291;291;295;295;295;295;296;296;297;297;298;298;298;298;299;299;300; \ No newline at end of file