web123456

012-JAVA bubble sorting, finding the most value and other simple algorithms (array exercises)

  • public class BubbleSort
  • {
  • //Bubble sort
  • public static void main(String[] args)
  • {
  • int a[] = {1,3,11,2,100,-1,0,99,78,123};
  • int i,j,t;
  • //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  • ("Raw Data:");
  • for(i=0;i<10;i++){
  • (a[i] + "\t");
  • }
  • ("");//Line break
  • //-------------------------------------------
  • for(i=0;i<9;i++){
  • //Show sorting process
  • ("Third" +(i+1) + "Sorting: a["+i+"] and a["+(i+1)+ "]~a[9]exchange");
  • //-------------------------------------------
  • for(j=i+1;j<10;j++){
  • if(a[i]>a[j]){
  • //Exchange the values ​​of a[i], a[j], t is a temporary variable
  • t = a[i];
  • a[i] = a[j];
  • a[j] = t;
  • }
  • //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  • for(int k=0;k<10;k++){
  • (a[k] + "\t");
  • }
  • ("");//Line break
  • //-------------------------------------------
  • }
  • }
  • //Sort data:
  • for(i=0;i<10;i++)
  • (a[i] + "\t");
  • }
  • }