MCS-201 · Unit 4

Arrays and Strings

Section 4 of 9

Block 1 · Unit 4Arrays and Strings8 min · Section 4 of 9

4.4Selection Sort and Passing Arrays to Functions

Covers IGNOU MCS-201 §4.5

Sorted data is easier to search, read and summarise. Selection sort is one of the simplest sorting methods and is the one used in this unit. This section also answers a question the study material raises: what happens when an array is given to a function?

Selection sort

For an array of n elements, selection sort makes n − 1 passes. Pass i scans the elements from position i to the end, so the total number of comparisons is

(n−1)+(n−2)+⋯+1=n(n−1)2(n-1) + (n-2) + \cdots + 1 = \frac{n(n-1)}{2}

which grows like n2n^2, written O(n2)O(n^2). A swap of two elements needs a temporary variable: tmp = a[i]; a[i] = a[min_pos]; a[min_pos] = tmp;.

Passing an array to a function

When an array name is used as a function argument, the function does not receive a copy of the elements. It receives the address of the first element. Through that address, the function works on the caller’s original array, so changes it makes are visible to the caller. The function cannot find the size from the array itself, so the size is passed as a second argument.

Two common applications of arrays (Check Your Progress 2): frequency counts, where count[v] records how many times the value v occurs, and tables of data such as marks lists, matrices and lookup tables for searching and sorting.

Key takeaways

  • Selection sort: for each position, find the smallest remaining element and swap it in.
  • n − 1 passes and n(n − 1)/2 comparisons, so about n² work.
  • Swapping needs a temporary variable.
  • A function receives the address of an array, so it can change the caller’s elements.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

How many passes does selection sort need for 8 elements?

Show answer

Answer: B. 7

n − 1 = 7 passes; after 7 elements are placed, the last one is already in position. 28 is the number of comparisons.

Question 2 · multiple choice

How many comparisons does selection sort make for 6 elements?

Show answer

Answer: D. 15

n(n − 1)/2 = 6 × 5 ÷ 2 = 15, that is 5 + 4 + 3 + 2 + 1.

Question 3 · multiple choice

After the first pass of ascending selection sort on {34, 12, 5, 27}, what is the array?

Show answer

Answer: C. 5 12 34 27

The smallest value, 5, is swapped with the first element, 34, giving 5 12 34 27. The fully sorted array appears only after all passes.

Question 4 · multiple choice

What does this program print?

C · swap_ends.c
#include <stdio.h>

int main(void)
{
    int a[4] = {1, 2, 3, 4};
    int tmp = a[0];
    a[0] = a[3];
    a[3] = tmp;
    printf("%d %d %d %d\n", a[0], a[1], a[2], a[3]);
    return 0;
}
Show answer

Answer: D. 4 2 3 1

Only the first and last elements are swapped using tmp. Without the temporary variable, the old value of a[0] would be lost, giving 4 2 3 4.

Question 5 · multiple choice

When marks is passed to a function as f(marks, n), what does the function receive?

Show answer

Answer: B. The address of the first element

The array name is converted to the address of element 0, which is why the function can modify the original array.

Question 6 · short answer

Show the array after each pass when {29, 10, 14, 37, 13} is sorted in ascending order by selection sort.

Show worked answer

Pass 1: the smallest of all five is 10; swap with 29 → 10 29 14 37 13. Pass 2: the smallest of 29 14 37 13 is 13; swap with 29 → 10 13 14 37 29. Pass 3: the smallest of 14 37 29 is 14, already in place → 10 13 14 37 29. Pass 4: the smallest of 37 29 is 29; swap with 37 → 10 13 14 29 37. The array is sorted after 4 passes.

↑ ↓ move↵ openTab switch type