Preface :
Divide and conquer is a problem-solving technique that makes use of recursion. Here is the idea when applied to sorting algorithm :
The divide-and conquer strategy is the basic for the mergesort, which literally partitions the elements in half by splitting them at the midpoint. Recursive steps divide the list into two parts, each holding one-half of the elements, then into four parts, each holding one-forth of the elements, and so forth. The process continues as long as a sublist has two or more elements. When executing in reverse order, the recursive steps merge the sorted half-lists back into larger and larger ordered list until the algorithm has rebuilt the original sequence in ascending order.
MergeSort :
Before we talk about MsergeSort, let's discuss on how MsergeSort merge two ordered sublist into one larger ordered sublist.
The mergesort algorithm assumes the existence of an original array arr and a temporary arr tempArr, both containing n elements. The merging process focuses on a sequence of elements in array arr having index range [first, last). The sequence consists of two ordered sublists separated by an intermediate index, called mid. A merge involves copying the elements from the two sublists in array arr to the index range [first, last) in array tempArr so that the new sequence in tempArr is ordered. Consider the following example that describes a sequence of nine integer elements with index range [first, last). The sequence consists of the four-element sorted sublist A and the five-element sublist sublist B, with index ranges [first, mid) and [mid, last) respectively :
After all, the merge algorithm concludes by copying the elements from tempArr back to the original array, starting at index first.
The msort() Method :
The msrot() method is a recursive algorithm that takes two Object arrays arr and tempArr along with integers first and last that specifies the index range for the sublist which is to be sorted. The method creates two half-lists by computing the index midpt, representing the midpoint of the index range :
int midpt = (last + first) /2 ;
Each recursive step makes two calls to msort(). The first one uses indices first and mid to define the index range [first, mid) for the lower half-list of the original sequence. The second call uses indices mid and last to define the index range [mid, last) for the upper half-list. The process sets in motion a chain of recursive calls that partitions the original list into smaller and smaller sublist (half-list) until their size is 1 (stopping condition). Sublists of size 1 are obviously ordered. When revisiting the chain of recursive calls in reverse order, msort() uses the merge algorithm to build successively larger ordered sublist. The final merging of sublists corresponds to the first recursive step and arranges the original array in sorted order.
Tracing an example is a good way to understand the msrot() algorithm. Below figure illustrates the algorithm for the 11-elements Integer array :
The following is the implementation of msort(). The index range for a singleton sublist is [first, first+1), where first+1=last. Thus, the recursive partitioning process continues only as long as first + 1 < last. The concluding merge phase for each recursive step builds an ordered list with range [first, last) from the ordered lower sublist with range [first, mid) and the ordered upper sublist with range [mid, last). The method checks whether the list is already sorted by comparing the last element in the lower sublist (arr[mid-1]) with the first element in the upper sublist (arr[mid]). If arr[mid-1] is less than arr[mid], the list is sorted and no merging of sublist is required. The test would indicate that the following sublist from first to last is already ordered. The test provides optimization that results in faster sorts for nearly ordered lists.
Efficiency of MergeSort :
An intuitive analysis of the process indicates that the worst-case running time for mSort() is O(n log2(n)). Assume the array has n=2^k elements. Below figure is a tree illustrating the recursive partitioning into progressively smaller and smaller sublists. At each level in the tree, we want to evaluate the amount of work required to merge the half-lists into a single sorted list :
The first call to msort() at level 0 generates two recursive calls that produce half-lists of size n/2, and the merge() method combines the half-list to create the ordered n-element list. At level 1, there are two calls to msot(); each produce two additional recursive calls with lists of size n/4. Each merge joins two sublists of size n/4 to create an ordered list of size n/2. In general, at level i, there are 2^i calls to merge(), and each call orders n/(2^i) elements :
At each level i in the tree, the merge involves n/(2^i) elements with linear running time that requires less than n/(2^i) comparisons. The combined 2^i merge operations at level i require less than (2^i)*(n/(2^i)) = n comparisons. Assuming that n=2^k, the partition process terminates at level K with sublists of size n/(2^k)=1. The total work done on all of the levels is no more than :
This is a blog to track what I had learned and share knowledge with all who can take advantage of them
標籤
- [ 英文學習 ]
- [ 計算機概論 ]
- [ 深入雲計算 ]
- [ 雜七雜八 ]
- [ Algorithm in Java ]
- [ Data Structures with Java ]
- [ IR Class ]
- [ Java 文章收集 ]
- [ Java 代碼範本 ]
- [ Java 套件 ]
- [ JVM 應用 ]
- [ LFD Note ]
- [ MangoDB ]
- [ Math CC ]
- [ MongoDB ]
- [ MySQL 小學堂 ]
- [ Python 考題 ]
- [ Python 常見問題 ]
- [ Python 範例代碼 ]
- [心得扎記]
- [網路教學]
- [C 常見考題]
- [C 範例代碼]
- [C/C++ 範例代碼]
- [Intro Alg]
- [Java 代碼範本]
- [Java 套件]
- [Linux 小技巧]
- [Linux 小學堂]
- [Linux 命令]
- [ML In Action]
- [ML]
- [MLP]
- [Postgres]
- [Python 學習筆記]
- [Quick Python]
- [Software Engineering]
- [The python tutorial]
- 工具收集
- 設計模式
- 資料結構
- ActiveMQ In Action
- AI
- Algorithm
- Android
- Ansible
- AWS
- Big Data 研究
- C/C++
- C++
- CCDH
- CI/CD
- Coursera
- Database
- DB
- Design Pattern
- Device Driver Programming
- Docker
- Docker 工具
- Docker Practice
- Eclipse
- English Writing
- ExtJS 3.x
- FP
- Fraud Prevention
- FreeBSD
- GCC
- Git
- Git Pro
- GNU
- Golang
- Gradle
- Groovy
- Hadoop
- Hadoop. Hadoop Ecosystem
- Java
- Java Framework
- Java UI
- JavaIDE
- JavaScript
- Jenkins
- JFreeChart
- Kaggle
- Kali/Metasploit
- Keras
- KVM
- Learn Spark
- LeetCode
- Linux
- Lucene
- Math
- ML
- ML Udemy
- Mockito
- MPI
- Nachos
- Network
- NLP
- node js
- OO
- OpenCL
- OpenMP
- OSC
- OSGi
- Pandas
- Perl
- PostgreSQL
- Py DS
- Python
- Python 自製工具
- Python Std Library
- Python tools
- QEMU
- R
- Real Python
- RIA
- RTC
- Ruby
- Ruby Packages
- Scala
- ScalaIA
- SQLAlchemy
- TensorFlow
- Tools
- UML
- Unix
- Verilog
- Vmware
- Windows 技巧
- wxPython
訂閱:
張貼留言 (Atom)
[Git 常見問題] error: The following untracked working tree files would be overwritten by merge
Source From Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 # git clean -d -fx 方案2: 今天在服务器上 gi...
-
前言 : 為什麼程序管理這麼重要呢?這是因為: * 首先,本章一開始就談到的,我們在操作系統時的各項工作其實都是經過某個 PID 來達成的 (包括你的 bash 環境), 因此,能不能進行某項工作,就與該程序的權限有關了。 * 再來,如果您的 Linux 系統是個...
-
屬性 : 系統相關 - 檔案與目錄 語法 : du [參數] [檔案] 參數 | 功能 -a | 顯示目錄中個別檔案的大小 -b | 以bytes為單位顯示 -c | 顯示個別檔案大小與總和 -D | 顯示符號鏈結的來源檔大小 -h | Hum...
-
來源自 這裡 說明 : split 是 Perl 中非常有用的函式之一,它可以將一個字串分割並將之置於陣列中。若無特別的指定,該函式亦使用 RE 與 $_ 變數 語法 : * split /PATTERN/,EXPR,LIMIT * split /...
沒有留言:
張貼留言