ArrayBubbleSort

ArrayBubbleSort - System.out.println( a[i]); S } public...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
// Sorts the values in a one-dimensional array - bubble sort / public class ArrayBubbleSort { public static void main(String[] args) { int[] a = {12, 62, 5, 11, 14, 19, 3, 9, 0}; // print out the values in the array System.out.println("The original values are: "); for (int i = 0 ; i < a.length; i++) System.out.println( a[i]); S // sort the value in the array bubbleSort(a, a.length); // print out the values in the array System.out.println("\n\nThe sorted values are: "); for (int i = 0 ; i < a.length; i++)
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: System.out.println( a[i]); S } public static void bubbleSort( int data, int size) { int hold; int lastPosition = size - 1; for ( int pass = 0; pass &lt; lastPosition; pass++ ) { for ( int i = 0; i &lt; lastPosition ; i++) if ( data[i] &gt; data[i + 1] ) // compare values { hold = data[i]; // swap if out of order data[i] = data[i+1]; data[i + 1] = hold; }// at end of i loop, data[lastPosition] has the largest value } } // end of pass loop } }...
View Full Document

This note was uploaded on 06/07/2010 for the course CS 201 taught by Professor Staff during the Spring '08 term at Iona.

Ask a homework question - tutors are online