sahajjain01

05.Sort the given array using Insertion Sort.

Aug 15th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.44 KB | None | 0 0
  1. //Program to sort the given array using Insertion Sort.
  2. #include <stdio.h>
  3. #include <conio.h>
  4.  
  5. void main()
  6. {
  7.     int i, j, temp,
  8.     arr[10] = {
  9.         1, 23, 55, 2, 26, 66, 87, 21, 88, 54
  10.     };
  11.  
  12.     clrscr();
  13.  
  14.     for (i = 1; i < 10; i++) {
  15.         j = i;
  16.  
  17.         while (j > 0 && arr[j] < arr[j-1]) {
  18.             temp     = arr[j];
  19.             arr[j]   = arr[j-1];
  20.             arr[j-1] = temp;
  21.  
  22.             j--;
  23.         }
  24.     }
  25.  
  26.     for (i = 0; i < 10; i++) {
  27.         printf("%d\n", arr[i]);
  28.     }
  29.  
  30.     getch();
  31. }
Add Comment
Please, Sign In to add comment