Sorting Algorithms - divide this into two sets

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
-Sorting Algorithms -bubble sort O(n^2) -heap sort O(nlogn) Merge Sort (Algorithms that uses “Divide and Conquer”) Divide and Conquer (is an strategy used in algorithms that divides the problems in smaller subproblems and so on until the subproblems are small enough so the solution is simple.) steps Divide- divide data into two or more disjoint sets. Renr -solve problem in the subsets. Conquer- take the solutions of the subproblems and combine them to get a big solution. Algorithms MergeSort(s) Input: array S (unsorted) Output: sorted array S Divide: If s has only one element , return; otherwise, put elements into two subarrays s1 and s2. The size of s1 and s2 is half the size of s .Call Mergesort(s1) and for s2 Conquer: put elements of s1 and s2 back into S. MergeSort(3,8,15,19,23,6,1,10)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: divide this into two sets >>mergeSort(3,8,15,19) and mergeSort(23,6,1,10) divide again >>mergeSort(3,8), mergeSort(15,19), mergeSort(23,6), mergeSort(1,10) divide again ...ms(3), ms(8),. ..... put two together s=(3,8) s=(15,19) s=(6,23) s=(1,10) again s=(3,8,15,19) s=(1,6,10,23) again s=(1,3,6,8,10,15,19,23) How long does merge sort take? To find this out, we use sth called amortized time, that is we find first the time spent in one element in the array and then multiply this by n (the number of elements) Instructions done for one element:-copy operations: the # of comparisons is also the # of times an element-comparison:nlog(n) is copied to the new array, in the merge step If there are n elements, the time will be O(nlog(n))...
View Full Document

This note was uploaded on 02/04/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 2

Sorting Algorithms - divide this into two sets

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online