## Algorithm Help

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

Moderators: SecretSquirrel, just brew it!

### Algorithm Help

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 = 5REDIM MyArray(Maxsize)For I = 1 to MaxsizeMyArray (I) = INT(Rnd(1) *100) + 1PRINT MyArray(I); SPACE\$(1);NEXT IPRINT "(Initial Array)"For Arraypos = 2 to Maxsize Tempvalue = Myarray(Arraypos)StopNow = 0Count = 1Time2Stop = 0WHILE (TIme2Stop = 0)If TempValue < MyArray(Count) THENFor J = ArrayPos to Count Step -1Myarray(J) = Myarray(J - 1)NEXT jMyarray(count) = TempValueStopNow = 1FOR I = 1 to MaxSizePRINT Myarray(I); SPACE\$;Next IPRINTEND IFCount = Count +1If (StopNow = 1) OR (Count = ArrayPos) THENTime2stop = 1End ifWENDNEXT ArrayPosFor I =1 To MaxSize Print MyArray(I) SPACE\$;Next IPRINT "(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

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: 16765
Joined: Sat Jan 26, 2002 7:00 pm
Location: The Heart of Dixie

### Re: Algorithm Help

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: 7394
Joined: Tue Jun 10, 2003 11:06 am
Location: Detroit, MI

### Re: Algorithm Help

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

Posts: 596
Joined: Fri Jun 18, 2004 2:08 am
Location: MX

### Re: Algorithm Help

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.
The years just pass like trains. I wave, but they don't slow down.
-- Steven Wilson
just brew it!
Gold subscriber

Posts: 43714
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

### Re: Algorithm Help

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

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: 7394
Joined: Tue Jun 10, 2003 11:06 am
Location: Detroit, MI

### Re: Algorithm Help

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...
The years just pass like trains. I wave, but they don't slow down.
-- Steven Wilson
just brew it!
Gold subscriber

Posts: 43714
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

### Re: Algorithm Help

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

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.

Oh, and I'd like to extend a (slightly belated) welcome to the Tech Report forums!
The years just pass like trains. I wave, but they don't slow down.
-- Steven Wilson
just brew it!