Skip to Content
CoursesCSC102การหาค่ามัธยฐานจากอาร์เรย์ลิสต์

การหาค่ามัธยฐานจากอาร์เรย์ลิสต์

โจทย์

Given two sorted arrays, nums1 and nums2, with sizes m and n respectively, write a Java program to find the median of the two sorted arrays. The median is defined as the middle value in a sorted list of numbers. If the total number of elements is odd, the median is the middle element. If the total number of elements is even, the median is the average of the two middle elements. Your program should include the size and elements of each array, then output the median of the combined sorted list.

Note: To solve this problem, use an “ArrayList” to merge the elements of the two arrays, sort the merged list, and then determine the median. For more information about the ArrayList: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html 

For Example:

For the input arrays nums1 = [1, 3, 5] and nums2 = [2, 4, 6, 8], the merged and sorted array would be [1, 2, 3, 4, 5, 6, 8], and the median would be 4.04.0.

InputOutput
3
1 3 5
4
2 4 6 8
The median of the two sorted arrays is: 4.0
2
1 2
2
3 4
The median of the two sorted arrays is: 2.5

โค้ด

import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<Double> nums1 = new ArrayList<>(); int m = scanner.nextInt(); for (int i = 0; i < m; i++) { nums1.add(scanner.nextDouble()); } ArrayList<Double> nums2 = new ArrayList<>(); int n = scanner.nextInt(); for (int i = 0; i < n; i++) { nums2.add(scanner.nextDouble()); } nums1.addAll(nums2); Collections.sort(nums1); double median = nums1.size() % 2 == 1 ? nums1.get(nums1.size() / 2) : (nums1.get(nums1.size() / 2 - 1) + nums1.get(nums1.size() / 2)) / 2; System.out.printf("The median of the two sorted arrays is: %.1f", median); } }

คำอธิบาย

การนำเข้าไลบรารี่ที่จำเป็น

import java.util.ArrayList; // สำหรับใช้งาน ArrayList import java.util.Collections; // สำหรับการเรียงลำดับข้อมูล import java.util.Scanner; // สำหรับรับข้อมูลจากผู้ใช้

1. การประกาศตัวแปรและรับข้อมูล

Scanner scanner = new Scanner(System.in); ArrayList<Double> nums1 = new ArrayList<>(); // สร้าง ArrayList สำหรับชุดข้อมูลแรก int m = scanner.nextInt(); // รับขนาดของชุดข้อมูลแรก

2. การรับข้อมูลชุดแรก

for (int i = 0; i < m; i++) { nums1.add(scanner.nextDouble()); }
  • ลูปนี้จะทำงาน m ครั้งเพื่อรับตัวเลขและเก็บใน nums1
  • ใช้ scanner.nextDouble() เพื่อรับค่าทศนิยม

3. การรับข้อมูลชุดที่สอง

ArrayList<Double> nums2 = new ArrayList<>(); // สร้าง ArrayList สำหรับชุดข้อมูลที่สอง int n = scanner.nextInt(); // รับขนาดของชุดข้อมูลที่สอง for (int i = 0; i < n; i++) { nums2.add(scanner.nextDouble()); }

4. การรวมและเรียงลำดับข้อมูล

nums1.addAll(nums2); // รวมข้อมูลจาก nums2 เข้าไปใน nums1 Collections.sort(nums1); // เรียงลำดับข้อมูลจากน้อยไปมาก

5. การคำนวณค่ามัธยฐาน

double median = nums1.size() % 2 == 1 ? nums1.get(nums1.size() / 2) : (nums1.get(nums1.size() / 2 - 1) + nums1.get(nums1.size() / 2)) / 2;
  • ถ้าจำนวนข้อมูลเป็นเลขคี่: ใช้ตัวกลางพอดี
  • ถ้าจำนวนข้อมูลเป็นเลขคู่: หาค่าเฉลี่ยของตัวกลางสองตัว

แผนภาพการทำงาน

ตัวอย่างการทำงาน

ตัวอย่างที่ 1: จำนวนข้อมูลรวมเป็นเลขคี่

Input: 3 // ขนาดของ nums1 1 3 5 // ข้อมูลใน nums1 4 // ขนาดของ nums2 2 4 6 8 // ข้อมูลใน nums2 การทำงาน: 1. รวมข้อมูล: [1, 3, 5, 2, 4, 6, 8] 2. เรียงลำดับ: [1, 2, 3, 4, 5, 6, 8] 3. จำนวนข้อมูลรวม = 7 (เลขคี่) 4. ค่ามัธยฐาน = ตำแหน่งที่ 4 = 4.0 Output: The median of the two sorted arrays is: 4.0

ตัวอย่างที่ 2: จำนวนข้อมูลรวมเป็นเลขคู่

Input: 2 // ขนาดของ nums1 1 2 // ข้อมูลใน nums1 2 // ขนาดของ nums2 3 4 // ข้อมูลใน nums2 การทำงาน: 1. รวมข้อมูล: [1, 2, 3, 4] 2. เรียงลำดับ: [1, 2, 3, 4] 3. จำนวนข้อมูลรวม = 4 (เลขคู่) 4. ค่ามัธยฐาน = (2 + 3) / 2 = 2.5 Output: The median of the two sorted arrays is: 2.5
ปรับปรุงล่าสุด