2020年5月7日 星期四

泡泡排序法


泡泡排序法P程式碼:
(陣列)
int []a=new int[10];//Java's Array
void setup(){
  size(500,800);
  for(int i=0;i<10;i++){//random choose number
    a[i]=int(random(10));//亂數決定數字
  }
  textSize(36);///利用textsize調整數字大小
  textAlign(LEFT,TOP);/文字改用左上角
  showArray(y);
}
int y=0;
void showArray(int y){ //左上角
  for(int i=0;i<10;i++){//rect(x,y, w,h)四邊形
    fill(255);  rect(i*50, y, 50,50);
    fill(0);         text(a[i], i*50, y);
  }   ///秀文字(字,  x,    y) 放哪裡 (左下角)
}
void draw(){
  
}
void mousePressed(){
  for(int i=0; i<10-1;i++){
    if(a[i] > a[i+1]){
      int temp=a[i];
      a[i]=a[i+1];
      a[i+1]=temp;
      fill(255,0,0,128); rect(i*50, y, 100,50);
    }
  }
  y+=50;
  showArray(y);
}




可調整背景顏色,及數字顏色深淺

沒有留言:

張貼留言