Laboratory Overview
What is new in this laboratory.
In this laboratory, you will move from file processing to a new family of problems: sorting. Instead of solving one isolated exercise, you will implement two complete sorting algorithms, test them on different kinds of arrays, and then add them to your existing array library.
algorithm β test on static array β test on dynamic array β library function
The selected data type for this laboratory is long. This keeps the comparison logic simple,
so that the main focus remains on the algorithms themselves and on writing clean reusable functions.
Learning Outcomes
- explain the basic idea of bubble sort and insertion sort,
- implement sorting functions for arrays of
long, - test sorting algorithms on static arrays,
- test sorting algorithms on dynamic arrays allocated with
mallocorcalloc, - reuse previously written library functions such as
randomize_l, - extend
array.handlibarray.awith new functions, - print arrays before and after sorting and verify correctness experimentally.
How this laboratory continues Lab 7
You will reuse the array module created in Laboratory 7 and extend it with sorting functions.
| Part from Lab 7 | Used in Lab 8 for |
|---|---|
array.h |
Adding declarations of bsort_l and isort_l. |
print_l |
Printing arrays before and after sorting. |
randomize_l |
Generating test data for dynamic arrays. |
libarray.a |
Rebuilding the library with new sorting object files. |
ex25.c. This laboratory continues the numbering with
ex26βex29.
Task 0 β Reminder and preparation
Step 1 β Log in to the AGH UNIX server
ssh your_login@student.agh.edu.pl
Step 2 β Go to the course directory
cd ~/I2PL
Step 3 β Create the directory for Laboratory 8
mkdir lab8
cd lab8
Step 4 β General rules
- Work only in
~/I2PL/lab8. - Do not use global variables.
- Keep all function declarations in
array.h. - Store each sorting function in a separate source file.
- Compile with warnings enabled.
gcc -Wall program.c -o program
Task 1 β Read about the selected algorithms
Step 1 β Bubble sort
Bubble sort repeatedly compares neighbouring elements and swaps them if they are in the wrong order. Larger values βbubbleβ toward the end of the array.
Step 2 β Insertion sort
Insertion sort grows a sorted prefix of the array. In each step, one element is inserted into the correct position among the already sorted values.
Task 2 β Extend array.h with sorting declarations
Step 1 β Open the header file
nano array.h
Step 2 β Add declarations
void bsort_l(long tab[], int size);
void isort_l(long tab[], int size);
_l to make it clear that these functions sort arrays of type long.
Task 3 β Implement bubble sort for long
Step 1 β Create the file
nano bsort_l.c
Step 2 β Include the header
#include "array.h"
Step 3 β Implement bubble sort
A good starting point is the following structure:
void bsort_l(long tab[], int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size - 1; j++) {
if (tab[j] > tab[j + 1]) {
long tmp = tab[j];
tab[j] = tab[j + 1];
tab[j + 1] = tmp;
}
}
}
}
long for the temporary variable as well. Do not accidentally reduce the type to int.
Task 4 β Implement insertion sort for long
Step 1 β Create the file
nano isort_l.c
Step 2 β Implement insertion sort
void isort_l(long tab[], int size) {
for (int i = 1; i < size; i++) {
long value = tab[i];
int j = i - 1;
while (j >= 0 && tab[j] > value) {
tab[j + 1] = tab[j];
j--;
}
tab[j + 1] = value;
}
}
Task 5 β Test sorting on a static array
Step 1 β Create the first test program
nano ex26_bubble_long.c
Step 2 β Define a static array
long array[] = { 12, 10, -12, 3, 4, 3, 1, -100, -12 };
Step 3 β Compute its size with sizeof
int size = sizeof(array) / sizeof(long);
Step 4 β Print before and after sorting
Display the whole array before sorting, call bsort_l, then display the sorted array.
Step 5 β Create the second test program
nano ex27_insertion_long.c
Repeat the same idea, but use isort_l.
Task 6 β Test sorting on a dynamic array
Step 1 β Create the first dynamic test
nano ex28_bubble_long.c
Step 2 β Ask the user for the size
int size;
printf("Enter array size: ");
scanf("%d", &size);
Step 3 β Allocate memory
long *array = malloc(size * sizeof(long));
or:
long *array = calloc(size, sizeof(long));
Step 4 β Fill the array with random values
Reuse the randomize function from your library.
randomize_l(array, size, -100, 100);
Step 5 β Print, sort, print again
Display the array before sorting and after sorting.
Step 6 β Free memory correctly
free(array);
array = NULL;
Step 7 β Prepare the second dynamic test
nano ex29_insertion_long.c
Repeat the same idea, but use isort_l.
free, set the pointer to NULL.
Task 7 β Print arrays in a reusable way
Step 1 β Write a helper function
void print_long_array(long tab[], int size) {
for (int i = 0; i < size; i++) {
printf("%ld ", tab[i]);
}
printf("\n");
}
Task 8 β Compile the object files and update the library
Step 1 β Compile the sorting functions
gcc -Wall -c bsort_l.c
gcc -Wall -c isort_l.c
Step 2 β Update the library archive
ar rcs libarray.a bsort_l.o isort_l.o
Step 3 β Compile a testing program with the library
gcc -Wall ex26_bubble_long.c -L. -larray -o ex26_bubble_long
-L and -l options used by gcc.
Task 9 β Compare the algorithms experimentally
Step 1 β Use the same input values
For static arrays, this is automatic. For dynamic arrays, use the same random range and similar sizes.
Step 2 β Check correctness first
The first goal is not speed but correctness. Make sure both programs always produce sorted output.
Step 3 β Reflect on the difference
- Which algorithm was easier to implement?
- Which one is easier to explain?
- Which one seems to perform fewer unnecessary moves?
Checklist before submission
array.hcontains declarations ofbsort_landisort_l,bsort_l.candisort_l.ccompile without warnings,- you have two test programs for static arrays,
- you have two test programs for dynamic arrays,
- each test displays the array before and after sorting,
- dynamic memory is released correctly,
libarray.ahas been updated with the new object files.