Sorting Courses with Bubble Sort

CCBeginner
Practice Now

Introduction

In this project, you will learn how to organize a list of programming courses using the bubble sort algorithm. The courses will be sorted in lexicographical order, allowing you to easily see and summarize the programming languages you have learned.

👀 Preview

$ gcc courses.c -o courses
$ ./courses
ASP
ASP.NET
BASIC
C
C## C++
COBOL
JAVA
PASCAL
PHP

ðŸŽŊ Tasks

In this project, you will learn:

  • How to correctly calculate the length of an array
  • How to modify the parameter type of a sorting function
  • How to correct the comparison and swapping logic in the sorting algorithm

🏆 Achievements

After completing this project, you will be able to:

  • Organize a list of programming courses using the bubble sort algorithm
  • Understand the importance of proper array length calculation and function parameter types
  • Apply the correct comparison and swapping logic in a sorting algorithm
  • Demonstrate your ability to follow step-by-step instructions and modify existing code to achieve the desired outcome

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/CompoundTypesGroup(["`Compound Types`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/CompoundTypesGroup -.-> c/arrays("`Arrays`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") subgraph Lab Skills c/if_else -.-> lab-301503{{"`Sorting Courses with Bubble Sort`"}} c/for_loop -.-> lab-301503{{"`Sorting Courses with Bubble Sort`"}} c/arrays -.-> lab-301503{{"`Sorting Courses with Bubble Sort`"}} c/pointers -.-> lab-301503{{"`Sorting Courses with Bubble Sort`"}} end

Correct the Array Length Calculation

In this step, you will learn how to correctly calculate the length of the courses array.

  1. Open the courses.c file located in the /home/labex/project/ directory.

  2. Find the following line of code:

    int n = strlen(courses);

    This line uses the strlen function to calculate the length of the courses array, which is incorrect. The strlen function is used to calculate the length of a string, not an array.

  3. Replace the above line with the following:

    int n = sizeof(courses) / sizeof(courses[0]);

    This line uses the sizeof operator to calculate the total size of the courses array, and then divides it by the size of a single element to get the number of elements in the array.

Change the Parameter Type of the Sorting Function

In this step, you will learn how to change the parameter type of the sort function.

  1. In the courses.c file, find the sort function definition:

    void sort(char p[], int n)
  2. Change the parameter type from char p[] to char *p[]. The updated function definition should look like this:

    void sort(char *p[], int n)

    This change is necessary because we are dealing with an array of strings (an array of pointers), not a single string.

Correct the Comparison and Swapping Logic

In this step, you will learn how to correct the comparison and swapping logic in the sort function.

  1. In the sort function, find the following code:

    if (k != j)
    {
        tmp = p[k];
        p[k] = p[i];
        p[i] = tmp;
    }
  2. Replace the condition if (k != j) with if (k != i). The updated code should look like this:

    if (k != i)
    {
        tmp = p[k];
        p[k] = p[i];
        p[i] = tmp;
    }

    This change is necessary to check if the smallest element has been found in the current iteration (indexed by i).

After completing these three steps, your courses.c file should be ready for compilation and execution. You can now proceed to the next step.

  1. In the sort function, complete the // TODO: section as follows:

    {
         if (strcmp(p[j], p[k]) < 0)
         {
             k = j;
         }
    }
  2. Execute the following command:

gcc courses.c -o courses
./courses
  1. Verify that the program produces the expected output.
ASP
ASP.NET
BASIC
C
C## C++
COBOL
JAVA
PASCAL
PHP
âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other C Tutorials you may like