Wrox Programmer Forums
|
BOOK Programming Interviews Exposed: Secrets to Landing Your Next Job 3rd Edition
This is the forum to discuss the Wrox book Programming Interviews Exposed: Secrets to Landing Your Next Job, 3rd Edition by John Mongan, Noah Kindler, Eric Giguere; ISBN: 978-1-118-26136-1
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK Programming Interviews Exposed: Secrets to Landing Your Next Job 3rd Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 12th, 2015, 02:29 PM
mjr mjr is offline
Registered User
 
Join Date: Mar 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Optimized Quicksort (3rd ed., p 142)

Should the comparison be changed from <= to <, since there's no need to swap an element with itself, unless
the swap isn't important (i.e., it doesn't do any harm) at that point and the need is just to adjust both i and j variables?

// If the values are in the wrong order, swap them.
if( i <= j ){

Compared to
// ...
if( i < j ) {

Thanks,

Last edited by mjr; March 12th, 2015 at 02:57 PM..
 
Old July 11th, 2017, 03:20 PM
Wrox Author
 
Join Date: Oct 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Exactly: when i == j the swap does nothing, but adjusting the values of i and j is essential. You could modify the code to eliminate this wasted swap, but I'm not sure the increased complexity would be worth it.

John





Similar Threads
Thread Thread Starter Forum Replies Last Post
Initial Comments - XSLT 2.0 3rd Ed. baryza Wrox Book Feedback 1 July 9th, 2007 07:18 PM
doubts on introduction of professional c# 3rd ed benraidertx BOOK: Professional C#, 2nd and 3rd Editions 1 June 1st, 2005 04:47 PM
Where did the Appendicies go? - 3rd ed AndrewGiles BOOK: Professional C#, 2nd and 3rd Editions 1 January 4th, 2005 08:04 PM
Expanding Forum to Cover Professional C# 3rd Ed jminatel BOOK: Professional C#, 2nd and 3rd Editions 0 July 21st, 2004 03:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.