编写程序实现(1)用泡法将一个数组排成升序的SUB1函数:(2)在升序数组中插入个数,并且保持该数组仍为升序数组的 SUB2 函数
时间: 2024-05-05 10:18:49 浏览: 14
SUB1函数的实现可以参考下面的代码:
```
void bubble_sort(int array[], int length) {
int temp;
for (int i = 0; i < length - 1; i++) {
for (int j = 0; j < length - i - 1; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
```
而SUB2函数的实现可以参考下面的代码:
```
void insert_sorted_array(int array[], int length, int num) {
int i = length - 1;
while (i >= 0 && array[i] > num) {
array[i + 1] = array[i];
i--;
}
array[i + 1] = num;
}
```
这两个函数可以结合使用来完成你所描述的任务。
相关问题
编写子函数:(1)用冒泡法将一个数据组成升序的函数--SUB1;(2)在升序数组中插入一个数,并且保持该数组仍为升序数组的函数--SUB2。主函数:(1)输入任意10个正整数给数组;(2)调用SUB1对数组进行排序;(3)从键盘输入一个正整数,调用SUB2将其插入该数组
下面是代码实现:
```c
#include <stdio.h>
void SUB1(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void SUB2(int arr[], int n, int x) {
int i = n - 1;
while (i >= 0 && arr[i] > x) {
arr[i + 1] = arr[i];
i--;
}
arr[i + 1] = x;
}
int main() {
int arr[10];
printf("请输入10个正整数:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
SUB1(arr, 10);
printf("排序后的数组为:\n");
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n请输入一个正整数:\n");
int x;
scanf("%d", &x);
SUB2(arr, 10, x);
printf("插入后的数组为:\n");
for (int i = 0; i < 11; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
解释一下:
`SUB1`函数实现了冒泡排序,将给定的数组升序排列。
`SUB2`函数实现了在升序数组中插入一个数,并且保持该数组仍为升序数组的功能。具体实现是先从数组末尾开始向前遍历,找到第一个比要插入的数小的位置,然后将该位置后面的所有元素都往后移一位,最后将要插入的数插入到该位置后面。
在主函数中,先输入10个正整数,调用`SUB1`对数组进行排序,然后从键盘输入一个正整数,调用`SUB2`将其插入该数组,最后输出插入后的数组。
将两个有序数组合并成一个有序数组,采用插入法,用vb6.0实现
以下是VB6.0实现的代码:
```
Private Sub MergeArrays(arr1() As Integer, arr2() As Integer, result() As Integer)
Dim i As Integer, j As Integer, k As Integer
i = 0: j = 0: k = 0
Do While i <= UBound(arr1) And j <= UBound(arr2)
If arr1(i) < arr2(j) Then
result(k) = arr1(i)
i = i + 1
Else
result(k) = arr2(j)
j = j + 1
End If
k = k + 1
Loop
If i <= UBound(arr1) Then
For m = i To UBound(arr1)
result(k) = arr1(m)
k = k + 1
Next
End If
If j <= UBound(arr2) Then
For m = j To UBound(arr2)
result(k) = arr2(m)
k = k + 1
Next
End If
End Sub
```
这段代码接受两个有序数组arr1和arr2作为参数,并将它们合并成一个新的有序数组result。该算法使用插入法,从arr1和arr2中选择一个较小的元素插入到result数组中,直到其中一个数组被遍历完。然后将剩余的元素追加到result数组中,最终得到一个有序的结果数组。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)