Advertisement
Lisaveta777

sort arr/ swap

Oct 22nd, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #define SIZE 10
  3.  
  4. //https://otvet.mail.ru/question/211049328
  5.  
  6. void sort_arr(int *,int);
  7. void print_arr(int *,int);
  8. void swap_elements(int *,int *);
  9.  
  10. int main()
  11. {
  12.     int arr1[SIZE] = {3,5,7,-3,1,2,5,4,3,5};
  13.     //int arr2[SIZE] = {8,-3,18,2,3,4,33,4,2,5};
  14.  
  15.  
  16.     print_arr(arr1,SIZE);
  17.     sort_arr(arr1,SIZE);
  18.     print_arr(arr1,SIZE);
  19.  
  20.     return 0;
  21. }
  22. void print_arr(int *a1,int s)
  23. {
  24.     int i;
  25.     for(i=0;i<s;i++)
  26.     {
  27.         printf("%d\t",a1[i]);
  28.     }
  29.     printf("\n");
  30. }
  31. void sort_arr(int *a1,int s)
  32. {
  33.     int i,j,t;
  34.     for(i=0;i<s-1;i++)
  35.     {
  36.         printf("\ni is %d\n",i);
  37.         for(j=i+1;j<s;j++)
  38.         {
  39.            printf("%d\t",j);
  40.  
  41.             if(a1[i] > a1[j])
  42.             {
  43.                 swap_elements(&a1[i],&a1[j]);
  44.                 // t = a1[i]; //without swap()
  45.                 //a1[i] = a1[j];
  46.                 //a1[j] = t;
  47.  
  48.             }
  49.         }
  50.  
  51.  
  52.  
  53.     }
  54. }
  55. void swap_elements(int *el1,int *el2)
  56. {
  57.     int temp= *el1;//*el1
  58.     *el1 =  *el2;
  59.     *el2 = temp;
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement