Advertisement
Lisaveta777

C_tasks 5.28 bubble

Dec 17th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. Пейстбин
  2.  
  3. #include <stdio.h>
  4. #define SIZE 10
  5. //sort array: compare two neighboring elements, starts with 0el first time, and
  6. //smallest end up at 0-place, starts with 1st el second time-sm to 1st place...
  7. //bubble
  8. int main()
  9. {
  10.     int flag,temp,i,j,k,arr[SIZE]={3,2,4,1,5,6,7,8,9,10};//{9,5,7,3,2,6,8,4,1,0};
  11.     for(i=0;i<SIZE;i++)printf("%d\t",arr[i]);//print array
  12.  
  13.     for(i=0;i<SIZE;i++)//place to put max number
  14.     {printf("\ni=%d\n",i);flag=0;//place=SIZE-i;
  15.         for(j=0;j<SIZE;j++)
  16.         {printf("j=%d\t",j);
  17.             if(arr[j]>arr[j+1])
  18.             temp=arr[j],arr[j]=arr[j+1],arr[j+1]=temp,flag++;
  19.         }//for j
  20.         if(!flag)
  21.             printf("\n%d lines\n",i),i=SIZE;//skip the rest!!!
  22.  
  23.         printf("\n");
  24.         for(k=0;k<SIZE;k++)printf("%d\t",arr[k]);//print array'
  25.         printf("\n");
  26.         if(i==SIZE-2)i=SIZE;
  27.     }//for i
  28.     for(i=0;i<SIZE;i++)printf("%d\t",arr[i]);//print array
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement