BubbleSort

The raw code refers to: UNSW COMP9024

#include <stdio.h>

/*
    Swap the values of the two integer variables pointed to by pa and pb, respectively.  
 */
void Swap(int *pa, int *pb) {
    int tmp = *pa;
    *pa = *pb;
    *pb = tmp;
}

/*
  Why Swap2(int a, int b) doesn't work?

    Only the values of the two parameters a and b are swapped.

 */
void Swap2(int a, int b) {
    printf("a = %d, b = %d\n", a, b);
    int tmp = a;
    a = b;
    b = tmp;
    printf("a = %d, b = %d\n", a, b);
}

/*
    void PrintArray(int *ptr, int n);

        Print the values of the n integer variables pointed to by an pointer ptr:

            ptr[0],  ptr[1],    ptr[2],    ...,  ptr[n-1]

        or 
            *ptr,   *(ptr+1),  *(ptr+2),   ...,  *(ptr + n -1)

 */
void PrintArray(int *ptr, int n) {
    for (int i = 0; i < n; i++) {
        printf("%d ", ptr[i]);
    }
    printf("\n");
}

/*
    Bubble sort the n numbers pointed to by ptr.
    The elements are sorted in ascending order (from the least to the greatest).
 */
void BubbleSort(int *ptr, int n) {
    /*
        1. iMax represents the max value of i in a pass
              (in the following "if (ptr[i] > ptr[i+1])")

        2. (n-1) passes needed in Bubble Sort
     */
    for (int iMax = n - 2; iMax >= 0; iMax--) { // (n-1) passes

        // printf() is quite useful in observing the behavior of a program
        printf("............... iMax = %d ...............\n\n", iMax);
        PrintArray(ptr, iMax + 2);
        printf("\n\n");

        // i is in [0, iMax] in the current pass
        for (int i = 0; i <= iMax; i++) {
            if (ptr[i] > ptr[i+1]) {
                Swap(ptr + i, ptr + i + 1);

                printf("After swapping ptr[%d] and ptr[%d]:\n", i, i+1);
                PrintArray(ptr, iMax + 2);
                printf("\n");
            }
        }       
    }
}

#if 1
int IsLess(int a, int b) {
    return a < b;
}

int IsLarger(int a, int b) {
    return a > b;
}

// Define a function pointer type, which points to a function
typedef int (*ComparatorFuncPtr)(int, int);


void BubbleSort2(int *ptr, int n, ComparatorFuncPtr compare) {
    for (int iMax = n - 2; iMax >= 0; iMax--) {
        // for (int i = 0; ____Q1_____; _____Q2____) {
          for (int i = 0; i<=iMax; i++) {
        //     if (_____Q3_____) {
          if (compare(ptr[i], ptr[i+1])) {
        //         ____Q4____;
              swap(ptr+i, ptr+i+1);
              // swap(&ptr[i], &ptr[i+1]); is also OK
            }
        }       
    }
}

int TestBubbleSort2(void) {    
    int arr[] = {30, 50, 20, 10, 60, 40};
    int len = sizeof(arr) / sizeof(arr[0]);

    // a function pointer variable which points to the function IsLarger()
    ComparatorFuncPtr fptr = &IsLarger;    
    printf("Before sorting:\n");
    PrintArray(arr, len);
    BubbleSort2(arr, len, fptr);
    // in ascending order
    printf("After sorting:\n");
    PrintArray(arr, len);

    // a function pointer variable which points to the function IsLess()
    // fptr = ____Q5____;    

    fptr=IsLess; // Both `IsLess` and `&IsLess` is OK, they are all function pointer. The function pointer declarification:  typedef int (*ComparatorFuncPtr)(int, int);

    printf("\nBefore sorting:\n");
    PrintArray(arr, len);
    BubbleSort2(arr, len, fptr);
    // in descending order
    printf("After sorting:\n");
    PrintArray(arr, len);
    return 0;
}
#endif


int main(void) {    
    // Let the C compiler determine the number of array elements for us.
    int arr[] = {30, 50, 20, 10, 60, 40};
    // calculate the number of elements
    int len = sizeof(arr) / sizeof(arr[0]);

    // int t1 = sizeof(arr);  // print 24
    // int t2 = sizeof(arr[0]);  // print 4
    // int t3 = sizeof(arr+1);  // print 8
    // arr is an array; arr[0] is a value; 
    // sizeof(arr) means the size of the array
    // sizeof(arr+1) means the size of the address (arr+1), in 64-bit OS, it's always 8 Bytes.
    // refer to my blog: https://senranja.github.io/2024/10/30/Cpp/Declaring-arrays-and-out-of-bounds/
    // they have nusances when use sizeof() function.

    printf("Before sorting:\n");
    PrintArray(arr, len);
    BubbleSort(arr, len);
    printf("After sorting:\n");
    PrintArray(arr, len);

    printf("\n\nWhy Swap2(i, j) doesn't work?\n\n");
    int i = 20, j = 24;
    printf("i = %d, j = %d\n\n", i, j);
    printf("\nSwap2(i, j)\n");
    // The values of i and j are passed.
    Swap2(i, j);
    printf("i = %d, j = %d\n\n", i, j);

    printf("After completing the code in Q1-Q5 (BubbleSort.c), please also uncomment line %d in %s:\n\n", (__LINE__ + 1), __FILE__);
    TestBubbleSort2();
    return 0;
}

Output:

Before sorting:
30 50 20 10 60 40 
............... iMax = 4 ...............

30 50 20 10 60 40 


After swapping ptr[1] and ptr[2]:
30 20 50 10 60 40 

After swapping ptr[2] and ptr[3]:
30 20 10 50 60 40 

After swapping ptr[4] and ptr[5]:
30 20 10 50 40 60 

............... iMax = 3 ...............

30 20 10 50 40 


After swapping ptr[0] and ptr[1]:
20 30 10 50 40 

After swapping ptr[1] and ptr[2]:
20 10 30 50 40 

After swapping ptr[3] and ptr[4]:
20 10 30 40 50 

............... iMax = 2 ...............

20 10 30 40 


After swapping ptr[0] and ptr[1]:
10 20 30 40 

............... iMax = 1 ...............

10 20 30 


............... iMax = 0 ...............

10 20 


After sorting:
10 20 30 40 50 60 


Why Swap2(i, j) doesn't work?
z5541664@vx17:~/COMP9024/Tutorials/Week2$ ls
build  diagrams  images  main  Makefile  README.md  src
z5541664@vx17:~/COMP9024/Tutorials/Week2$ ./main 
Before sorting:
30 50 20 10 60 40 
............... iMax = 4 ...............

30 50 20 10 60 40 


After swapping ptr[1] and ptr[2]:
30 20 50 10 60 40 

After swapping ptr[2] and ptr[3]:
30 20 10 50 60 40 

After swapping ptr[4] and ptr[5]:
30 20 10 50 40 60 

............... iMax = 3 ...............

30 20 10 50 40 


After swapping ptr[0] and ptr[1]:
20 30 10 50 40 

After swapping ptr[1] and ptr[2]:
20 10 30 50 40 

After swapping ptr[3] and ptr[4]:
20 10 30 40 50 

............... iMax = 2 ...............

20 10 30 40 


After swapping ptr[0] and ptr[1]:
10 20 30 40 

............... iMax = 1 ...............

10 20 30 


............... iMax = 0 ...............

10 20 


After sorting:
10 20 30 40 50 60 


Why Swap2(i, j) doesn't work?

i = 20, j = 24


Swap2(i, j)
a = 20, b = 24
a = 24, b = 20
i = 20, j = 24

After completing the code in Q1-Q5 (BubbleSort.c), please also uncomment line 156 in src/BubbleSort.c:

Before sorting:
30 50 20 10 60 40 90 60 120 150 35 
After sorting:
10 20 30 35 40 50 60 60 90 120 150

Welcome to point out the mistakes and faults!