Guest User

Untitled

a guest
May 26th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctime>
  4. #include <iostream>
  5.  
  6. void PrintA(int A[], int N);
  7. void Swap(int *a, int *b);
  8. void InsertionSort(int A[], int N);
  9.  
  10. int main(void)
  11. {
  12. const int N = 10;
  13. int A[N];
  14.  
  15. for (int i = 0; i < N; i++)
  16. {
  17. A[i] = 1 + rand() % 9;
  18. }
  19.  
  20. printf("Array under insertion sort: n");
  21. PrintA(A, N);
  22.  
  23. InsertionSort(A, N);
  24.  
  25. /*printf("Array after insertion sort: n");
  26. PrintA(A, N);*/
  27.  
  28. system("pause");
  29. }
  30.  
  31. void PrintA(int A[], int N)
  32. {
  33. for (int i = 0; i < N; i++)
  34. {
  35. printf("%d ", A[i]);
  36. }
  37. printf("n");
  38. }
  39.  
  40. void Swap(int *a, int *b)
  41. {
  42. int temp = *a;
  43. *a = *b;
  44. *b = temp;
  45. }
  46.  
  47. void InsertionSort(int A[], int N)
  48. {
  49. int j;
  50.  
  51. for (int i = 1; i < N; i++)
  52. {
  53. j = i;
  54. while (j > 0 && A[j - 1] > A[j])
  55. {
  56. Swap(&A[j - 1], &A[j]);
  57. j--;
  58. }
  59. }
  60. PrintA(A, N);
  61. }
  62.  
  63. void InsertionSort(int A[], int N)
  64. {
  65. // Вот здесь:
  66. int template[A.Length];
  67. for(int i = 0;i < A.Length;i++)
  68. {
  69. template[i] = A[i];
  70. }
  71. int j;
  72.  
  73. for (int i = 1; i < N; i++)
  74. {
  75. j = i;
  76. while (j > 0 && A[j - 1] > A[j])
  77. {
  78. Swap(&A[j - 1], &A[j]);
  79. j--;
  80. }
  81. }
  82. //Передаем наш массив
  83. PrintA(template, N);
  84. }
  85.  
  86. int main(void)
  87. {
  88. const int N = 10;
  89. int A[N], B[N];
  90.  
  91. ....код наполняющий массив A
  92.  
  93. memcpy((void*)B, (void*)A, (sizeof(int) * N));
  94. PrintA(B, N);
  95.  
  96. typedef struct Massiv_ {
  97. int A[100];
  98. } Massiv;
  99.  
  100. void Sort(Massiv M, int N) {
  101. // Сортируем M.A - тот массив, что передан, тронут не будет
  102. }
Add Comment
Please, Sign In to add comment