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;
}
}}}