易语言的快速排序怎么写
时间: 2023-02-20 15:13:16 浏览: 132
易语言的快速排序代码如下:
```
Begin Global
Var Arr[100]
End Global
Function QuickSort(L, R)
Var i, j, x, t
i = L
j = R
x = Arr[(L + R) / 2]
While (i <= j)
While (Arr[i] < x)
i = i + 1
End While
While (Arr[j] > x)
j = j - 1
End While
If (i <= j) Then
t = Arr[i]
Arr[i] = Arr[j]
Arr[j] = t
i = i + 1
j = j - 1
End If
Wend
If (L < j) Then
QuickSort(L, j)
End If
If (i < R) Then
QuickSort(i, R)
End If
End Function
Begin Program
Var i, N
Input "请输入数组的大小:", N
For i = 0 To N - 1
Input "请输入第" & i + 1 & "个数:", Arr[i]
Next i
QuickSort(0, N - 1)
For i = 0 To N - 1
Print Arr[i]
Next i
End Program
```
这是一个简单的易语言快速排序的代码示例。
相关问题
易语言快速排序怎么写
易语言的快速排序算法可以使用递归的方法实现。首先,选择一个中间数作为基准数,然后把数组分为两部分,比基准数小的数放在左边,比基准数大的数放在右边。对于左边和右边的两个数组分别递归执行快速排序。最终数组就已经有序了。
代码如下:
```
Sub QuickSort(a() As Integer, l As Integer, r As Integer)
If l < r Then
Dim i As Integer, j As Integer, x As Integer
i = l
j = r
x = a(l)
While i < j
While i < j And a(j) >= x
j -= 1
End While
If i < j Then
a(i) = a(j)
i += 1
End If
While i < j And a(i) < x
i += 1
End While
If i < j Then
a(j) = a(i)
j -= 1
End If
End While
a(i) = x
QuickSort a(), l, i - 1
QuickSort a(), i + 1, r
End If
End Sub
```
阅读全文