{"id":29913625,"url":"https://github.com/akkaraponph/report-external-sorted","last_synced_at":"2026-02-08T23:03:37.785Z","repository":{"id":246392960,"uuid":"415848767","full_name":"akkaraponph/report-external-sorted","owner":"akkaraponph","description":"Algorithm and Computer Architecture","archived":false,"fork":false,"pushed_at":"2021-10-31T04:44:35.000Z","size":7574,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"Algorithm","last_synced_at":"2026-01-26T02:32:57.047Z","etag":null,"topics":["algorithm","computer-architecture","external-sorting","flash-memory","report"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/akkaraponph.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-11T08:53:37.000Z","updated_at":"2024-06-27T14:58:22.000Z","dependencies_parsed_at":"2024-06-27T19:24:26.540Z","dependency_job_id":"4d69637a-69a1-4464-8d21-ac2217832068","html_url":"https://github.com/akkaraponph/report-external-sorted","commit_stats":null,"previous_names":["billowdev/report-external-sorted","akkaraponph/report-external-sorted"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/akkaraponph/report-external-sorted","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akkaraponph%2Freport-external-sorted","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akkaraponph%2Freport-external-sorted/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akkaraponph%2Freport-external-sorted/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akkaraponph%2Freport-external-sorted/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/akkaraponph","download_url":"https://codeload.github.com/akkaraponph/report-external-sorted/tar.gz/refs/heads/Algorithm","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akkaraponph%2Freport-external-sorted/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29248507,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-08T22:49:53.206Z","status":"ssl_error","status_checked_at":"2026-02-08T22:49:51.384Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","computer-architecture","external-sorting","flash-memory","report"],"created_at":"2025-08-02T02:13:08.459Z","updated_at":"2026-02-08T23:03:37.767Z","avatar_url":"https://github.com/akkaraponph.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Report-External-Sorted\n\n\n# External Sort\n\u003cp\u003e\n\tการเรียงลำดับภายนอก คือ อัลกอรึธึมการเรียงลำดับที่สามารถจัดการข้อมูล จำนวนมหาศาลได้ เมื่อข้อมูลจัดเรียงไม่พอดี (fit) กับหน่วยความจำหลักของคอมพิวเตอร์ (RAM) และจะต้องอยู่ในหน่วยความจำภายนอกที่ช้ากว่า (Hard drive) แทน\n\tการเรียงลำดับภายนอกมักใช้อัลกอริทึมแบบไฮบริดในการเรียงลำดับส่วนของข้อมูล ที่มีขนาดเล็ก ข้อมูลจะถูกใส่ลงในหน่วยความจำหลัก จะถูกอ่าน จัดเรียง และเขียนลงในไฟล์ชั่วคราว \nซึ่งในขั้นตอนการรวมไฟล์ย่อยที่เรียงลำดับจะถูกรวมเป็นไฟล์ใหญ่ไฟล์เดียว\nNatural merge sort\n\u003c/p\u003e\n\n## การเรียงลำดับแบบผสาน (Merge sort)\n\n\u003cp\u003e\n“แบ่งรายการที่ไม่เรียงลำดับออกเป็น n รายการย่อย โดยแต่ละรายการมีองค์ประกอบหนึ่งรายการ ซึ่งรายการขององค์ประกอบหนึ่งรายการจะถือเป็นการจัดเรียง รวมรายการย่อยซ้ำๆ เพื่อสร้างรายการย่อยที่เรียงลำดับใหม่จนกว่าจะมีรายการย่อยเหลือเพียงรายการเดียว”\nกล่าวคือ รายการเรียงลำดับ merge sort จะเป็นลักษณะ การตัด array ออกเป็น 2 ส่วนในแต่ละส่วนก็จะเอาไป recursion ตัดออกเป็นชิ้นย่อย ๆ ลงไปอีกจนเหลือขนาดเล็ก ที่สามารถ sort ได้ ก็จะจัดการ sort ชิ้นเล็กๆ ให้เสร็จแล้วค่อยนำชิ้นเล็กๆ ที่ sort เสร็จมาต่อกันอีกที\nการที่จะนำมาต่อกัน เราจะใช้การวนซ้ำ ซึ่งจะใช้วิธีใดก็ได้ ในที่นี้เราจะใช้ while loop ทำการวนซ้ำ จนกว่าข้อมูล จะหมด โดยแต่ละรอบให้ทำการเช็คว่า ตำแหน่งแรกของ array ย่อย 2 ตัวอันไหนที่น้อยกว่า ให้ใส่ใน array ใหญ่\n\u003c/p\u003e\n\n\u003cimg src=\"image/natural-merge.png\" /\u003e\n\n\u003cp\u003e\nจะเห็นได้ว่า merge sort จะเป็นอัลกอริทึมในลักษณะ recursion และ ใช้หลักการแบ่ง (divide \u0026 conquer) ซึ่ง Divide and Conquer เป็นวิธีการแก้ปัญหาแบบเรียกซ้ำ ซึ่งแบ่งปัญหาออกเป็นปัญหาย่อยที่มีขนาดเล็กลง แก้ปัญหาย่อยแบบเรียกซ้ำ และสุดท้ายรวมการแก้ปัญหาย่อยเพื่อแก้ปัญหาเดิม วิธีนี้มักจะช่วยให้เราลดความซับซ้อนของเวลาได้มาก\n\u003c/p\u003e\n\n\n```\n1.Algorithm merge sort (low, height)\n2.\tif (low \u003cpre heigh):\n3.\t\tmid = (low + height)/2 \n4.\t\tmerge sort(low, mid)\n5.\t\tmerge sort(mid + 1, height)\n6.\t\tmerge (low, mid, height)\n7.end\n\n```\n\n### ภาพเพิ่มเติมสำหรับ Merge sort\nกระบวนการแยก\n\u003cimg src=\"image/divide.png\" /\u003e\n- ref: https://www.happycoders.eu/wp-content/uploads/2020/07/mergesort_algorithm_divide-v2-800x447.png\n\nกระบวนการ merge\n \u003cimg src=\"image/merge-process.png\" /\u003e\n\n- ref: https://www.happycoders.eu/wp-content/uploads/2020/07/mergesort_algorithm_merge-v2-800x416.png\n\nภาพรวม\n\n \u003cimg src=\"image/overview-merge.png\" /\u003e\n\n- ref: https://th.wikipedia.org/wiki/%E0%B9%84%E0%B8%9F%E0%B8%A5%E0%B9%8C:Merge_sort_algorithm_diagram.svg\n\n\n### การ Merge ลิสต์ที่เรียงลำดับแล้วสองลิสต์ รวมกันเป็น ลิสต์เรียงลำดับ ลิสต์ดียว \n(two sorted list merge to single sorted list)\n\n\n \u003cimg src=\"image/twm_1.jpg\" width=\"330px\" /\u003e\n \u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\n \u003cimg src=\"image/twm_2.jpg\" width=\"330px\" /\u003e\n\n\u003cbr\u003e\n\u003cbr\u003e\n\u003cbr\u003e\n\n \u003cimg src=\"image/two-sorted-merge-to-single-sorted.jpg\"  /\u003e\n\n\n\n## Balanced merge sort\n\u003cp\u003e\nการเรียงลำดับการผสาน k-way แบบสมดุลที่จัดเรียงสตรีมข้อมูลโดยใช้การผสานซ้ำๆ มันกระจายอินพุตออกเป็นสองสตรีมโดยอ่านบล็อกอินพุตที่พอดีกับหน่วยความจำซ้ำๆ รัน เรียงลำดับ แล้วเขียนไปยังสตรีมถัดไป จากนั้นจะผสานสองสตรีมซ้ำๆ และทำให้แต่ละรันที่ผสานเข้าเป็นหนึ่งในสองเอาต์พุตสตรีม จนกว่าจะมีเอาต์พุตที่จัดเรียงเพียงรายการเดียว\n\u003c/p\u003e\n\n\u003cp\u003e\nยกตัวอย่าง แม้ว่าดิสก์ไดร์ฟส่วนใหญ่สามารถทำงานกับไฟล์ชั่วคราวจำนวนมากได้แต่ก็ใช้ไม่ได้กับพื้นที่จัดเก็บเทป ซึ่งเป็นเรื่องยากที่จะมีไฟล์หลายไฟล์ในเทปเดียว และเรามีเทปไดร์ฟในจำนวนจำกัดเราสามารถทำได้ด้วย 3 หรือ 4 เทป แต่เราสามารถเพิ่มประสิทธิภาพได้ด้วยเทปไดร์ฟมากขึ้น\n\u003c/p\u003e\n\n### ตัวอย่าง 4-way merge\n \n\u003cimg src=\"image/4-way-merge-1.png\" width=\"500px\" /\u003e\n\n\u003cimg src=\"image/4-way.jpg\" width=\"500px\" /\u003e\n\n### Iterative 2-way merge \n\nการผสาน 2 ทางแบบวนซ้ำ เป็นการผสานอาร์เรย์ k สองชุดซ้ำๆ โดยใช้การผสานแบบ 2 ทางจนเหลือเพียงอาร์เรย์เดียว หากอาร์เรย์ถูกรวมในลำดับที่ต้องการ เวลาทำงานที่ได้จะเป็น O(kn) เวลาทำงานสามารถปรับปรุงได้โดยการรวมครั้งแรกกับครั้งที่สอง ครั้งที่สามกับครั้งที่สี่ และอื่นๆ\nเนื่องจาก จำนวนอาร์เรย์ลดลงครึ่งหนึ่งในการวนซ้ำแต่ละครั้ง ทุกองค์ประกอบจะถูกย้ายเพียงครั้งเดียว เวลาทำงานต่อการวนซ้ำจึงอยู่ใน Θ(n) เนื่องจาก n คือจำนวนขององค์ประกอบ เวลาทำงานทั้งหมดจึงเป็น Θ(n log k)\n \nPolyphase merge\n\tการเรียงลำดับแบบผสานเฟสเป็นรูปแบบของการเรียงลำดับการผสานจากล่างขึ้นบน ที่เรียงลำดับรายการโดยใช้การกระจาย รายการย่อย (runs) ที่ไม่สม่ำเสมอ\nซึ่งส่วนใหญ่ใช้สำหรับการเรียงลำดับภายนอกและมีประสิทธิภาพมากกว่าการเรียงลำดับ ผสานทั่วไปเมื่อมีน้อยกว่า 8 ไฟล์การทำงานภายนอก (เช่น เทปไดร์ฟหรือไฟล์บนฮาร์ดไดร์ฟ) การเรียงลำดับการรวมหลายเฟสไม่ใช่การเรียงลำดับที่เสถียร \nกล่าวคือ การเรียงลำดับแบบ polyphase merge จะลดจำนวนการรัน (runs) ทุกครั้งที่วนซ้ำของลูปหลักโดยการรวมรัน(runs) เป็นรัน(runs) ที่ใหญ่ขึ้น ซึ่งเป็นการรวมกันของสองเทคนิคคือ balanced two-way และ multi-way merging\n\n### Polyphase merge example\n\nสมมุติว่าเรามี 3 เทป  (T1, T2, T3 ) และเราจะ merge ตามลำดับดังนี้ \nจัดเรียงและแจกจ่ายบันทึกไปยัง T1 และ T2\nMerge T1 และ T2 ไปยัง T3 เหลือไว้บางตัวใน T2\nMerge T2 และ T3 ไปยัง T1 เหลือไว้บางตัวใน T2\nMerge T3 และ T1 ไปยัง T2 เหลือไว้บางตัวใน T3\nMerge T1 และ T2 ไปยัง T3 เหลือไว้บางตัวใน T2 และอื่นๆ\nเรามักทิ้งเทปต้นฉบับไว้สองอันและเทปหนึ่งอันสำหรับวางไฟล์ที่ผสาน\n \n\u003cimg src=\"image/polyphase.png\" /\u003e \n\n\n## อ้างอิง\n\n Aman Chauhan 1(2560) .  polyphase merge sort. ค้นคืนแล้ว 29 กันยายน 2564 แหล่งที่มา https://practice.geeksforgeeks.org/problems/explain-poly-way-merge-sort\n\nCSC 344 – Algorithms and Complexity. Lecture #4 – External Sorting. ค้นคืนแล้ว 29 กันยายน 2564. แหล่งที่มา https://home.adelphi.edu/~siegfried/cs344/344l4.pdf\n\nGeeksforGeeks(2564). Merge Sort. ค้นคืนแล้ว 28 กันยายน 2564. แหล่งที่มา https://www.geeksforgeeks.org/merge-sort/\n\nGeeksforGeeks(2564). External Sorting. ค้นคืนแล้ว 27 กันยากัน 2564. แหล่งที่มา  https://www.geeksforgeeks.org/external-sorting/\n\nMinsoo Jeon and Dongseung Kim. Load-Balanced Parallel Merge Sort. \nค้นคืนแล้ว 29 กันยายน 2564. แหล่งที่มา https://www.researchgate.net/publication/220091378_Parallel_Merge_Sort_with_Load_Balancing#pf4\n\nSon-klin Limthongkul(2557). External sorting. ค้นคืนแล้ว 28 กันยากัน 2564. แหล่งที่มา https://slideplayer.in.th/slide/2180294/\n\nSven Woltmann(2563). Merge Sort – Algorithm, Source Code, Time Complexity. \nค้นคืนแล้ว 28 กันยายน 2564. แหล่งที่มา\nhttps://www.happycoders.eu/algorithms/merge-sort/#Natural_Merge_Sort\n\nTa(2012). [ชีทสรุป พี่ต้า] Data Structure \u0026 Algorithm. Merge Sort. ค้นคืนแล้ว 28 กันยายน 2564. แหล่งที่มา https://docs.google.com/file/d/0B09a_TYwhKDZZXNSRHV3aFhuamc/edit?resourcekey=0-JzOfoiKxvZreJUR5U0vPYg\n\nWikipedia(2564). Merge sort. ค้นคืนแล้ว 27 กันยายน 2564. แหล่งที่มา https://en.wikipedia.org/wiki/Merge_sort\n\nWikipedia(2564).Two-way_merge. ค้นคืนแล้ว 29 กันยายน 2564 \t\nแหล่งที่มา https://en.wikipedia.org/wiki/K-way_merge_algorithm#Two-way_merge\n\nWikipedia(2021). Polyphase merge sort. ค้นคืนแล้ว 29 กันยายน 2564. แหล่งที่มา https://en.wikipedia.org/wiki/Polyphase_merge_sort\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakkaraponph%2Freport-external-sorted","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fakkaraponph%2Freport-external-sorted","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakkaraponph%2Freport-external-sorted/lists"}