truebman 0 Newbie Poster

I've benn working on this problem for a week. I still can't get it remotely closely to working Can anyone HELP :'( me


----------------------------------------------------------------------------
The task is to write a program which reports the amount of time required by
various sorting algorithms to sort different numbers of data values. Then,
using the output of the program, draw a graph that represents the results.
You must at a minimum use one method whose performance is "on the order of N
squared", and one method that is better than that.

1 **************************************************************************
2 // Timertest.java
3
4 import java.io.*;
5 import java.util.*;
6 import java.text.*;
7
8 class Timer
9 {
10 private long starttick;
11
12 public void StartTimer()
13 // This function starts the timer
14 {
15 starttick = System.currentTimeMillis();
16 } // StartTimer
17 public double ElapsedSeconds()
18 /*
19 This function returns the number of seconds that have elapsed
20 (to the nearest millisecond) since the StartTimer function was
21 called.
22 */
23 {
24 return((System.currentTimeMillis() - starttick) / 1000.0);
25 } // ElapsedSeconds
26 } // class Timer
27 public class Timertest
28 {
29 public static void main(String[] args) throws IOException
30 // This function tests the functionality of the Timer class
31 {
32 int inbyte;
33 long mscount;
34 double seconds1,seconds,seconds3;
35 Timer t = new Timer();
36 DecimalFormat fmt = new DecimalFormat("###0.000");
37
38 System.out.println(
39 "Press one of the letters below, followed by Enter:");
40 System.out.println(" r = run timer");
41 System.out.println(" b = begin timer");
42 System.out.println(" s = stop timer");
43 System.out.println(" q = quit program");
44 do
45 {
46 do
47 {
48 inbyte = System.in.read();
49 } while (inbyte != 'b' && inbyte != 'q' && inbyte != 'r');
50 if (inbyte == 'r')
51 {
52 mscount = System.currentTimeMillis();
53 while (System.currentTimeMillis() - mscount < 50)
54 System.out.println(System.currentTimeMillis());
55 }
56 else if (inbyte != 'q')
57 {
58 System.out.println("Timing...");
59 t.StartTimer();
60 do
61 {
62 inbyte = System.in.read();
63 } while (inbyte != 's' && inbyte != 'q');
64 seconds = t.ElapsedSeconds();
65 System.out.println("Elapsed seconds: " + fmt.format(seconds));
66 }
67 } while (inbyte != 'q');
68 System.out.println("End of program.");
69 } // main
70 } // class Timertest
71 **************************************************************************
72 // Randomtest.java
73
74 import java.util.*;
75
76 public class Randomtest
77 {
78 public static void fillArray(int[] a)
79 /*
80 This function will fill an array with the numbers from 1 to
81 a.length and then will shuffle those numbers into a random
82 sequence.
83 */
84 {
85 int i,j,temp;
86 Random randnum = new Random();
87
88 for (i = 0; i < a.length; i++) a = i + 1;
89 for (i = 0; i < a.length; i++)
90 {
91 j = randnum.nextInt(a.length);
92 temp = a[j]; a[j] = a; a = temp;
93 }
94 } // fillArray
95 public static void main(String[] args)
96 // This function tests the fillArray function
97 {
98 int i;
99 int[] a = new int[10];
100
101 fillArray(a);
102 System.out.println("Random shuffle of numbers from 1 to " + a.length);
103 for (i = 0; i < a.length; i++)
104 {
105 System.out.println(" "+a);
106 }
107 } // main
108 } // class Randomtest
109 **************************************************************************
110 BUBBLE SORT EXAMPLE
111 **************************************************************************
112 The following illustrates how to translate pseudocode for the bubble sort
113 algorithm into Java code:
114 **************************************************************************
115
116 void bubblesort(int[] a, int n)
117 {
118 int i,j;
119 int temp;
120
121 for (i = 1; i < n; i++)
122 {
123 for (j = n-1; j >= i; j--);
124 {
125 if (a[j-1] > a[j])
126 {
127 temp = a[j-1];
128 a[j-1] = a[j];
129 a[j] = temp;
130 }
131 }
132 }
133 } // bubblesort
134
135 public static void main(String[] args)
136 {
137 final int MAX = 10000;
138 int n;
139 int[] a = new int[MAX];
140 double seconds;
141 Timer t = new Timer();
142
143 ...
144 fillArray(a);
145 ...
146 t.StartTimer();
147 bubblesort(a,n);
148 seconds = t.ElapsedSeconds();
149 ...
150 } // main
151 **************************************************************************