100% found this document useful (1 vote)
39 views

#Include : Printf Scanf Printf

The document contains code to sort an array of integers in ascending order using bubble sort. It then inserts a user-provided integer into the sorted array at the appropriate position based on its value. The code first inputs elements into an array, sorts the array, prints the sorted array, takes input for an element to insert, finds the correct insertion position, makes space at that position by shifting elements, inserts the element, and finally prints the array with the inserted element.

Uploaded by

sindhu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
39 views

#Include : Printf Scanf Printf

The document contains code to sort an array of integers in ascending order using bubble sort. It then inserts a user-provided integer into the sorted array at the appropriate position based on its value. The code first inputs elements into an array, sorts the array, prints the sorted array, takes input for an element to insert, finds the correct insertion position, makes space at that position by shifting elements, inserts the element, and finally prints the array with the inserted element.

Uploaded by

sindhu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1. #include <stdio.

h>
2. void main()
3. {
4. int array[10];
5. int i, j, n, m, temp, key, pos;
6.
7. printf("Enter how many elements \n");
8. scanf("%d", &n);
9. printf("Enter the elements \n");
10.
11. for (i = 0; i < n; i++)
12. {
13. scanf("%d", &array[i]);
14. }
15.
16. printf("Input array elements are \n");
17. for (i = 0; i < n; i++)
18. {
19. printf("%d\n", array[i]);
20. }
21.
22. // Sorting the elements of the array
23. for (i = 0; i < n; i++)
24. {
25. for (j = i + 1; j < n; j++)
26. {
27. if (array[i] > array[j])
28. {
29. temp = array[i];
30. array[i] = array[j];
31. array[j] = temp;
32. }
33. }
34. }
35.
36. printf("Sorted list is \n");
37. for (i = 0; i < n; i++)
38. {
39. printf("%d\n", array[i]);
40. }
41.
42. printf("Enter the element to be inserted \n");
43. scanf("%d", &key);
44.
45. for (i = 0; i < n; i++)
46. {
47. if (key < array[i])
48. {
49. pos = i;
50. break;
51. }
52. if (key > array[n-1])
53. {
54. pos = n;
55. break;
56. }
57. }
58. if (pos != n)
59. {
60. m = n - pos + 1 ;
61. for (i = 0; i <= m; i++)
62. {
63. array[n - i + 2] = array[n - i + 1] ;
64. }
65. }
66.
67. array[pos] = key;
68.
69. printf("Final list is \n");
70. for (i = 0; i < n + 1; i++)
71. {
72. printf("%d\n", array[i]);
73. }
74.
75. }

You might also like