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

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

RISEBY
浏览 120回答 1
1回答

不负相思意

'在模块中粘贴下面的代码,“工程属性-启动对象”要改为“Sub Main”Sub Swap(a() As Long, i As Long, j As Long)Dim t As Longt = a(i)a(i) = a(j)a(j) = tEnd SubSub SelSort(a() As Long, n As Long, incr As Long)Dim i As Long, j As LongFor i = incr To (n - 1)For j = i To incr Step -1If j >= incr And a(j) < a(j - incr) Then Swap a, j, j - incrNext jNext iEnd SubSub ShellSort(a() As Long, n As Long)Dim i As Long, j As LongFor i = n / 2 To 3 Step -2For j = 0 To (i - 1)SelSort a, n - j, iSelSort a, n, 1Next jNext iEnd SubSub Main()Dim i As LongDim a() As LongDoReDim Preserve a(i)a(i) = Val(InputBox("请输入第 " & (i + 1) & " 个数,输入小于0的数结束"))i = i + 1Loop Until a(i - 1) < 0i = UBound(a)If i > 0 ThenReDim Preserve a(i - 1)ShellSort a, UBound(a) + 1For i = 0 To UBound(a)Debug.Print a(i)Next iEnd IfEnd Sub
打开App,查看更多内容
随时随地看视频慕课网APP