0% found this document useful (0 votes)
6 views

Sorting A 2D Array

2d array

Uploaded by

nailearivera45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Sorting A 2D Array

2d array

Uploaded by

nailearivera45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Here s how you can implement a selection

sort algorithm to sort the rows of a


two-dimensional array in Delphi:
```delphi
program Sort2DArrayRows;
{$APPTYPE CONSOLE}
uses
SysUtils;
const
Rows = 5;
Cols = 5;
var
arrNumbers: array[1..Rows, 1..Cols] of
Integer;
i, j: Integer;
// Procedure to generate random numbers
procedure GenerateRandomNumbers;
begin
Randomize; // Initialize random number
generator
for i := 1 to Rows do
begin
for j := 1 to Cols do
begin
arrNumbers[i, j] := Random(91) + 10; //
Generates a number from 10 to 100
end;
end;
end;
// Procedure to print the array
procedure PrintArray;
begin
for i := 1 to Rows do
begin
for j := 1 to Cols do
begin
Write(arrNumbers[i, j]:4);
end;
Writeln;
end;
end;
// Procedure to sort each row using
Selection Sort
procedure SortRows;
var
j, k, minIndex, temp: Integer;
begin
for i := 1 to Rows do
begin
// Selection sort for the current row
for j := 1 to Cols - 1 do
begin
minIndex := j;
// Find the index of the minimum
element in the remaining unsorted part of
the row
for k := j + 1 to Cols do
begin
if arrNumbers[i, k] < arrNumbers[i,
minIndex] then
begin
minIndex := k;
end;
end;
// Swap the found minimum element
with the first element of the unsorted part
if minIndex <> j then
begin
temp := arrNumbers[i, j];
arrNumbers[i, j] := arrNumbers[i,
minIndex];
arrNumbers[i, minIndex] := temp;
end;
end;
end;
end;
begin
// Generate random numbers
GenerateRandomNumbers;
// Print original array
Writeln('Original Array:');
PrintArray;
// Sort each row
SortRows;
// Print sorted array
Writeln('Sorted Rows Array:');
PrintArray;
Readln; // Wait for user input before
closing
end.
```
### Explanation:
1. **GenerateRandomNumbers**: This
procedure fills the `arrNumbers` array with
random integers in the range of 10 to 100.
2. **PrintArray**: This procedure displays
the contents of the two-dimensional array
in a formatted way.
3. **SortRows**: This procedure sorts each
row of the array using the Selection Sort
algorithm:
- For each row, the outer loop iterates
through each element.
- The inner loop finds the index of the
minimum element in the remaining
unsorted portion of the row.
- After finding the minimum element's
index, it swaps the minimum element with
the current element.
4. **Main Program**: The program
generates random numbers, prints the
original array, sorts each row using
selection sort, and then prints the sorted
array.
You can compile and run this code in a
Delphi environment to see how the rows of
the 2D array are sorted using the selection
sort algorithm.

You might also like