sorting and query algorithms

New to FreeBASIC? Post your questions here.
Post Reply
ron77
Posts: 212
Joined: Feb 21, 2019 19:24

sorting and query algorithms

Post by ron77 »

hello here is an example of sorting algorithms

1. bubble sorting an array

2. simple query

3. binary query

Code: Select all

#Include "crt.bi"

Dim As String sFruits(...) = { _
"banana","apple","tomato","orange", _
"lime","pineapple","grape","melon", _
"onion","sallery","avocado","cucumber", _
"strawberry","carret","pear","watermelon" }

#Define AttemptPrint Print
'#Define AttemptPrint rem

Sub ShowArray( Array() As String )
   For N As Integer = 0 To UBound(Array)
      Print Array(N);" ";      
   Next
   Print
End Sub
Sub SortArray( Array() As String )
   'Bubble
   Do
      Dim As Integer iSorted = 0
      For N As Integer = 0 To UBound(Array)-1
         If Array(N) > Array(N+1) Then
            Swap Array(N),Array(N+1): iSorted=1
         EndIf
      Next
      If iSorted = 0 Then Exit Do
   Loop
End Sub
Function SimpleQuery( Array() As String , sQuery As String ) As Integer
   Var sQueryL = LCase(sQuery)
   For N As Integer = 0 To UBound(Array)
      If Array(N) = sQueryL Then 
         AttemptPrint "Attempts: " & N+1,
         Return N
      EndIf
   Next
   AttemptPrint "Attempts: " & UBound(Array)+1,
   Return -1
End Function
Function BinaryQuery( Array() As String , sQuery As String ) As Integer
   Dim As Integer iFirst=0 , iLast=UBound(Array)
   Var sQueryL = LCase(sQuery)
   
   Dim As Integer iAttempts
   
   While iLast>=iFirst
      
      iAttempts += 1
      
      Dim As Integer iMid = (iFirst+iLast+1)\2 'cint((iFirst+iLast)/2)
      Dim As Integer iResult = strcmp( sQueryL , Array(iMid) )
      If iResult = 0 Then 
         AttemptPrint "Attempts: " & iAttempts,
         Return iMid
      EndIf
      If iResult > 0 Then iFirst = iMid+1 : Continue While
      iLast = iMid-1      
   Wend
   AttemptPrint "Attempts: " & iAttempts,
   Return -1
End Function

SortArray(sFruits())
ShowArray(sFruits())

Print " ----- SimpleQuery() ----- "
Print "carret",SimpleQuery(sFruits(),"carret")
Print "watermelon",SimpleQuery(sFruits(),"watermelon")
Print "brick",SimpleQuery(sFruits(),"brick")

Print
Print " ----- BinaryQuery() ----- "
Print "carret",BinaryQuery(sFruits(),"carret")
Print "watermelon",BinaryQuery(sFruits(),"watermelon")
Print "brick",BinaryQuery(sFruits(),"brick")

Sleep
and once again I am giving a link to my small community forum - called "Pure Programming Community" dedicated to programming in freebasic, C, and assembly as well as chatbot development and also other basic languages.



I hope this is not considered spam. if so please tell me and I'll stop or erase the post / thread.

thank you.
ron77

okay I removed the link as this was inappropriate
Last edited by ron77 on Jul 01, 2021 12:39, edited 1 time in total.
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: sorting and query algorithms

Post by paul doe »

ron77 wrote:...
I hope this is not considered spam. if so please tell me and I'll stop or erase the post / thread.
...
It will be considered spam if you post a link to your forum
every
single
time
you
post
so bear that in mind next time. Thanks.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: sorting and query algorithms

Post by fxm »

ron77 wrote:and once again I am giving a link to my small community forum - called "Pure Programming Community" dedicated to programming in freebasic, C, and assembly as well as chatbot development and also other basic languages.

[url]https://.....[/url]

I hope this is not considered spam. if so please tell me and I'll stop or erase the post / thread.

thank you.
ron77
Yes:
Once it's okay, twice it's limit, now it's enough.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: sorting and query algorithms

Post by jj2007 »

ron77 wrote:I hope this is not considered spam. if so please tell me and I'll stop or erase the post / thread.
Ron, I'm not a moderator, just a simple user, but reflect on what you are doing here. There is an empty new forum, you are the only active user there. What do you have to offer to the FB community? Bubble sort, with no comments? Code like this?

Code: Select all

      If iResult = 0 Then
         AttemptPrint "Attempts: " & iAttempts,
         Return iMid
      EndIf
      If iResult > 0 Then iFirst = iMid+1 : Continue While
So what happens if iResult<0?
Vortex
Posts: 118
Joined: Sep 19, 2005 9:50

Re: sorting and query algorithms

Post by Vortex »

Hi ron77,

You can read the Pointer Data Type section of the FreeBASIC manual, it mentions about the qsort function.
Post Reply