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 Size;
double SizeTwo;
int iterationsB = 0;
int iterationsS = 0;
int random = 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;
random =(int)(Math.random() * ((max - min) + 1)) + min; //inclusive;
int[] arr = new int[random];
for(int i = 0; i < arr.length; i++){
int random2 =(int)(Math.random() * ((1000000 - 1) + 1)) + 1; //inclusive;
arr[i] = random2;
}
int random3 = (int)(Math.random() * ((1000000 - 1) + 1)) + 1;
int randomSpot = (int)(Math.random() * ((random - 1) + 1)) + 1;
insertionSort(arr);
if(binarySearch(arr,0,arr.length-1,key) == -1){
arr[randomSpot] = key;
}
Size = sequentialSearch(arr,key);
SizeTwo = binarySearch(arr,0,arr.length-1,key);
double ratioPercentage = 400 / Size;
SizeTwo *= ratioPercentage;
Size *= ratioPercentage;
System.out.println(Size + " " + SizeTwo);
setDoubleBuffered(true);
}
public static void insertionSort(int array[]) {
int b = array.length;
for (int i = 1; i < b; i++) {
int key = array[i];
int p = i-1;
while ( ( array [p] > key ) && (p > -1) ) {
array [p+1] = array [p];
p--;
}
array[p+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)Size;
int spot2 = bottom - (int)SizeTwo;
g.fillRect(150, spot1,20,(int)Size);
g.setColor(Color.blue);
g.fillRect(300, spot2,20,(int)SizeTwo);
//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: " + random, 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.