Languages :: Java :: algorithm of simple java bubble sort |
|||
| By: Bernard |
Date: 07/10/2005 10:33:14 |
Points: 20 | Status: Answered Quality : Excellent |
|
I search for an algorithm of simple java bubble sort |
|||
| By: VGR | Date: 07/10/2005 10:34:12 | Type : Answer |
|
| Hello 8-) I suppose this PAQ will do it ;-) It's exactly an algorithm and it's almost directly portable to Java, given it's in Pascal ;-) |
|||
| By: spoonman | Date: 24/05/2007 14:02:56 | Type : Comment |
|
| Bubble sorts are an elementary way of sorting and for more complex lists they are probably not appropriate. Wikipedia has a nice breakdown on sorting algorithm's which you may want to look at. Examples of many of these algorithms can be found here. Below is how Sun suggests you do a bubble sort. This was taken from the link given above. /* * @(#)BubbleSortAlgorithm.java 1.6 95/01/31 James Gosling * * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. Please refer to the file "copyright.html" * for further important copyright and licensing information. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ /** * A bubble sort demonstration algorithm * SortAlgorithm.java, Thu Oct 27 10:32:35 1994 * * @author James Gosling * @version 1.6, 31 Jan 1995 * * Modified 23 Jun 1995 by Jason Harrison@cs.ubc.ca: * Algorithm completes early when no items have been swapped in the * last pass. */ class BubbleSort2Algorithm extends SortAlgorithm { void sort(int a[]) throws Exception { for (int i = a.length; --i>=0; ) { boolean flipped = false; for (int j = 0; j<i; j++) { if (stopRequested) { return; } if (a[j] > a[j+1]) { int T = a[j]; a[j] = a[j+1]; a[j+1] = T; flipped = true; } pause(i,j); } if (!flipped) { return; } } } } |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








