Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sortgui;
- import java.applet.Applet;
- import java.awt.*;
- public class SortGui extends Applet implements Runnable{
- int[] bubbel= new int[200];
- boolean oordning=true;
- double tid;
- int v=20;
- int ctal;
- boolean tilldela;
- public void init(){
- tid=System.currentTimeMillis();
- for(int slump=0; slump<bubbel.length; slump++){
- bubbel[slump]=(int)(Math.random()*200);
- }
- }
- public void start(){
- Thread trad= new Thread(this);
- trad.start();
- }
- public void paint(Graphics g){
- for(int x=0; x<bubbel.length; x++){
- g.fillOval(x, bubbel[x], 4, 4);
- }
- }
- public void sort() {
- while(oordning){
- repaint();
- try {
- Thread.sleep(v);
- } catch (Exception e) {
- }
- oordning=false;
- for(int tal=0; tal<bubbel.length-1; tal++){
- if(check(bubbel[tal], bubbel[tal+1])){
- oordning=true;
- change(tal);
- }
- }
- }
- }
- public boolean check(int a, int b){
- if(b>a){
- return true;
- }
- return false;
- }
- public void change(int tal){
- int byt;
- byt=bubbel[tal];
- bubbel[tal]=bubbel[tal+1];
- bubbel[tal+1]=byt;
- }
- @Override
- public void run() {
- try {
- Thread.sleep(2000);
- } catch (Exception e) {
- }
- sort();
- //for(int hej=0; hej<bubbel.length; hej++){
- for(int kontroll:bubbel){
- System.out.println(bubbel[kontroll]);
- }
- // System.out.println(System.currentTimeMillis()-tid);
- }
- }
Add Comment
Please, Sign In to add comment