TEXT   195
algo lab .insertion
By asha945 on 8th February 2023 03:53:25 AM


  1. #include <stdio.h>
  2. void insertion_sort (int a[], int length){
  3.  
  4.  
  5. for(int i=1;i< length;i++){
  6.     int key=a[i],j;
  7.     j=i-1;
  8.     while (j>=0 && a[j]>key){
  9.         a[j+1]=a[j];
  10.         j=j-1;
  11.     }
  12.     a[j+1]=key;
  13. }
  14. }
  15.  
  16.  
  17. main(){
  18. int a[]={8,4,9,5,7,6,3,2},i;
  19. insertion_sort(a,8);
  20. for(int i=0;i<8;i++){
  21.     printf("Sorted array is:[%d]=%d\n",i,a[i]);
  22. }
  23. return 0;
  24. }

Raw Paste

Login or Register to edit or fork this paste. It's free.