How to resolve the algorithm Sorting algorithms/Selection sort step by step in the PL/I programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sorting algorithms/Selection sort step by step in the PL/I programming language
Table of Contents
Problem Statement
Sort an array (or list) of elements using the Selection sort algorithm.
It works as follows: First find the smallest element in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted.
Its asymptotic complexity is O(n2) making it inefficient on large arrays. Its primary purpose is for when writing data is very expensive (slow) when compared to reading, eg. writing to flash memory or EEPROM. No other sorting algorithm has less data movement.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting algorithms/Selection sort step by step in the PL/I programming language
Source code in the pl/i programming language
Selection: procedure options (main); /* 2 November 2013 */
declare a(10) fixed binary initial (
5, 7, 3, 98, 4, -3, 25, 20, 60, 17);
put edit (trim(a)) (a, x(1));
call Selection_Sort (a);
put skip edit (trim(a)) (a, x(1));
Selection_sort: procedure (a);
declare a(*) fixed binary;
declare t fixed binary;
declare n fixed binary;
declare (i, j, k) fixed binary;
n = hbound(a,1);
do j = 1 to n;
k = j; t = a(j);
do i = j+1 to n;
if t > a(i) then do; t = a(i); k = i; end;
end;
a(k) = a(j); a(j) = t;
end;
end Selection_Sort;
end Selection;
You may also check:How to resolve the algorithm Infinity step by step in the PL/I programming language
You may also check:How to resolve the algorithm Singly-linked list/Traversal step by step in the PL/I programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the PL/I programming language
You may also check:How to resolve the algorithm Numerical integration step by step in the PL/I programming language
You may also check:How to resolve the algorithm Largest proper divisor of n step by step in the PL/I programming language