Posted: Thu Mar 02, 2006 8:38 pm Post subject: (No subject)
Hi.
I am wondering if someone can help me explain what this does:
procedure swap (var list : array 1 .. * of int, i, j : int)
what does "*" do and what are i and j?
thank you.
Sponsor Sponsor
nemesest
Posted: Thu Mar 02, 2006 9:19 pm Post subject: (No subject)
Quote:
procedure shift (var list : array 1 .. * of int, i, j : int)
const temp := list (j)
for decreasing k : j .. i + 1
list (k) := list (k - 1)
end for
list (i) := temp
end shift
procedure insertionSort (var list : array 1 .. * of int)
for j : 2 .. upper (list)
var i := 1
loop
exit when i = j or list (i) >= list (j)
i += 1
end loop
shift (list, i, j)
end for
end insertionSort
%main proggy
%create random data
var numbers : array 1 .. 5000 of int
for i : 1 .. 5000
numbers (i) := i
end for
insertionSort (numbers)
for i : 1 .. 5000
end for
drawfill (1, 50, 50, 50)
Also, borrowing the author's code, would that be a fair test? I have found that the reversed numbers are faster than ordered numbers? Is this right?
Delos
Posted: Thu Mar 02, 2006 9:47 pm Post subject: (No subject)
nemesest wrote:
Hi.
I am wondering if someone can help me explain what this does:
procedure swap (var list : array 1 .. * of int, i, j : int)
what does "*" do and what are i and j?
thank you.
The '*'s indicate that the upper bounds of the array are not known at compilation time. They are dynamic. This is because the array being passed to the procedure may be of various lengths.
i and j are simply parameters - in this case specifying which subset of the array is to be sorted.
As for using the code - technically zylum posted this up as a service to other users. However, if you do use it you really should cite him as the creator/coder.
My personal view is that if you are not able to understand most, if not all, of the code, then you shouldn't be using it. If you were using this on an assignment, for instance, you might get asked what a particular set of lines means...that could be troublesome.
nemesest
Posted: Sat Mar 04, 2006 9:34 am Post subject: (No subject)
I'm sorry for asking so many questions but
Quote:
procedure shift (var list : array 1 .. * of int, i, j : int)
const temp := list (j) for decreasing k : j .. i + 1
list (k) := list (k - 1)
end for
list (i) := temp
end shift
What is the purpose of the underlined code?
Quote:
i += 1
And what does + do?
As for using the creator's code I am only using it for myself. I learned insertion, bubble, and selection sort at school, so I wanted to look at other sorts.
Cervantes
Posted: Sat Mar 04, 2006 11:36 am Post subject: (No subject)
He's declaring a constant. A constant is just like a variable, except it cannot be changed. The temp constant is storing the value of list (j) so that its value isn't lost when we go to shift things around.
+ is the addition operator. += is a combination of the addition (+) and assignment (:=) operators.
code:
var x := 5
x += 7 % this is the same as 'x := x + 7'
put x
We can do this with other operators, such as -, *, /, mod...
StealthArcher
Posted: Mon Sep 17, 2007 10:19 pm Post subject: (No subject)
person @ Tue Jan 24, 2006 7:06 pm wrote:
no offense, but bubble and selection sort kinda sux
Bubble sort is better than almost anything, when finding one discrepancy in a pile of data.
Namely you have a list of 10000 words, only one is out of place, bsort will do it the fastest.
Clayton
Posted: Tue Sep 18, 2007 7:10 am Post subject: RE:sorting procedures
Dude... this thing is over a year old...
Also, bubble sort is incredibly slow when compared to many other sorts. Period.
rdrake
Posted: Tue Sep 18, 2007 11:45 am Post subject: Re: RE:sorting procedures
Clayton @ Tue Sep 18, 2007 7:10 am wrote:
Also, bubble sort is incredibly slow when compared to many other sorts. Period.
It may be slower in almost every case, but it's fast when it only has to make one pass.
And how many times is that going to happen? Sure bubble sort is fast when you have to go through once, but it's not going to happen all that often. You're almost always better off going with something else like a quick sort.
Posted: Sat Mar 07, 2015 9:50 pm Post subject: RE:sorting procedures
Is there any way to do this without procedure? I haven't learned that yet
Nathan4102
Posted: Sat Mar 07, 2015 10:29 pm Post subject: RE:sorting procedures
I mean you could, but you shouldn't. Look up some tutorials on procedures and functions, they're really easy to work with and it'll make everything so much easier.