Why my bubble sort does not work?

New to FreeBASIC? Post your questions here.
Post Reply
demosthenesk
Posts: 237
Joined: Jul 15, 2021 7:23
Location: Greece
Contact:

Why my bubble sort does not work?

Post by demosthenesk »

Hello,
i try to implement a bubble sort algorithm but i fail. Please help me.

Here is my code

Code: Select all

'declare an array 
Dim array(1 To 10) As Integer

'Fill array with random integers
For i As Integer = 1 To 10
	Randomize
	'get a random integer from 1 to 10
	array(i) = Int(Rnd * 10) + 1
Next

'print array
Sub printArray(array1() As Integer)
	For i As Integer = 1 To 10
		Print ; array1(i)
	Next
End Sub

'bubble sort
Sub BubbleSort(array2() As Integer)
	Dim size As Integer = Ubound(array2)
	Dim temp As Integer = 0
	For i As Integer = 0 To (i < size)
		For j As Integer = 1 To (j < (size-1))
			If array2(j-1) > array2(j) Then
				temp = array2(j-1)
				array2(j-1) = array2(j)
				array2(j) = temp
			End if
		Next
	Next
'printArray(array2())
End Sub

BubbleSort(array())
Print "-------"
printArray(array())


how can i pass array byref, maybe with pointers...i dont know...

i tried to port from java

Code: Select all

class BubbleSortExample {
	static void bubbleSort(int[] arr) {
		int n = arr.length;
		int temp = 0;
		for(int i=0; i < n; i++){
			for(int j=1; j < (n-i); j++){
				if(arr[j-1] > arr[j])
				{
				//swap
				temp = arr[j-1];
				arr[j-1] = arr[j];
				arr[j] = temp;
				}
}}}
demosthenesk
Posts: 237
Joined: Jul 15, 2021 7:23
Location: Greece
Contact:

Re: Why my bubble sort does not work?

Post by demosthenesk »

well i found my way...thanks anyway...

https://rosettacode.org/wiki/Sorting_al ... #FreeBASIC
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Why my bubble sort does not work?

Post by badidea »

There are several bug in your first post, e.g. in line:

Code: Select all

For i As Integer = 0 To (i < size)
That is not how freebasic for/next loops work, (i < size) will return -1 (false), so the loop never starts. Second, variable i is never used, and if you did, then how probably want to start the loop at 1 (instead of 0).
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Why my bubble sort does not work?

Post by dodicat »

An array is always passed by reference in Freebasic.
Here is the bubblesort I use from time to time, I compare it with yours.

Code: Select all


Sub bubblesort(bs() As Long)
    ' sort from lower bound to the highter bound
    ' array's can have subscript range from -2147483648 to +2147483647
    Dim As Long lb = LBound(bs)
    Dim As Long ub = UBound(bs)
    Dim As Long done, i
 
    Do
        done = 0
        For i = lb To ub -1
            ' replace "<" with ">" for downwards sort
            If bs(i) > bs(i +1) Then
                Swap bs(i), bs(i +1)
                done = 1
            End If
        Next
    Loop Until done = 0
 
End Sub

sub bubblesort2(a() as long)
for n1 as long=lbound(a) to ubound(a)-1
    for n2 as long =n1+1 to ubound(a)
        if a(n2)<a(n1) then swap a(n1),a(n2)
    next n2
next n1
end sub

dim as long a(7000),b(lbound(a) to ubound(a))
randomize 1
for n as long=lbound(a) to ubound(a)
    a(n)=rnd*100000
    b(n)=a(n)
next

dim as double t=timer
bubblesort(a())
print timer-t
for n as long=lbound(a) to 10
    print a(n);
next
print
print
print
 t=timer
bubblesort2(b())
print timer-t
for n as long=lbound(b) to 10
    print b(n);
next
print

  
demosthenesk
Posts: 237
Joined: Jul 15, 2021 7:23
Location: Greece
Contact:

Re: Why my bubble sort does not work?

Post by demosthenesk »

Thanks for the reply... i use also from now the same code.
Post Reply