Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <assert.h>
- #define N 5
- void bubblesort(int* a, int n)
- {
- int j, nn;
- do {
- nn = 0;
- for (j = 1; j < n; ++j)
- if (a[j-1] > a[j]) {
- int t = a[j-1]; a[j-1] = a[j]; a[j] = t;
- nn = j;
- }
- n = nn;
- } while (n);
- }
- int* copyarr(int * src, size_t len)
- {
- int * p = malloc(len * sizeof(int));
- memcpy(p, src, len * sizeof(int));
- return p;
- }
- int issorted(int* a, int n)
- {
- if (n < 2)
- {
- return 1;
- }
- for (int i = 0; i < n - 1; i++)
- {
- if (a[i] <= a[i - 1]) {
- return 0;
- }
- }
- return 1;
- }
- int sameelements(int arr1[], int arr2[], int size) {
- for (int i = 0; i < size; i++)
- {
- if (arr1[i] != arr2[i])
- return 0;
- }
- return 1;
- }
- int cmpfunc (const void * a, const void * b) {
- return ( *(int*)a - *(int*)b );
- }
- int main()
- {
- srand(time(NULL));
- int a[N];
- for (int i = 0; i < N; i++)
- {
- a[i] = rand(NULL);
- }
- int* c = copyarr(a, N);
- qsort(c, N, sizeof(int), cmpfunc);
- bubblesort(a, N);
- for (int i = 0; i < 5; i++)
- {
- printf("%d ", a[i]);
- }
- assert(issorted(a, N));
- assert(sameelements(a, c, N));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment