File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ 
2+ #include  < iostream> 
3+ using  namespace  std ; 
4+ void  display (int  *array, int  size)
5+ {
6+  for  (int  i = 0 ; i < size; i++)
7+  cout << array[i] << "  " 
8+  cout << endl;
9+ }
10+ void  insertionSort (int  *array, int  size)
11+ {
12+  int  key, j;
13+  for  (int  i = 1 ; i < size; i++)
14+  {
15+  key = array[i]; // take value
16+  j = i;
17+  while  (j > 0  && array[j - 1 ] > key)
18+  {
19+  array[j] = array[j - 1 ];
20+  j--;
21+  }
22+  array[j] = key; 
23+  }
24+ }
25+ int  main ()
26+ {
27+  int  n;
28+  cout << " Enter the number of elements: " 
29+  cin >> n;
30+  int  arr[n]; // create an array with given number of elements
31+  cout << " Enter elements:" 
32+  for  (int  i = 0 ; i < n; i++)
33+  {
34+  cin >> arr[i];
35+  }
36+  cout << " Array before Sorting: " 
37+  display (arr, n);
38+  insertionSort (arr, n);
39+  cout << " Array after Sorting: " 
40+  display (arr, n);
41+ }
                         You can’t perform that action at this time. 
           
                  
0 commit comments