---
title: "Sort array using a function"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:array-sort-function"
source: https://www.bctengineer.com/q/semester_1/computer_programming/array-sort-function
site: BCT Engineering
---

# Sort array using a function

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2068 Shrawan, 3+5 marks
- Video: [C Program to Sort Array Elements in Ascending & Descending Order | Learn Coding](https://www.youtube.com/watch?v=D53l7rRPlSs) — Learn Coding
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/array-sort-function
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Write a program to take a list of values from user into an array. Pass the list to a function which sorts the values in ascending order. Display the sorted list from main program.

## Short answer

Read values into array in main(), pass array to sortAsc() which uses bubble sort (adjacent comparison and swap) for ascending order. Since array is passed by reference, sorted array is directly available in main() for display.

## Answer

#### Method
The values are read into an array in `main()`. The array name is passed to `sortAsc()`, which sorts it in place with bubble sort: adjacent elements are compared and swapped if the left one is bigger, and after each pass the largest of the remaining elements moves to the end. Because arrays are passed by address, the sorted array is directly available in `main()`, which displays it.

```c
#include <stdio.h>

void sortAsc(int a[], int n)
{
    for (int i = 0; i < n - 1; i++)
        for (int j = 0; j < n - 1 - i; j++)
            if (a[j] > a[j + 1]) {
                int t = a[j];
                a[j] = a[j + 1];
                a[j + 1] = t;
            }
}

int main(void)
{
    int a[100], n;
    printf("How many values? ");
    scanf("%d", &n);
    printf("Enter the values: ");
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    sortAsc(a, n);                 /* array is changed in place */
    printf("Sorted list: ");
    for (int i = 0; i < n; i++)
        printf("%d ", a[i]);
    printf("\n");
    return 0;
}
```

**Sample run**

Input: `6 42 7 19 3 88 25`

```text
How many values? Enter the values: Sorted list: 3 7 19 25 42 88
```

## Check yourself

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

1. What does each pass of bubble sort do?
   A. Finds the smallest and puts it first without comparing neighbours
   B. Compares adjacent elements and swaps if the left is bigger, moving the largest to the end
   C. Reverses the array
   D. Splits the array into halves
   Answer: B. After each pass the largest remaining element settles at the end. (Key point: Bubble sort idea)

2. Why does main() see the sorted array without a return?
   A. A copy is returned automatically
   B. The array is global
   C. The function prints it back
   D. The array is passed by address and sorted in place
   Answer: D. Changes made through the address affect the original array. (Key point: In-place sorting)

3. What is needed to swap a[j] and a[j+1] safely?
   A. Nothing, a[j] = a[j+1] is enough
   B. A temporary variable
   C. A second array only
   D. A pointer to a[0]
   Answer: B. Without temp, one value would be overwritten. (Key point: Swap)

## Related in Computer Programming

- [Sort array ascending in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-sort-ascending) — Computer Programming, chapter 11, asked 2×
- [Sort employee structures by salary](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-employee-sort-salary) — Computer Programming, chapter 8, asked 2×
- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7
- [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
- [Pass array; transpose 3×3 matrix](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-array-transpose-3x3) — Computer Programming, chapter 7
- [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

## Related in other subjects

- [Array class template with max, min, sort](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-template-array-max-min-sort) — Object Oriented Programming, chapter 9
- [Function template: sum and average of array](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-template-sum-average) — Object Oriented Programming, chapter 9
- [Band pass (1500 rad/s, 300 rad/s) from 4th order ladder](https://www.bctengineer.com/question.md?key=semester_5%2Ffilter_design%3Abandpass-1500-300-fourth-order) — Filter Design, chapter 3, asked 4×
- [Namespace with pass by reference program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Anamespace-pass-by-ref-program) — Object Oriented Programming, chapter 2
