RandomNotryBubbles

RandomNotryBubbles -...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
import java.util.*; import java.io.*; // Import all classes in java.io package. Saves typing. // the BufferedReaderClass we will use for keyboard // input is in this package public class RandomNotryBubbles { p public static void main(String[] args){ String line; // to hold user keyboard input double num = 0; // to hold the integer converted from the the user input int i; int MAX; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); MAX = Integer.parseInt(args[0]); if (MAX <= 0) { System.out.println("Must give a positive integer"); return; } Random gen = new Random(); double ranNum; double[] numbers; numbers = new double[MAX]; // double[] numbers = new double[MAX]; System.out.println("generating random numbers to sort"); for (i=0; i<numbers.length; i++) { ranNum = 100*gen.nextDouble();//between 0 and 1000 System.out.println("number: "+ranNum); numbers[i] = ranNum; }// end for System.out.print("original numbers: "); printValues(numbers); System.out.println();
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: System.out.println(&quot;largest value: &quot;+ getLargest(numbers)); System.out.print(&quot;one bubble numbers: &quot;); bubbleUpBiggest(numbers); printValues(numbers); System.out.println(); System.out.print(&quot;bubble sorted numbers: &quot;); bubbleSort(numbers); printValues(numbers); System.out.println(); }// end main method public static void printValues (double arr) { int i; for (i=0; i&lt;arr.length; i++) { System.out.print(arr[i]+&quot;, &quot;); } return; } public static double getLargest (double arr) { int i; double big = 0; for (i=0; i&lt;arr.length; i++) { if (arr[i]&gt;big) { big = arr[i]; } } return big; } public static void bubbleUpBiggest (double arr) { int i; double temp; for (i=0; i&lt;arr.length-1; i++) { if (arr[i] &gt; arr[i+1]) { temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } } return; } public static void bubbleSort (double arr) { int i; for (i=0; i&lt;arr.length; i++) { bubbleUpBiggest(arr); } return; } } // end class...
View Full Document

Page1 / 2

RandomNotryBubbles -...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online