Advertisement
defineSN

BubbleSort@pointers,functions

Jan 19th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.69 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. void swap(int* a, int* b);
  4.  
  5. int main(){
  6.    
  7.     int first=0,last=0,n=0,i=0,j=0,arr[15];
  8.    
  9.     printf("enter the size of the array(less than 15): ");
  10.     scanf("%d",&n);
  11.     printf("enter the array members: ");
  12.     for(i=0;i<n;i++){
  13.         scanf("%d",&arr[i]);   
  14.     }
  15.     printf("\nthe entered array: ");
  16.         for(i=0;i<n;i++){
  17.             printf("%d ",arr[i]);  
  18.     }
  19.    
  20.     last=(n-1);
  21.    
  22.     //bubble sort
  23.     for(i=last;i>first;i--){
  24.        
  25.         for(j=first;j<last;j++){
  26.            
  27.             if(arr[j]>arr[j+1]){
  28.                 swap(&arr[j],&arr[j+1]);
  29.             }      
  30.         }  
  31.     }
  32.     printf("\nthe sorted array: ");
  33.         for(i=0;i<n;i++){
  34.             printf("%d ",arr[i]);  
  35.     }
  36.     return 0;
  37. }
  38.  
  39. void swap(int* a, int* b){ 
  40.     *a=*a + *b;
  41.     *b=*a-*b;
  42.     *a=*a-*b;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement