fork download
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. void bubble(long[], long);
  6. void insertion(long[], long);
  7.  
  8. int main()
  9. {
  10. int sortType = 0;
  11. int arr[100];
  12. int arr2[100];
  13. int numOfInts = 0;
  14. int numOfInts2 = 0;
  15. int loopVar = 0;
  16. int loopVar2 = 0;
  17. int swap = 0;
  18.  
  19. printf("1 for bubble sorting. \n");
  20. printf("2 for insertion sorting. \n");
  21. printf("Choice: ");
  22. scanf("%d", sortType);
  23. switch (sortType)
  24. {
  25. case 1:
  26. printf("Enter number of elements: ");
  27. scanf("%d", &numOfInts);
  28.  
  29. printf("Enter %d integers: \n", numOfInts);
  30.  
  31. for (loopVar = 0; loopVar < numOfInts; loopVar++)
  32. {
  33. scanf("%d", &arr[loopVar]);
  34. }
  35.  
  36. bubble(arr, numOfInts);
  37. break;
  38. case 2:
  39. printf("Enter total number of elements: ");
  40. scanf("%d", &numOfInts2);
  41. for (loopVar2 = 0; loopVar2 < numOfInts2; loopVar2++)
  42. {
  43. printf("Enter %d element: ", (loopVar2 + 1));
  44. scanf("%d", &arr2[loopVar2]);
  45. }
  46. insertion(arr2, numOfInts2);
  47. for (loopVar2 = 0; loopVar2 < numOfInts2; loopVar2++)
  48. {
  49. printf("%d \n", arr2[loopVar2]);
  50. }
  51. default:
  52. break;
  53. }
  54. }
  55. void bubble(long a[], long n)
  56. {
  57. int arr[100], numOfInts, loopVar, d, swap;
  58.  
  59.  
  60. for (loopVar = 0; loopVar < (numOfInts - 1); loopVar++)
  61. {
  62. for (d = 0; d < numOfInts - loopVar - 1; d++)
  63. {
  64. if (arr[d] > arr[d + 1])
  65. {
  66. swap = arr[d];
  67. arr[d] = arr[d + 1];
  68. arr[d + 1] = swap;
  69. }
  70. }
  71. }
  72. printf("Sorted list in ascending order: \n");
  73.  
  74. for (loopVar = 0; loopVar < numOfInts; loopVar++)
  75. {
  76. printf("%d ", arr[loopVar]);
  77. }
  78. }
  79. void insertion(long arr2[], long x)
  80. {
  81. int loopVar2 = 0;
  82. int numOfInts2 = 0;
  83. int j = 0;
  84. int temp = 0;
  85. for (loopVar2 = 0; loopVar2 < numOfInts2; loopVar2++)
  86. {
  87. for (j = loopVar2 - 1; j >= 0; j--)
  88. {
  89. if (arr2[j] > arr2[j + 1])
  90. {
  91. temp = arr2[j];
  92. arr2[j] = arr2[j + 1];
  93. arr2[j + 1] = temp;
  94. }
  95. else {
  96. break;
  97. }
  98. }
  99. }
  100.  
  101. }
Success #stdin #stdout 0s 2160KB
stdin
Standard input is empty
stdout
1 for bubble sorting. 
2 for insertion sorting. 
Choice: