If you want to complete this program here you are free to use it, some commands
ID: 3542978 • Letter: I
Question
If you want to complete this program here you are free to use it, some commands are missing
Sub main()
Dim first, last As Integer
Dim list(5) As Integer
list(1) = 4
list(2) = 1
list(3) = 3
list(4) = 7
list(5) = 2
first = 1
last = 5
mergeSort a:=list, p:=first, r:=last
'we would typically do it like this mergeSort(a, first, last)
'but this is how VBA does it
End Sub
Sub mergeSort(a() As Integer, p, r)
Dim mid As Double
Dim i, q As Integer
If p < r Then
mid = (p + r) / 2
q = Int(mid)
MsgBox p
MsgBox q
mergeSort a:=a, p:=p, r:=q
MsgBox q + 1
MsgBox r
mergeSort a:=a, p:=(q + 1), r:=r
'then i should put the "merge" command here to merge the
'two pieces: p,q and q+1, r
End If
End Sub
Sub merge(a() As Integer, first, last)
'this is where the code should be that merges two sets of data.
'recall that they will not be random lists,
'but the two inputs will be already in order with repect to themselves
'(but not in order with respect to the other list of course)
End Sub
If you want to complete this program here you are free to use it, some commands are missing
Sub main()
Dim first, last As Integer
Dim list(5) As Integer
list(1) = 4
list(2) = 1
list(3) = 3
list(4) = 7
list(5) = 2
first = 1
last = 5
mergeSort a:=list, p:=first, r:=last
'we would typically do it like this mergeSort(a, first, last)
'but this is how VBA does it
End Sub
Sub mergeSort(a() As Integer, p, r)
Dim mid As Double
Dim i, q As Integer
If p < r Then
mid = (p + r) / 2
q = Int(mid)
MsgBox p
MsgBox q
mergeSort a:=a, p:=p, r:=q
MsgBox q + 1
MsgBox r
mergeSort a:=a, p:=(q + 1), r:=r
'then i should put the "merge" command here to merge the
'two pieces: p,q and q+1, r
End If
End Sub
Sub merge(a() As Integer, first, last)
'this is where the code should be that merges two sets of data.
'recall that they will not be random lists,
'but the two inputs will be already in order with repect to themselves
'(but not in order with respect to the other list of course)
End Sub
Code the "merge" portion of the "mergeSort" algorithm. Code it as a function - i.e. as a piece of code that can be "called" in the "Sub main()" part of the code. Do so passing it the information: A, p, q, r - the array, beginning point, mid point, and final point. If A = 23816 and p, q, r are 1,3, and 5. In other words, for this example, assume the algorithm has already sorted from 1 to 3 and from 4 to 5. Now the job is to put these "two" arrays (which are stored in this one array) into 1 array that is sorted entirely. After testing, incorporate it into "mergesort" to complete the "mergesort" algorithm.Explanation / Answer
Sub merge(a() As Integer, first, last)
'this is where the code should be that merges two sets of data.
'recall that they will not be random lists,
'but the two inputs will be already in order with repect to themselves
'(but not in order with respect to the other list of course)
End Sub
Sub merge(a() As Integer, first, last)
first is the first element of first sorted arraylast is the last element of first sorted array
b=a(first last) as integer
c=a(last+1,a.length) as integer
i=0;
j=0;
k=0;
so while(i<last or j < a.length ){
if(b(i)<c(j)
a(k)=b(i) and i++ ;
else a(k)=c(j) and j++;
k++;
}
if (i==last) while (j<a.length){a[k]=c[j];j++}
else while (i<last){a[k]=a[i];i++}
'this is where the code should be that merges two sets of data.
'recall that they will not be random lists,
'but the two inputs will be already in order with repect to themselves
'(but not in order with respect to the other list of course)
End Sub