Algorithm Help

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

Algorithm Help

Postposted on Sat Feb 18, 2012 8:22 pm

OK, first, I just want to say that I'm new here, so hello and all that. Anyways, I've been reading "Programing for Dummies" recently, and it got into the section on advanced programing topics. And that particular section was on sorting and searching algorithms. The one I don't understand is the insertion sort algorithm in Liberty Basic. Here it is(Note: There may be a few mistakes because I wrote it from looking at it, it's not the actual code):
Code: Select all
Max Size = 5
REDIM MyArray(Maxsize)

For I = 1 to Maxsize
MyArray (I) = INT(Rnd(1) *100) + 1
PRINT MyArray(I); SPACE$(1);
NEXT I
PRINT "(Initial Array)"

For Arraypos = 2 to Maxsize
Tempvalue = Myarray(Arraypos)
StopNow = 0
Count = 1
Time2Stop = 0
WHILE (TIme2Stop = 0)
If TempValue < MyArray(Count) THEN
For J = ArrayPos to Count Step -1
Myarray(J) = Myarray(J - 1)
NEXT j
Myarray(count) = TempValue
StopNow = 1
FOR I = 1 to MaxSize
PRINT Myarray(I); SPACE$;
Next I
PRINT
END IF
Count = Count +1
If (StopNow = 1) OR (Count = ArrayPos) THEN
Time2stop = 1
End if
WEND
NEXT ArrayPos
For I =1 To MaxSize
Print MyArray(I) SPACE$;
Next I
PRINT "(Sorted Array)"


1: Why did they make Maxsize equal to 6 if the only used 5? And how did they use the Redim function if Myarray hasn't been declaired yet?
2:What does INT (RND (1) *100) +1 do? It never explained it.
3:ArrayPos to Count step -1 would repeat twice, wouldn't it? If so, why would it need to?
4:How did they use the PRINT function with nothing to print?

These are my questions for now.
Dr. Coconut
Gerbil
 
Posts: 21
Joined: Sat Feb 18, 2012 7:45 pm

Re: Algorithm Help

Postposted on Sat Feb 18, 2012 9:26 pm

Dr. Coconut wrote:2:What does INT (RND (1) *100) +1 do? It never explained it.
RND(1) is a random real number between 0 and 1. If you multiply it by 100 then take the integer, you'll have a random integer from 0 to 99. Adding 1 gives you a random integer from 1 to 100. This is just filling the array with random numbers prior to sorting it.

The first PRINT commands output the values of the unsorted array, then the text "(initial array)".
Inside the sort loop, they're printing out the array each time that it is reordered. The looped PRINTs with a semi-colon at the end print the value and a space but they do not include a carriage return and line feed. The PRINT by itself adds the carriage return and line feed at the end of the line.
After the sort is complete, they output the values of the sorted array and then the text "(sorted array)".

The way that the insertion sort works is to go through the array looking at each new value and putting it into the correct order in the part of the array that has already been sorted. I know that when I've had to manually sort stacks of records for filing, I've used an insertion sort method. You may want to try this and compare it to a bubble sort, counting the number of comparisons and swaps for each method. Move on to heap sort, quick sort, etc.
JustAnEngineer
Gerbil God
Gold subscriber
 
 
Posts: 15528
Joined: Sat Jan 26, 2002 7:00 pm
Location: The Heart of Dixie

Re: Algorithm Help

Postposted on Sun Feb 19, 2012 12:35 am

Dr. Coconut wrote:1: Why did they make Maxsize equal to 6 if the only used 5? And how did they use the Redim function if Myarray hasn't been declaired yet?

REDIM: http://www.libertybasicuniversity.com/l ... 1ST4AN.htm

Dr. Coconut wrote:2:What does INT (RND (1) *100) +1 do? It never explained it.

RND(n): http://www.libertybasicuniversity.com/l ... NLSAKK.htm

Dr. Coconut wrote:3:ArrayPos to Count step -1 would repeat twice, wouldn't it? If so, why would it need to?

Code: Select all
For J = ArrayPos to Count Step -1
  Myarray(J) = Myarray(J - 1)
NEXT j

FOR..NEXT: http://www.libertybasicuniversity.com/lb4help/

Dr. Coconut wrote:4:How did they use the PRINT function with nothing to print?

PRINT: http://www.libertybasicuniversity.com/l ... 1R69FU.htm

The entire HELP manual online for every single question you could ever dream up of for this "liberty" $59.95 RIP-OFF language: http://www.libertybasicuniversity.com/lb4help/
thegleek
Darth Gerbil
Gold subscriber
 
 
Posts: 7367
Joined: Tue Jun 10, 2003 11:06 am
Location: Detroit, MI

Re: Algorithm Help

Postposted on Sun Feb 19, 2012 2:46 pm

Wow... the Libery BASIC website brought be back to the 90's....
Intel Core i7 3770K / 16 GB Kingston HyperX DDR3-1600 / Intel 520 180GB SSD / Zotac GTX660 / Corsair TX650 V2 PSU / Audigy 2 ZS
Wajo
Gerbil Elite
Silver subscriber
 
 
Posts: 596
Joined: Fri Jun 18, 2004 2:08 am
Location: MX

Re: Algorithm Help

Postposted on Sun Feb 19, 2012 4:49 pm

I really have to wonder whether BASIC is still a reasonable first language for beginners, now that we've got more modern interpreted languages like Python.
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37962
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Algorithm Help

Postposted on Sun Feb 19, 2012 5:02 pm

Thanks for the help, I'm still kind of new to programming, so that would explain the questions. I do know a few things though.
Dr. Coconut
Gerbil
 
Posts: 21
Joined: Sat Feb 18, 2012 7:45 pm

Re: Algorithm Help

Postposted on Mon Feb 20, 2012 6:37 pm

just brew it! wrote:I really have to wonder whether BASIC is still a reasonable first language for beginners, now that we've got more modern interpreted languages like Python.

What I'm really curious about is how one FINDS this "Libery" BASIC... is it that common? was it a factor of advertisement? word-of-mouth/recommendation by peers? Or is this some lame blatant attempt at a spammer trying to "advertise" this unknown BS version of basic to make money or paid-to-click crap?
thegleek
Darth Gerbil
Gold subscriber
 
 
Posts: 7367
Joined: Tue Jun 10, 2003 11:06 am
Location: Detroit, MI

Re: Algorithm Help

Postposted on Mon Feb 20, 2012 6:45 pm

thegleek wrote:
just brew it! wrote:I really have to wonder whether BASIC is still a reasonable first language for beginners, now that we've got more modern interpreted languages like Python.

What I'm really curious about is how one FINDS this "Libery" BASIC... is it that common? was it a factor of advertisement? word-of-mouth/recommendation by peers? Or is this some lame blatant attempt at a spammer trying to "advertise" this unknown BS version of basic to make money or paid-to-click crap?

That's pretty funny given that you were the first person in this thread to post links to their web site... :lol:
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37962
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Algorithm Help

Postposted on Tue Feb 21, 2012 8:01 pm

Umm, no, I'm not trying to spam, I read it in a programing book.
Dr. Coconut
Gerbil
 
Posts: 21
Joined: Sat Feb 18, 2012 7:45 pm

Re: Algorithm Help

Postposted on Tue Feb 21, 2012 9:34 pm

Dr. Coconut wrote:Umm, no, I'm not trying to spam, I read it in a programing book.

Don't worry about it, thegleek is just being... himself. :lol:

Oh, and I'd like to extend a (slightly belated) welcome to the Tech Report forums!
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37962
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 1 guest