freebasic随即产生n(n>=3)个1至100的自然数存入数组a中,然后把数组a进行升序排序。

接着输入一个x,将其插入数组a中,使其依然有序。(用子程序实现)!!

Sub quicksort (arr() As Integer, leftN As Integer, rightN As Integer)
Dim pivot As Integer, leftNIdx As Integer, rightNIdx As Integer
leftNIdx = leftN
rightNIdx = rightN
If (rightN - leftN) > 0 Then
pivot = (leftN + rightN) / 2
While (leftNIdx <= pivot) And (rightNIdx >= pivot)
While (arr(leftNIdx) < arr(pivot)) And (leftNIdx <= pivot)
leftNIdx = leftNIdx + 1
Wend
While (arr(rightNIdx) > arr(pivot)) And (rightNIdx >= pivot)
rightNIdx = rightNIdx - 1
Wend
Swap arr(leftNIdx), arr(rightNIdx)
leftNIdx = leftNIdx + 1
rightNIdx = rightNIdx - 1
If (leftNIdx - 1) = pivot Then
rightNIdx = rightNIdx + 1
pivot = rightNIdx
Elseif (rightNIdx + 1) = pivot Then
leftNIdx = leftNIdx - 1
pivot = leftNIdx
End If
Wend
quicksort arr(), leftN, pivot - 1
quicksort arr(), pivot + 1, rightN
End If
End Sub
Randomize Timer
Dim n As Ubyte,x As Ubyte,i As Ubyte, a() As Integer
Input "请输入N:",n
Redim a(n)
Print "原顺序:"
For i=0 To n-1
a(i)=Rnd*99+1
Print a(i),
Next
quicksort a(),0,n-1
Print "排序后顺序:"
For i=0 To n-1
Print a(i),
Next
Input "请插入值x:",a(n)
quicksort a(),0,n
Print "再排序后:"
For i=0 To n
Print a(i),
Next

Do
Loop Until Inkey$=" " '空格键退出
======
请输入N:100
原顺序:
46 12 99 45 89
12 51 10 57 69
64 68 16 23 80
88 71 19 46 8
64 93 58 56 83
87 41 9 19 57
29 86 48 55 78
88 29 83 98 45
91 55 42 33 84
53 7 76 70 40
15 32 80 77 47
93 67 10 12 18
47 81 25 98 56
80 51 71 59 17
84 98 95 58 98
60 58 78 48 97
61 59 60 53 76
70 65 40 73 73
74 94 80 94 36
13 85 45 66 47
排序后顺序:
7 8 9 10 10
12 12 12 13 15
16 17 18 19 19
23 25 29 29 32
33 36 40 40 41
42 45 45 45 46
46 47 47 47 48
48 51 51 53 53
55 55 56 56 57
57 58 58 58 59
59 60 60 61 64
64 65 66 67 68
69 70 70 71 71
73 73 74 76 76
77 78 78 80 80
80 80 81 83 83
84 84 85 86 87
88 88 89 91 93
93 94 94 95 97
98 98 98 98 99
请插入值x:3
再排序后:
3 7 8 9 10
10 12 12 12 13
15 16 17 18 19
19 23 25 29 29
32 33 36 40 40
41 42 45 45 45
46 46 47 47 47
48 48 51 51 53
53 55 55 56 56
57 57 58 58 58
59 59 60 60 61
64 64 65 66 67
68 69 70 70 71
71 73 73 74 76
76 77 78 78 80
80 80 80 81 83
83 84 84 85 86
87 88 88 89 91
93 93 94 94 95
97 98 98 98 98
99
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-02-04
居然还有人会写freebasic的快速排序,不过这类题主只可能是要抄个简单的答案。你不是浪费了好代码。