为了账号安全,请及时绑定邮箱和手机立即绑定

将C改成VB希尔排序, 数组a最好改成inputbox输入?

将C改成VB希尔排序, 数组a最好改成inputbox输入?

RISEBY 2022-06-16 17:11:31
#include <stdio.h>void swap(int a[], int i, int j){int t = a[i];a[i] = a[j];a[j] = t;}void selsort(int a[], int n, int incr){int i, j;for (i = incr; i < n; i += incr)for (j = i; (j >= incr)&& (a[j] < a[j-incr]); j -= incr)swap(a, j, j-incr);}void shellsort(int a[], int n){int i, j;for (i = n / 2; i > 2; i /= 2)for (j = 0; j < i; j++)selsort(&a[j], n - j, i);selsort(a, n, 1);}int main(){int i;int a[10] = {2, 1, 9, 0, 8, 3, 7, 5, 6, 4};shellsort(a, 10);for (i = 0; i < 10 ; i++)printf("%d ", a[i]);printf("\n");return 0;}
查看完整描述

1 回答

?
不负相思意

TA贡献1777条经验 获得超10个赞

'在模块中粘贴下面的代码,“工程属性-启动对象”要改为“Sub Main”

Sub Swap(a() As Long, i As Long, j As Long)
Dim t As Long
t = a(i)
a(i) = a(j)
a(j) = t
End Sub

Sub SelSort(a() As Long, n As Long, incr As Long)
Dim i As Long, j As Long
For i = incr To (n - 1)
For j = i To incr Step -1
If j >= incr And a(j) < a(j - incr) Then Swap a, j, j - incr
Next j
Next i
End Sub

Sub ShellSort(a() As Long, n As Long)
Dim i As Long, j As Long
For i = n / 2 To 3 Step -2
For j = 0 To (i - 1)
SelSort a, n - j, i
SelSort a, n, 1
Next j
Next i
End Sub

Sub Main()
Dim i As Long
Dim a() As Long
Do
ReDim Preserve a(i)
a(i) = Val(InputBox("请输入第 " & (i + 1) & " 个数,输入小于0的数结束"))
i = i + 1
Loop Until a(i - 1) < 0
i = UBound(a)
If i > 0 Then
ReDim Preserve a(i - 1)
ShellSort a, UBound(a) + 1
For i = 0 To UBound(a)
Debug.Print a(i)
Next i
End If
End Sub



查看完整回答
反对 回复 2022-06-20
  • 1 回答
  • 0 关注
  • 119 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信