---
title: "Sort employee structures by salary"
subject: "Computer Programming"
chapter: 8
key: "semester_1/computer_programming:struct-employee-sort-salary"
source: https://www.bctengineer.com/q/semester_1/computer_programming/struct-employee-sort-salary
site: BCT Engineering
---

# Sort employee structures by salary

Computer Programming · Chapter 8: Structures

- Asked in: 2073 Chaitra, 8 marks; 2069 Chaitra, 6 marks (asked 2 times)
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/struct-employee-sort-salary
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-8

## Question

Write a program in C to read name, age and salary of 10 different employees as the three members of a structure named as "employee". Sort this data in salary basis using user defined function and display sorted data from main function. (Variant: create an array of structure Employee with name and salary as members; pass it to a function which sorts in ascending order on the basis of salary and display the sorted array from main().)

## Short answer

Read 10 employee records (name, age, salary) into an array of struct employee; sort by salary ascending using bubble sort (swap whole structures), then display. Sample sorted output: Hari 41 38000.00, Maya 33 39500.00, … Anita 38 70000.00.

## Answer

**Method.** A structure `employee` with `name`, `age` and `salary` is declared and an array of 10 such structures is read in `main()`. The array is passed to `sortBySalary()`, which sorts it in ascending order of `salary` using bubble sort; whole structures are swapped with a temporary structure variable. The sorted data is displayed from `main()`.

(For the variant with only name and salary, remove `age`; the same function works unchanged.)

```c
#include <stdio.h>
#define N 10

struct employee {
    char name[30];
    int age;
    float salary;
};

void sortBySalary(struct employee e[], int n)
{
    struct employee t;
    for (int i = 0; i < n - 1; i++)
        for (int j = 0; j < n - 1 - i; j++)
            if (e[j].salary > e[j + 1].salary) {
                t = e[j];
                e[j] = e[j + 1];
                e[j + 1] = t;
            }
}

int main(void)
{
    struct employee e[N];
    for (int i = 0; i < N; i++) {
        printf("Employee %d (name age salary): ", i + 1);
        scanf("%29s %d %f", e[i].name, &e[i].age, &e[i].salary);
    }
    sortBySalary(e, N);
    printf("\n%-12s %-5s %s\n", "Name", "Age", "Salary");
    for (int i = 0; i < N; i++)
        printf("%-12s %-5d %.2f\n", e[i].name, e[i].age, e[i].salary);
    return 0;
}
```

**Sample run**

Input (entered one after another): `Ram 30 45000 Sita 25 52000 Hari 41 38000 Gita 29 61000 Bibek 35 47500 Maya 33 39500 Kiran 27 43000 Anita 38 70000 Suman 45 55000 Rita 31 41000`

```text
Name         Age   Salary
Hari         41    38000.00
Maya         33    39500.00
Rita         31    41000.00
Kiran        27    43000.00
Ram          30    45000.00
Bibek        35    47500.00
Sita         25    52000.00
Suman        45    55000.00
Gita         29    61000.00
Anita        38    70000.00
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Which sorting method does sortBySalary() use?
   A. Bubble sort on salary in ascending order
   B. Binary search
   C. Selection by name
   D. Bubble sort in descending order
   Answer: A. It swaps adjacent records when e[j].salary > e[j+1].salary. (Key point: Sort type)

2. How are two employees swapped?
   A. Only the salary is swapped
   B. The whole structures are swapped using a temporary structure variable
   C. Name is swapped but age is left
   D. They cannot be swapped in C
   Answer: B. t = e[j]; e[j] = e[j+1]; e[j+1] = t; keeps name, age and salary together. (Key point: Swapping)

3. Why must the whole structure and not only salary be swapped?
   A. Because salary is a string
   B. So each name and age stay with their own salary
   C. To save memory
   D. Because C forbids swapping floats
   Answer: B. Swapping only salary would mismatch the records. (Key point: Why whole record)

4. Why does the sorted order appear in main() after the call?
   A. The function returns a new array
   B. The array is copied by value
   C. An array is passed by address, so the function sorts the original array
   D. main() sorts it again
   Answer: C. Array arguments refer to the original data. (Key point: Passing array)

5. Why is the inner loop condition j < n - 1 - i?
   A. The last i elements are already in place after i passes
   B. To skip the first element
   C. To avoid comparing equal salaries
   D. Because arrays are 1-indexed
   Answer: A. Each bubble-sort pass puts the largest remaining value at the end. (Key point: Inner loop limit)

## Related in Computer Programming

- [Employees with salary above 10,000](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-employee-salary-above-10000) — Computer Programming, chapter 8
- [Display records sorted by name from file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-sorted-records-by-name) — Computer Programming, chapter 10
- [Employee text file; sort into binary file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-employee-sorted-binary) — Computer Programming, chapter 10
- [Read records until end; show nth record](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-worker-nth-record) — Computer Programming, chapter 10, asked 2×
- [Switch versus user defined function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aswitch-vs-user-function) — Computer Programming, chapter 5
- [Sending structure members to a function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astructure-members-to-function) — Computer Programming, chapter 8

## Related in other subjects

- [Write and display 10 employees using file](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aemployee-file-program) — Object Oriented Programming, chapter 8
- [Convert user-defined type to basic type](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Auser-to-basic-conversion) — Object Oriented Programming, chapter 5
- [Manager derived from person and employee](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amanager-person-employee) — Object Oriented Programming, chapter 6
- [RA: employees on project DBMS](https://www.bctengineer.com/question.md?key=semester_6%2Fdatabase_management_system%3Ara-emp-project-dbms) — Database Management System, chapter 3
