BigO

import java.util.*; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.Timer; import java.util.ArrayList; import java.util.Iterator; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JPanel; import javax.imageio.*; import java.awt.image.*; import java.io.*; public class BigOChart extends JPanel implements Runnable, MouseListener { boolean ingame = true; private Dimension d; int BOARD_WIDTH=400; int BOARD_HEIGHT=600; private Thread animator; int count = 0; int bottom = 430; double size1; double size2; int iterationsB = 0; int iterationsS = 0; int rnd = 0; public BigOChart() { addKeyListener(new TAdapter()); addMouseListener(this); setFocusable(true); d = new Dimension(BOARD_WIDTH, BOARD_HEIGHT); setBackground(Color.black); if (animator == null || !ingame) { animator = new Thread(this); animator.start(); } int key = 30; //need item you are looking for int max = 10000; int min = 1; rnd =(int)(Math.random() * ((max - min) + 1)) + min; //inclusive; int[] arr = new int[rnd]; for(int i = 0; i < arr.length; i++){ int rnd2 =(int)(Math.random() * ((1000000 - 1) + 1)) + 1; //inclusive; arr[i] = rnd2; } int rnd3 = (int)(Math.random() * ((1000000 - 1) + 1)) + 1; int rndSpot = (int)(Math.random() * ((rnd - 1) + 1)) + 1; insertionSort(arr); if(binarySearch(arr,0,arr.length-1,key) == -1){ arr[rndSpot] = key; } size1 = sequentialSearch(arr,key); size2 = binarySearch(arr,0,arr.length-1,key); double ratioPercentage = 400 / size1; size2 *= ratioPercentage; size1 *= ratioPercentage; System.out.println(size1 + " " + size2); setDoubleBuffered(true); } public static void insertionSort(int array[]) { int n = array.length; for (int j = 1; j < n; j++) { int key = array[j]; int i = j-1; while ( (i > -1) && ( array [i] > key ) ) { array [i+1] = array [i]; i--; } array[i+1] = key; } } public int binarySearch(int arr[], int first, int last, int key){ while (first <= last) { int mid = (first + last) / 2; if (arr[mid] < key) { first = mid + 1; } else if (arr[mid] > key) { last = mid - 1; } else if (arr[mid] == key) { break; } iterationsB ++; } return iterationsB; } public int sequentialSearch(int[] elements, int target) { for (int j = 0; j < elements.length; j++) { if (elements[j] == target) { return j; } iterationsS++; } return iterationsS; } public void paint(Graphics g) { super.paint(g); g.setColor(Color.white); g.fillRect(0, 0, d.width, d.height); //lines g.setColor(Color.black); g.fillRect(90, 30, 2, bottom-30); g.fillRect(90, bottom-2, 300,2); //bar graph g.setColor(Color.red); int spot1 = bottom - (int)size1; int spot2 = bottom - (int)size2; g.fillRect(150, spot1,20,(int)size1); g.setColor(Color.blue); g.fillRect(300, spot2,20,(int)size2); //Text to screen Font small = new Font("Helvetica", Font.BOLD, 14); FontMetrics metr = this.getFontMetrics(small); g.setColor(Color.black); g.setFont(small); g.drawString("Iterations", 10, 250); g.setColor(Color.red); g.drawString("" + iterationsS, 30, spot1 + 10); g.setColor(Color.blue); g.drawString("" + iterationsB, 30, spot2); g.setColor(Color.red); g.drawString("Sequential Search", 100, 450); g.setColor(Color.blue); g.drawString("Binary Search", 250, 450); g.setColor(Color.black); g.drawString("Data Set Size: " + rnd, 150, 470); //text to screen above Toolkit.getDefaultToolkit().sync(); g.dispose(); } private class TAdapter extends KeyAdapter { public void keyReleased(KeyEvent e) { // player.keyReleased(e); int key = e.getKeyCode(); } public void keyPressed(KeyEvent e) { //System.out.println( e.getKeyCode()); // message = "Key Pressed: " + e.getKeyCode(); int key = e.getKeyCode(); if(key==39){ } } } public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void run() { long beforeTime, timeDiff, sleep; beforeTime = System.currentTimeMillis(); int animationDelay = 20; long time = System.currentTimeMillis(); while (true) {//infinite loop // spriteManager.update(); repaint(); try { time += animationDelay; Thread.sleep(Math.max(0,time - System.currentTimeMillis())); }catch (InterruptedException e) { System.out.println(e); }//end catch }//end while loop }//end of run }//end of class

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.