Exercise 7.4 – Selection sort of an array
Learning Objectives
By the end of the exercise, the student will:
- explain the concept of selection sort: in each step, select the minimum element of the remaining array and place it in its position
- implement ascending and descending sorting using two nested loops
- utilize a sorted array (minimum, maximum, median, number of distinct elements)
- correctly swap elements in an array
Quick Reference
- Selection sort: for each position
ifrom 0 ton - 2, find the index of the minimum element amonga[i..n-1]and swap it witha[i]. - The outer loop selects the position, the inner loop finds the index of the minimum:
int m = i; for (int j = i + 1; j < n; j++) if (a[j] < a[m]) m = j;. - Swap:
int t = a[i]; a[i] = a[m]; a[m] = t;. - For descending order, the condition in the inner loop changes to
a[j] > a[m]. - After sorting, the minimum element is
a[0], and the maximum isa[n - 1]. The median is the middle element of the sorted array: for oddnit isa[n / 2], for evennit is the arithmetic mean ofa[n/2 - 1]anda[n/2]. - In a sorted array, equal elements are adjacent: number of distinct elements = 1 + number of positions where
a[i] != a[i - 1].
Example Input and Output
| Input (keyboard) | Output (screen) |
|---|---|
4 9 3 7 3 | Array: 3 3 7 9 |
Tasks
Work in order. Level A is for everyone, Level B is standard, Level C is a challenge.
Below each task is an editor with starter code. Replace each // TODO with your code, adjust the input if needed, and click Run. Code in the editor is not saved, so copy it if you want to keep it; final evaluation is performed in the quiz at the end.
n (1 ≤ n ≤ 100), then n integers into an array (each on a separate line). Sort the array in ascending order using selection sort and print on three lines: Array: ... (sorted array, each element preceded by a space), Min: ..., and Max: ....Input (keyboard) – data read by the program, each on a separate line; you can modify them:
/*****************************************************************************
Technical School Pirot
Exercise - 7.4 - Selection sort of an array
*****************************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
int n;
int a[100];
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for (int i = 0; i < n - 1; i++)
{
int m = i;
for (int j = i + 1; j < n; j++)
{
if (a[j] < a[m])
m = j;
}
int t = a[i];
a[i] = a[m];
a[m] = t;
}
printf("Array:");
for (int i = 0; i < n; i++)
{
printf(" %d", a[i]);
}
printf("
");
// TODO: complete the program with minimum and maximum elements
return 0;
}
4 9 3 7 3
Expected output for the above input:
Array: 3 3 7 9 Min: 3 Max: 9
Array: ... and Median: ... (middle element of the sorted array; for an even number of elements, the arithmetic mean of the two middle elements; one decimal place).Input (keyboard) – data read by the program, each on a separate line; you can modify them:
/*****************************************************************************
Technical School Pirot
Exercise - 7.4 - Selection sort of an array
*****************************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
int n;
int a[100];
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for (int i = 0; i < n - 1; i++)
{
int m = i;
for (int j = i + 1; j < n; j++)
{
if (a[j] < a[m])
m = j;
}
int t = a[i];
a[i] = a[m];
a[m] = t;
}
printf("Array:");
for (int i = 0; i < n; i++)
{
printf(" %d", a[i]);
}
printf("
");
// TODO: complete the program with descending sort and median calculation
return 0;
}
4 9 3 7 3
Expected output for the above input:
Array: 9 7 3 3 Median: 5.0
Array: ..., Distinct: ... (how many distinct values are in the array), and Duplicates: ... (how many duplicate elements, i.e. n minus the number of distinct elements).Input (keyboard) – data read by the program, each on a separate line; you can modify them:
/*****************************************************************************
Technical School Pirot
Exercise - 7.4 - Selection sort of an array
*****************************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
int n;
int a[100];
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for (int i = 0; i < n - 1; i++)
{
int m = i;
for (int j = i + 1; j < n; j++)
{
if (a[j] < a[m])
m = j;
}
int t = a[i];
a[i] = a[m];
a[m] = t;
}
printf("Array:");
for (int i = 0; i < n; i++)
{
printf(" %d", a[i]);
}
printf("
");
// TODO: complete the program with distinct and duplicate count
return 0;
}
4 9 3 7 3
Expected output for the above input:
Array: 3 3 7 9 Distinct: 3 Duplicates: 1
Explore (Ungraded)
First predict the result, then verify it in the program.
- How many comparisons does selection sort make for an array of 10 elements? What about 100?
- What happens with an array that is already sorted? Does the number of comparisons change?
- Modify the program to sort only the first 5 elements of the array.
Hints
Open hints one by one, only when you get stuck.
Hint 1
m), and perform the swap only after the inner loop finishes.Hint 2
a[j] > a[m] yields descending order. Median: double med = (n % 2) ? a[n / 2] : (a[n / 2 - 1] + a[n / 2]) / 2.0;.Hint 3
distinct = 1; then for i from 1: if (a[i] != a[i - 1]) distinct++;.Hint 4
Self-Check
Before submitting, check whether:
- ☐ The outer loop goes up to
n - 2(the last element is already in place by then). - ☐ Swapping uses a temporary variable and is executed once per outer loop iteration.
- ☐ Median works for both even and odd number of elements.
- ☐ The program also works for a single-element array.