2020年5月7日 星期四

泡泡排序法



泡泡排序法
int []a=new int[10];
void setup(){
   size(500,800);
   for(int i=0;i<10;i++){
      a[i]=int(random(10));
   }
   textSize(36);
   textAlign(LEFT,TOP);
   showArray(y);
}
int y=0;
void showArray(int y){
   for(int i=0;i<10;i++){
      fill(255); rect(i*50,y,50,50);
      fill(0);text(a[i],i*50,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);
}

沒有留言:

張貼留言