Guest User

Untitled

a guest
Nov 17th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1.  
  2. package sortgui;
  3.  
  4. import java.applet.Applet;
  5. import java.awt.*;
  6.  
  7.  
  8. public class SortGui extends Applet implements Runnable{
  9.  
  10. int[] bubbel= new int[200];
  11. boolean oordning=true;
  12. double tid;
  13. int v=20;
  14. int ctal;
  15. boolean tilldela;
  16.  
  17. public void init(){
  18. tid=System.currentTimeMillis();
  19.  
  20. for(int slump=0; slump<bubbel.length; slump++){
  21.  
  22. bubbel[slump]=(int)(Math.random()*200);
  23. }
  24. }
  25.  
  26.  
  27. public void start(){
  28. Thread trad= new Thread(this);
  29. trad.start();
  30. }
  31.  
  32. public void paint(Graphics g){
  33. for(int x=0; x<bubbel.length; x++){
  34. g.fillOval(x, bubbel[x], 4, 4);
  35. }
  36. }
  37.  
  38. public void sort() {
  39. while(oordning){
  40. repaint();
  41. try {
  42. Thread.sleep(v);
  43. } catch (Exception e) {
  44. }
  45. oordning=false;
  46. for(int tal=0; tal<bubbel.length-1; tal++){
  47. if(check(bubbel[tal], bubbel[tal+1])){
  48. oordning=true;
  49. change(tal);
  50. }
  51. }
  52. }
  53. }
  54.  
  55. public boolean check(int a, int b){
  56. if(b>a){
  57. return true;
  58. }
  59. return false;
  60. }
  61.  
  62. public void change(int tal){
  63. int byt;
  64. byt=bubbel[tal];
  65. bubbel[tal]=bubbel[tal+1];
  66. bubbel[tal+1]=byt;
  67. }
  68.  
  69. @Override
  70. public void run() {
  71. try {
  72. Thread.sleep(2000);
  73. } catch (Exception e) {
  74. }
  75. sort();
  76. //for(int hej=0; hej<bubbel.length; hej++){
  77. for(int kontroll:bubbel){
  78. System.out.println(bubbel[kontroll]);
  79. }
  80. // System.out.println(System.currentTimeMillis()-tid);
  81. }
  82. }
Add Comment
Please, Sign In to add comment