sourav8256

Untitled

Jun 16th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1.  #include <stdio.h>
  2. void print_array(int,int[]);
  3. void swap(int,int,int[]);
  4.  
  5. int main(int argc, char const *argv[])
  6. {
  7.     int input_array[] = {23,43,56,63,53,55,21,66,89,54,33};
  8.     int input_array_size = sizeof(input_array)/sizeof(int);
  9.     print_array(input_array_size,input_array);
  10.  
  11.     /*it is not necessary to go till the very last number because we are checking that in i+1 anyway*/
  12.     for (int j = 0; j < input_array_size-1; j++){
  13.         /*again we can skip the last j numbers because they are already sorted in the previous loop*/
  14.         for(int i=0;i<input_array_size-j-1;i++){
  15.             if(input_array[i]>input_array[i+1]){
  16.                 printf("checking %d and %d\n",input_array[i],input_array[i+1] );
  17.                 swap(i,i+1,input_array);
  18.             }
  19.         }
  20.     }
  21.    
  22.  
  23.     print_array(input_array_size,input_array);
  24.     return 0;
  25. }
  26.  
  27. void print_array(int size,int array[]){
  28.     printf("\n");
  29.     for(int i=0;i<size;i++){
  30.         printf("[%d]",array[i] );
  31.     }
  32. }
  33.  
  34. void swap(int p1,int p2,int *array){
  35.     int temp = *(array+p1); // gets the value at the pointer location and saves it to temp
  36.     *(array+p1) = *(array+p2);
  37.     *(array+p2) = temp;
  38. }
Add Comment
Please, Sign In to add comment