Day17 - import java.util.Random; class Day17 cfw_ public...

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

View Full Document Right Arrow Icon
import java.util.Random; class Day17 { public static void mergeSort( int [ ] arr ) { if( arr.length > 1 ) { int totalItems = arr.length; int halfItems = arr.length / 2; int [ ] firstHalf = new int[ halfItems ]; int [ ] secondHalf = new int[ totalItems - halfItems ]; int i; for( i = 0; i < firstHalf.length; i++ ) firstHalf[ i ] = arr[ i ]; for( int j = 0; j < secondHalf.length; i++, j++ ) secondHalf[ j ] = arr[ i ]; mergeSort( firstHalf ); mergeSort( secondHalf ); merge( firstHalf, secondHalf, arr ); } } private static void merge( int [ ] first, int [ ] second, int [ ] result ) { if( first.length + second.length != result.length ) throw new IllegalArgumentException( "OOPS!!! " + first.length + " " + second.length + " " + result.length ); int f = 0, s = 0, r = 0; { if( first[ f ] <= second[ s ] ) result[ r++ ] = first[ f++ ]; else result[ r++ ] = second[ s++ ]; } while( f < first.length ) result[ r++ ] = first[ f++ ]; while( s < second.length )
Background image of page 1

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

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

This note was uploaded on 11/06/2011 for the course COMPUTER ENC1230 taught by Professor Jailhwashi during the Spring '11 term at FIU.

Page1 / 3

Day17 - import java.util.Random; class Day17 cfw_ public...

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