Bubble Sort

by Vijayaprasad 2010-01-28 16:04:54

/*
* @(#)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;
}
}
}
}

Tagged in:

820
like
0
dislike
0
mail
flag

You must LOGIN to add comments