MergeSort - { int leftEnd = rightPos -1; int tmpPos =...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
//running time <= CN* o(log N) = o(N * log N) public static void mergeSort(int [] a) { int [] tmpArray = new int [a.length]; merSort(a, tmpArray, 0, a.lenght-1 ); } pricate static void mergeSort(int[]a, int[] tmp, left, right) { if (left<right){ int center = (left+right)/2; mergeSort(a, tmp, left, center); mergeSort(a, tmp, center+1, right) merge(a, tmp, left, center+1, right) } } private static void merge(a, tmp, int leftPos, int rightPos, int rightEnd)
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { int leftEnd = rightPos -1; int tmpPos = leftPos; int left= leftPos; while(leftPos&lt;= leftEnd &amp;&amp; rightPos &lt;= rightEnd) { if(a[leftPos]&lt;a[rightPos]){ tmp[tmpPos++]=a[leftPos++]; } else { tmp[tmpPos++]=a[rightPos++]; } } while( leftPos&lt;= leftEnd) tmpArray[tmpPos++]=a[leftPos++]; while(rightPos&lt;=rightEnd) tmpArray[tmpPos++]=a[rightPos++]; for(int i=0 ; i&lt;rightEnd){ a[i]=tmp[i]; i++; } }...
View Full Document

This note was uploaded on 02/18/2011 for the course 22C 031 taught by Professor Kasturi during the Spring '11 term at University of Iowa.

Ask a homework question - tutors are online