_TruELecter_

Untitled

Oct 28th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <assert.h>
  3. #define N 5
  4.  
  5. void bubblesort(int* a, int n)
  6. {
  7. int j, nn;
  8.  
  9. do {
  10. nn = 0;
  11. for (j = 1; j < n; ++j)
  12. if (a[j-1] > a[j]) {
  13. int t = a[j-1]; a[j-1] = a[j]; a[j] = t;
  14. nn = j;
  15. }
  16. n = nn;
  17. } while (n);
  18. }
  19.  
  20. int* copyarr(int * src, size_t len)
  21. {
  22. int * p = malloc(len * sizeof(int));
  23. memcpy(p, src, len * sizeof(int));
  24. return p;
  25. }
  26.  
  27. int issorted(int* a, int n)
  28. {
  29. if (n < 2)
  30. {
  31. return 1;
  32. }
  33.  
  34. for (int i = 0; i < n - 1; i++)
  35. {
  36. if (a[i] <= a[i - 1]) {
  37. return 0;
  38. }
  39. }
  40.  
  41. return 1;
  42. }
  43.  
  44. int sameelements(int arr1[], int arr2[], int size) {
  45. for (int i = 0; i < size; i++)
  46. {
  47. if (arr1[i] != arr2[i])
  48. return 0;
  49. }
  50. return 1;
  51. }
  52.  
  53. int cmpfunc (const void * a, const void * b) {
  54. return ( *(int*)a - *(int*)b );
  55. }
  56.  
  57.  
  58. int main()
  59. {
  60. srand(time(NULL));
  61. int a[N];
  62. for (int i = 0; i < N; i++)
  63. {
  64. a[i] = rand(NULL);
  65. }
  66. int* c = copyarr(a, N);
  67. qsort(c, N, sizeof(int), cmpfunc);
  68. bubblesort(a, N);
  69.  
  70. for (int i = 0; i < 5; i++)
  71. {
  72. printf("%d ", a[i]);
  73. }
  74.  
  75. assert(issorted(a, N));
  76. assert(sameelements(a, c, N));
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment