Advertisement
defineSN

InsertionSort@pointers,functions

Jan 19th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. void swap(int* a, int* b);
  4.  
  5. int main(){
  6.     int first=0,last=0,j=0,n=0,mark=0,bool=0,arr[15];
  7.    
  8.         do{
  9.             // entering array
  10.            
  11.             printf("enter the size of the array(less than 15): ");
  12.             scanf("%d",&n);
  13.             printf("enter the array members: ");
  14.             for(j=0;j<n;j++){
  15.                 scanf("%d",&arr[j]);   
  16.             }
  17.             printf("\nthe entered array: ");
  18.                 for(j=0;j<n;j++){
  19.                     printf("%d ",arr[j]);  
  20.             }
  21.            
  22.            
  23.             // sorting array
  24.            
  25.             last=(n-1);
  26.            
  27.             for(j=0,mark=0;mark<last;mark++){
  28.                 j=mark+1;
  29.                 while(arr[j-1]>arr[j]){
  30.                     swap(&arr[j-1],&arr[j]);
  31.                     if(j>1) // this step is CRUCIAL, as when j=1, decrementing it will result in arr[-1] to be compared in next while iteration
  32.                     j--;           
  33.                 }  
  34.             }
  35.            
  36.             // printing the sorted array
  37.            
  38.             printf("\nthe sorted array: ");
  39.                 for(j=0;j<n;j++){
  40.                     printf("%d ",arr[j]);  
  41.             }      
  42.            
  43.             printf("\n\niterate again for new number?\n yes(1) or no(0)\n enter choice: ");
  44.             scanf("%d",&bool);
  45.            
  46.         }while(bool==1);
  47.    
  48.     return 0;
  49. }
  50.  
  51. void swap(int* a, int* b){ 
  52.     *a=*a + *b;
  53.     *b=*a-*b;
  54.     *a=*a-*b;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement