Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help me with my small coding error: Here's my code below, and I need it to plot the algorithms time using StdDraw, whenever the method

Help me with my small coding error:

Here's my code below, and I need it to plot the algorithms time using StdDraw, whenever the method runs it gives me a blank graph. I get the correct number of counts thats not the issue. But there's something wrong with my plot method. Does anyone know how to fix this?

import java.awt.Color; import java.util.Arrays;

public class ThreeSumBrute { public static int count(int[] a) { Arrays.sort (a); int n = a.length; int count = 0; for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { for (int k = j+1; k < n; k++) { if (a[i] + a[j] + a[k] == 0) { count++; } } } } return count; } public static void main(String[] args) { In in = new In("1Kints.txt"); int[] a = in.readAllInts(); StdOut.println(count(a)); plot(a); } public static void plot(int[] a) { In in = new In("1Kints.txt"); a = in.readAllInts(); int n = a.length; StdDraw.setCanvasSize(512, 512); StdDraw.setXscale(-100, 100); StdDraw.setYscale(-100, 100); StdDraw.setPenRadius(0.01); StdDraw.setPenColor(Color.BLACK); StdDraw.save ("BrutePlot.jpg"); for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { for (int k = j+1; k < n; k++) { if (a[i] + a[j] + a[k] == 0) { double time = timeTrial(n); StdDraw.point (n, time); } } } } } public static double timeTrial(int n) { int MAX = 1000000; int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = StdRandom.uniform(-MAX, MAX); } Stopwatch timer = new Stopwatch(); ThreeSumBrute.count(a); return timer.elapsedTime(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Records And Database Management

Authors: Jeffrey R Stewart Ed D, Judith S Greene, Judith A Hickey

4th Edition

0070614741, 9780070614741

More Books

Students also viewed these Databases questions