Last Updated:

Bubble sorting in Pascal

Bubble sorting in Pascal

Let's imagine that you need to write a program to work with a large number of variables. For example, you need 1000 variables of type integer, declare something 1000 variables? This approach is very effective, as it will take a lot of time. Therefore, in programming there are data structures that are huge cabinets with shuflyads, and each of them is a kind of variable inside which lies the value.

In programming, such a cabinet is called an array. It contains shoes, which are called cells, and the data type of the entire array is the same. You need one variable to store an array. To use a cell during the program, you need to specify its serial number. But it is also worth understanding that the cell number and its value are completely different things, the cell address is needed only to access it, but not for use directly in the program itself.

And so, the array consists of cells (shufladok). But they need to be numbered somehow, so usually integers are used in numbering, by the way, not always the numbering goes in order. The address of the item itself is called the index.

Also remember that the elements of an array are strictly of the same type, if the array is of type integer, then there are integers in the cells, and if the type is string, then only text is in the elements.

Arrays can also be indexed by constants or variables.

There are several ways to create an array:

const n = 200;
months = (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec);
years = 1900..2100;
people = array[years] of longint;
growth: array[months] of real;
hum: people;
notes: array[1..n] of string;

To access a specific cell of an array, you need to specify its name (the name of the variable to which the array is bound) and specify the index in square brackets.

Simple arrays are called one-dimensional arrays. They have a linear structure:

var ch: array [1..11] of char;
h: char;
i: integer;

for i := 1 to 11 do read (ch[i]);

for i := 1 to 11 do write (ch[i]:3);


In this part of the code, the array is given 11 cells. The array itself is of type char, so the data in it has this type. The program reads 11 characters that the user will enter, and then, in the second for loop, displays them on the screen. The variable i denotes the array index and is of type integer.

Array sorting

Sometimes when working with arrays, you need to order data in ascending or descending order. There are many ways to do this, but below we will analyze the method of sorting with a bubble, since it is both effective and quite easy to understand.

The bubble method is that the lighter elements (bubbles) are at the beginning of the array and the more complex ones are at the end.

The essence of the method

The essence of this method is that the elements of every 2 elements are compared in pairs. And if the element is larger, then it moves back. And so it goes until the end of the array. As a result, it turns out that at the end there will be the largest element.

But, of course, one comparison is not enough, so you need to run several such loops, but with a small condition, since the last contents of the cell do not need to be compared, then we simply subtract from the number of comparisons in the loop, and the last object will not be affected.

m = 10;

arr: array[1..m] of integer;
i, j, k: integer;


write ('Source array: ');
for i := 1 to m do begin
arr[i] := random(256);
write (arr[i]:4);
writeln; writeln;

for i := 1 to m-1 do
for j := 1 to m-i do
if arr[j] > arr[j+1] then begin
k := arr[j];
arr[j] := arr[j+1];
arr[j+1] := k

write ('Sorted array: ');
for i := 1 to m do
write (arr[i]:4);