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).
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?)
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
A C G Z S
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
Coded by KC. Emenike, for blah blah assignment
Program BubbleSort(input, output);
(*Define type constant size of array*)
(*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*)
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*)
for j:=1 to size do
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!!!
- Lab Work ~ A C Program for bubble sort (depthgr8.wordpress.com)
- Implementation of while loop in bubble sort? (stackoverflow.com)
- Computer Algorithms: Bucket Sort (stoimen.com)
- Trying to sort array of random integers (daniweb.com)
- Case 81: Bubble Sort (thecodelesscode.com)