Skip to content

Task 2: Sorted Student Directory

Difficulty: Moderate

Problem to Solve

The first student lookup used linear search, which works on records in any order but might inspect every student. In this continuation, you will prepare the directory for a faster search.

Create a file named student_directory.c inside a folder named sorted-student-directory. First, sort an array of student structures by ID. Then ask for an ID and find the matching record with binary search.

Start with these deliberately unsorted records:

ID Name Major
108 Kamol Physics
103 Madina Economics
105 Zebo Computer Science
101 Amina Computer Science
107 Nilufar Biology
102 Farid Mathematics
106 Rustam Design
104 Daler Engineering

Requirements

  • Define a structure named Student containing an ID, name, and major.
  • Create and initialize an array containing the eight records shown above.
  • Sort the records from smallest ID to largest ID using selection sort.
  • When swapping, move the complete Student record so its fields stay together.
  • Print the sorted directory as ID - Name, one student per line.
  • Prompt with Student ID: and read an integer ID.
  • Search the sorted array using binary search, not linear search.
  • Count how many array elements the binary search examines.
  • If the student exists, print the name, major, and comparison count.
  • If the student does not exist, print Student not found. and the comparison count.
  • Keep all program logic inside main; do not create additional functions.
  • Use only standard C libraries.

Sort before searching

Binary search is correct only when the records are sorted by the same field being searched. In this task, sort and search using id.

Expected Output

For an existing ID:

$ ./student_directory
Sorted directory:
101 - Amina
102 - Farid
103 - Madina
104 - Daler
105 - Zebo
106 - Rustam
107 - Nilufar
108 - Kamol

Student ID: 106
Name: Rustam
Major: Design
Comparisons: 2

For a missing ID, the final lines should resemble:

Student ID: 999
Student not found.
Comparisons: 4

Starter Code

#include <stdio.h>

typedef struct
{
    int id;
    char name[50];
    char major[50];
} Student;

int main(void)
{
    Student students[] = {
        {108, "Kamol", "Physics"},
        {103, "Madina", "Economics"},
        {105, "Zebo", "Computer Science"},
        {101, "Amina", "Computer Science"},
        {107, "Nilufar", "Biology"},
        {102, "Farid", "Mathematics"},
        {106, "Rustam", "Design"},
        {104, "Daler", "Engineering"}
    };

    int student_count = sizeof students / sizeof students[0];

    // Sort the complete records by ID using selection sort.

    // Print the sorted directory.

    // Read the target ID.

    // Search for the ID using binary search.

    // Print the result and number of comparisons.
}
Selection sort hint

For every position i, find the index of the smallest ID from i through the end of the array. Compare IDs with:

students[j].id < students[smallest].id

Then swap complete structures:

Student temporary = students[i];
students[i] = students[smallest];
students[smallest] = temporary;
Binary search boundaries

Begin with:

int low = 0;
int high = student_count - 1;
int found_index = -1;
int comparisons = 0;

Continue while low <= high. Calculate the middle index with:

int middle = low + (high - low) / 2;

Increment comparisons whenever you inspect students[middle].

Choosing the remaining half

Compare the target ID with students[middle].id:

  • If they are equal, save middle and stop.
  • If the target is smaller, set high to middle - 1.
  • If the target is larger, set low to middle + 1.

Do not reset low or high inside the loop.

Debugging hint

Temporarily print low, middle, high, and students[middle].id during every iteration. This makes it easier to see which half is discarded. Remove the diagnostic output before submitting.

Compile and Test

From the sorted-student-directory folder, run:

cc student_directory.c -o student_directory
./student_directory

If your system provides gcc instead of cc, run gcc student_directory.c -o student_directory.

Test at least:

  • the first ID, 101;
  • a middle ID such as 104 or 105;
  • the last ID, 108;
  • an ID smaller than every record, such as 1;
  • the next unused ID, 109; and
  • an ID much larger than every record, such as 999.

Confirm that the directory is sorted correctly before checking the search results.

Continue with Task 3: Class Gradebook.