# Pascal Bubble Sort Algorithm and Code

English: an example on bubble sort. (Photo credit: Wikipedia)

Did I tell you that I also run a course in Information Technology? Well, if I haven’t, I just did now. So now you know. I’ve never had a theoretical programming language background (apart from the basic Fortran and C programming that students never took serious in University years, so if you are currently taking a basic programming language course in school, I suggest you take it seriously), so I never had any experience with the Bubble sort algorithm. Anyway, now I had to know it, since it was necessary for the solution of the problem statement. And I promise you, it eventually turned out to be too easy (though some of my mates still dubbed the solutions).

# The Challenge

With virtually no prior knowledge of Pascal programming, I was required to write a program that reads 10 inputs, and sort them using the BUBBLE SORT algorithm ONLY.

# The Available

All I had in my disposal was:

• Two weeks, and judging this time by my schedule, that was like two days
• A knowledge of data types and their usage in Pascal, thanks to Mr. Lecturer
• The Internet (of course, what could we do without the King Solomon of our time?)

# The Approach

Understand the problem

“What the heck is Bubble sort?” I asked myself. If there is a sort type called Bubble Sort, then there are other techniques of sorting data.

What next? Ask King Solomon of course (By now you should know who king Solomon is). Thanks to many resources online, I was able to get useful resources on the internet on what bubble sort was, the other techniques of sort, and even a sample bubble sort procedure (by that time I didn’t have an idea what a procedure meant though).

So, what is sort?

Sorting (according to the English dictionary) is the arrangement of items in a dataset either in an order, which could be either ascending or descending. Sort techniques are bubble sort (which is what we are interested in for now), selection sort, blah blah blah.

In bubble sort algorithm, a data array is read, and the first value is compared with the second, if it is greater, it is swapped with the second value. Since the swap cannot be done without losing data in one of the variable spaces, a temporary variable is created to temporarily hold the content of one of the values while the swap is carried out. Lets look at an example.

Suppose you have a row of blocks with letters on them in random order and you wish to arrange them in alphabetical order from left to right.

G A  Z C  S

Begin with the first block (array item position 1). in this case, letter G. If the block to the right of it (i.e. array item position 2) should come before it, swap them so that they are in order. i.e.

A  G  Z  C  S

If you were doing this by hand, you might just pick the blocks to be moved with one in each hand and cross your arms to swap them. Or you might move the first one in position a1 out of it’s position temporarily (and store it in a temporary position, say temp) and then move the second one in position a2 to the location of a1 and then finally move the value in temp (which of course was originally the value in a1) to a2.

Now repeat the same steps, this time between the second (a2) and the third (a3) item in the array.

A  C  Z  G  S

Then again

A C  G  Z  S

And again

A  C  G  S  Z

And that’s it.

Now the code for sorting an array of 10 numeric characters is show below

(*
This simple program reads 10 numbers (they must not be blank, or it will
return an error) and sorts them in DESCENDING order using the Bubble Sort
Method

Coded by KC. Emenike, for blah blah assignment
*)

Program BubbleSort(input, output);

(*Define type constant size of array*)
const size=10;

(*Define array type*)
type arrtype = array[1..size] of integer;

(*Declare variables for i(array data placeholder), j(count variable) and*)
(*temp(swapped data placeholder*)
var i,j, temp : integer;
arr : arrtype;

(*start of program*)
begin

for i:=1 to size do

(*Main bubble sort loop*)
for j:=1 to size-1 do
for i:=1 to size-1 do
if arr[i+1]>arr[i] then begin (*change > to < if you want ASCENDING order*)
temp:=arr[i];
arr[i]:=arr[i+1];
arr[i+1]:=temp;
end;

(*Write Output*)
for j:=1 to size do
writeln(arr[j]);

end.

# Final Words

Please pardon the “un-programmy” look of my program; still learning the trade myself. But anyways, here is the whole deal – without the procedure I was unable to understand initially.

And you could notice that the program suddenly disappeared after execution. Well, I didn’t know how to fix that, but re-running the program made me able to see the result of the previous program, and from the result, my program was correct!

Please feel free to post comments in the comment section below. I would appreciate your suggestions that could enable me craft a better code, and even procedures too.

[UPDATE]: I finally understood how procedures work, and I was able to even add a pause procedure to make my program display for a few seconds before disappearing. Yippee!!!

## 18 thoughts on “Pascal Bubble Sort Algorithm and Code”

1. lazarous kaonga

cool

2. What’s up, for all time i used to check weblog posts here early in the break of day, because i like to gain knowledge of more and more.

4. A farmer has a plot that is shaped like a long rectangular widescreen units of M N units (1 ≤ M ≤ N, 50). On the ground are planted the trees, which the farmer does not wish to cut. Wanting to oversee culture, ferierul and a small square-shaped robot with 3 units per side which can teleghida through the farm, complete with drive unit a specific surface.
The robot can move vertically and horizontally, but cannot pass the trees, not destroy, it cannot rotate and needs for movement of an area corresponding to the size of.
Write a program which determines the maximum area that can track, farmer using this system.

Date of FARM input file. IN: N M C11C22 …C1M C21C22. . . C2M …
CN1CN2 …CNM Last N lines, each line encodes contains how many meters (without being separated by spaces) with the meaning: ‘. ‘-land; ‘+ ‘-the place where a tree is planted;
‘ R ‘-managing the robot.

output file FARM. OUT will get C11C22. . . C1M C21C22. . . C2M
. . .
CN1CN2. . . CNM how the farmer can use his ground robot is encoded on the N line, every line contains one M character (without being separated by spaces) with the meaning: ‘. ‘-uncovered ground robot; ‘ * ‘-land that can be checked by robot; ‘ + ‘ – the place where he left the tree.

5. Hello there! Would you mind if I share your blog with my
twitter group? There’s a lot of people that I think would really enjoy your content. Please let me know. Cheers

6. I’d should test with you here. Which is not one thing I usually do! I enjoy reading a submit that can make people think. Also, thanks for permitting me to comment!

7. What’s Happening i’m new to this, I stumbled upon this I’ve found It absolutely helpful and it has aided me out loads. I hope to contribute & help other users like its aided me. Good job.

• I’m happy you have learnt something. Please do well to share and contribute to the learning for others as well

8. Hi there, I found your website by means of Google even as searching for a comparable topic, your site came up, it appears great. I have bookmarked it in my google bookmarks.

9. Really Good Web Site Pages Got Here.. Very Interesting Subject For A Post Keep Up The Great Work!!. Check Out My Free Winnipeg Computer Repair Blog And Tell Me What You Think!!!!

• Thanks Winnipeg. Just checked out your blog. It is great! Keep up the good work.

10. Pingback: mjinubygrcxe4zssxctv

11. Pingback: cwefverbgtrnyrt

12. i agreed, this is a fantastic article for all the newbie too…

• Thanks caroline

13. Very interesting details you have remarked, thank you for posting.