Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Help with java insertion sort program Date: Thu, 14 Aug 2014 08:07:39 -0400 Organization: A noiseless patient Spider Lines: 45 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 14 Aug 2014 12:07:43 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="bd4e1cba8d8592bec5d5ed9e4537fbfd"; logging-data="31089"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18zdwxt+2PqIkaLyeJs0PiI" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.0 In-Reply-To: Cancel-Lock: sha1:a/pMEOGXqglN8AjBIWCVv/2AWT8= Xref: csiph.com comp.lang.java.programmer:29664 On 8/14/2014 12:51 AM, Mike Amling wrote: > On 8/10/14 6:43 AM, Eric Sosman wrote: >> On 8/10/2014 7:09 AM, Mike Amling wrote: >>> On 8/9/14 9:11 PM, Scott Dunning wrote: >>>> Hello I trying write a sorting program that will ask a user what >>>> type of >>>> sorting method to use (insertion, bubble, selection) and then ask them >>>> to enter integers to sort. >>> >>> One wonders why classes are still teaching these algorithms. I've had >>> colleagues who have written bubble sort into production code, because >>> that's the only algorithm they were ever taught (or maybe it was better >>> than some even worse ones). >>> >>> If you do complete this assignment, throw away the code, look up >>> QuickSort, implement a reasonable variation of it, and don't look back. >> >> Better still, look up Arrays.sort() and Collections.sort() and >> throw away your Quicksort implementation. > > How do you get Arrays.sort to sort primitives descending? Arrays.sort(theArrayOfPrimitives); for (int i = theArrayOfPrimitives.length; --i >= 0; ) { // Note direction of traversal } Or, if you *really* *really* *REEEELY* want the array front-to-back: Arrays.sort(theArrayOfPrimitives); for (int i = 0, j = theArrayOfPrimitives.length; i < --j; ++i) { PrimitiveType temp = theArrayOfPrimitives[i]; theArrayOfPrimitives[i] = theArrayOfPrimitives[j]; theArrayOfPrimitives[j] = temp; } > What if you want to, say, apply the same permutation to the elements of > array Y that sorting applies to the elements array X? Whenever I've wanted to do this, it's nearly always turned out that my data structure design was at fault. On the remaining few occasions, I've instead sorted an array of indices. -- esosman@comcast-dot-net.invalid