Sorting Insertion Sort Merge Sort
Sorting Insertion Sort Merge Sort
Sorting
The sorting problem is to arrange a sequence of records so that the values of their key fields form a nondecreasing sequence. That is, given records r1, r2, . . . ,rn, with key values k1, k2, . . . , kn, respectively, we must produce the same records in an order ri1, ri2 , . . . , rin such that ki1 ki2 . kin . The records all need not have distinct values
Insertion Sort
Insertion sort is a simple sorting algorithm that is appropriate for small inputs Its very time consuming so if the size of input is large, Insertion Sort is not a likely choice for sorting that data. Merge Sort, QuickSort are faster algorithms for sorting for that matter.
Merge Sort
MergeSort Algorithms fall under divide and conquer category The divide and conquer strategy is well known in wars. The philosophy of this strategy is , divide your enemy into parts and then conquer these parts. To conquer these parts is easy, as these parts cannot resist or react like a big united enemy. The same philosophy is applied in the above algorithms. To understand the divide and conquer strategy in sorting algorithm, lets consider an example.
Mergesort
Merge-sort is based on an algorithmic design pattern called divide-andconquer. The divide-and-conquer pattern consists of the following three steps: 1. Divide: If the input size is smaller than a certain threshold (say, one or two elements), solve the problem directly using a straightforward method and return the solution obtained. Otherwise, divide the input data into two or more disjoint subsets. 2. Recur: Recursively solve the subproblems associated with the subsets. 3. Conquer: Take the solutions to the subproblems and merge them into a solution to the original problem.
Mergesort Con
To sort a sequence S with n elements using the three divide-andconquer steps, the merge-sort algorithm proceeds as follows: 1. Divide: If S has zero or one element, return S immediately; it is already sorted. Otherwise (S has at least two elements), remove all the elements from S and put them into two sequences, S1 and S2, each containing about half of the elements of S; that is, S1 contains the first n/2 elements of S, and S2 contains the remaining n/2 elements. 2. Recur: Recursively sort sequences S1 and S2. 3. Conquer: Put back the elements into S by merging the sorted sequences S1 and S2 into a sorted sequence.
Mergesort Con
Mergesort Con.