




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An implementation of the optimized quicksort algorithm in java, including the use of a random pivot, median-of-three pivot, and insertion sort for segments with fewer than 10 entries. The benchmark program includes two optimizations and accepts initial and final array sizes, increment for array size, and the number of random arrays to be averaged.
Typology: Assignments
1 / 8
This page cannot be seen from the preview
Don't miss anything!
Benchmarking of... Page 2
The benchmark program uses a global variable “PIVOT” which contains 0, 1, or 2. Within the partition method, the pivot value is assumed to be in x[lo]. If PIVOT==1, a random location within the region [lo..hi] is swapped with x[lo]. If PIVOT==2, the above logic sets the median value into that location. The rest of the partition method uses an implementation I have taken from Thomas Naps 5 (and was included in Hoare's original discussion^1 ). The QuickSort implementation used includes two optimizations: (1) if the array segment to be sorted contains fewer than 10 entries, the insertion sort is used on it; (2) since the sorting of the right-hand segment is tail recursive, that recursive call is replaced by a loop depending on the update of parameters — the initial “if” becomes “while (hi > lo)”, and the recursive call is replaced by the updated “hi = mid + 1”. The main accepts from the user (or the command line) the initial array size, the final array size, the increment for the array size, and the number of random arrays to be averaged — given the current speed of computers, we cannot measure the time required to sort a single array except for sizes that would probably exceed physical memory available. The data show that selection of a random pivot does indeed slow down the sorting, while the median-of-three speeds it up. The program was run on wyoming.ewu.edu, a computer with a single 2 GHz Intel® Pentium®–4 processor, within the Red Hat Linux and Java 1.4. environments.
Printed 2020-11 月-30 at 19:
Benchmarking of... Page 4 As usual, the best choice of an algorithm depends on the data that your program is likely to encounter. If there is a chance that the data will be sorted or nearly sorted, then the 8% overhead of the random pivot is a small price to pay for avoiding the O(n^2 ) behavior. On the other hand, the expense of selecting the middle element rather than the first is minimal and provides quite effective protection against sorted data. Then again, if you are assured that your data will not be sorted or nearly sorted, then you can achieve about a 5% speed-up by using the median-of-three partitioning strategy. Printed 2020-11 月-30 at 19:
Benchmarking of... Page 5 CkPivot.java /**
Benchmarking of... Page 7 System.out.print ("Maximum array length: "); if ( args.length < 2 ) maxSize = readInt(); else { maxSize = Integer.parseInt(args[1]); System.out.println ("" + maxSize); } // end if/else regarding args[1] System.out.print ("Increment for length: "); if ( args.length < 3 ) sizeStep = readInt(); else { sizeStep = Integer.parseInt(args[2]); System.out.println ("" + sizeStep); } // end if/else regarding args[2] if ( DISPLAY ) nPasses = 1; else { System.out.print ("Number of passes: "); if ( args.length < 4 ) nPasses = readInt(); else { nPasses = Integer.parseInt(args[3]); System.out.println ("" + nPasses); } // end if/else regarding args[3] } // end if/else test = new Integer [maxSize]; fillArray ( test ); System.out.println ("n,front,random,median-of-three"); while ( size <= maxSize ) { long seed = generator.nextLong(); // Same sequence in all 3 // Partition uses first item as pivot start = System.currentTimeMillis(); generator.setSeed(seed); PIVOT = 0; for ( pass = 0; pass < nPasses; pass++ ) { shuffleArray ( test, size ); if (DISPLAY) showArray ( test, size ); qSortO ( test, size ); if ( CHECK && !sorted ( test, size ) ) { System.out.println("Sort failed. Abort in pass " + (pass+1)); break; } } // end for elapsed[0] = (System.currentTimeMillis() - start)/1000.0; // Partition uses a random pivot start = System.currentTimeMillis(); generator.setSeed(seed); PIVOT = 1; for ( pass = 0; pass < nPasses; pass++ ) { shuffleArray ( test, size ); if (DISPLAY) showArray ( test, size ); qSortO ( test, size ); if ( CHECK && !sorted ( test, size ) ) { System.out.println("Sort failed. Abort in pass " + (pass+1)); break; } } // end for elapsed[1] = (System.currentTimeMillis() - start)/1000.0; // Partition uses median-of-three for pivot start = System.currentTimeMillis(); generator.setSeed(seed); PIVOT = 2; for ( pass = 0; pass < nPasses; pass++ ) { shuffleArray ( test, size ); if (DISPLAY) showArray ( test, size ); qSortO ( test, size ); if ( CHECK && !sorted ( test, size ) ) { System.out.println("Sort failed. Abort in pass " + (pass+1)); break; } } // end for elapsed[2] = (System.currentTimeMillis() - start)/1000.0; if (DISPLAY) { System.out.println (""); showArray ( test, size ); } System.out.print (size+""); for ( int k = 0; k < elapsed.length; k++ ) System.out.print ( "," + elapsed[k] ); System.out.println(""); size += sizeStep; // Next size } // end while ( size <= maxSize ) } // end main() /**