c编程:对数组中的 n 个数据从小到大排序。 输入格式: 第一行为数据个数 n,1≤n≤20;第二行为 n 个整数。 输出格式: 数组中的 n 个元素按从小到大的顺序输出。
时间: 2024-02-15 21:04:13 浏览: 121
好的,这里给出一个 C 语言程序,可以对数组中的 n 个数据从小到大排序。请注意,程序中并没有处理输入的范围限制,请确保输入符合要求。
```c
#include <stdio.h>
int main() {
int n, temp;
scanf("%d", &n);
int a[20];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
return 0;
}
```
程序中先读入 n 个整数并存放到数组 a 中,然后使用两层循环对数组中的元素进行比较和交换,最后输出排好序的数组。
相关问题
用汇编语言编写:有100个有符号双字节整数,存放在数据区,编程实现从小到大排序。并按顺序输出打印。
下面是一个使用x86汇编语言实现的程序,可以对100个有符号双字节整数进行从小到大的排序,并将结果按顺序输出打印。
```assembly
section .data
array dw 23, -56, 45, -12, 67, -34, 89, -23, 12, -78
dw 43, -87, 54, -32, 76, -65, 98, -54, 21, -43
; 更多数据可以在这里添加
section .text
global _start
_start:
; 计算数组长度
mov ax, word [array_end]
sub ax, word [array]
inc ax
mov bx, ax
mov cx, ax
; 外层循环,执行n-1次
dec cx
outer_loop:
push cx
xor cx, cx
; 内层循环,从第一个元素开始比较,直到倒数第二个元素
inner_loop:
mov ax, cx
add ax, 1
mov dx, ax
mov si, word [array + cx * 2]
mov di, word [array + dx * 2]
; 如果前一个元素比后一个元素大,则交换它们
cmp si, di
jng skip_swap
mov word [array + cx * 2], di
mov word [array + dx * 2], si
skip_swap:
inc cx
cmp cx, bx
jb inner_loop
; 打印数组元素
mov cx, bx
xor bx, bx
print_loop:
mov ax, word [array + bx * 2]
call print_signed_int
inc bx
cmp bx, cx
jb print_loop
; 减少外层循环次数
pop cx
dec cx
cmp cx, 0
jne outer_loop
; 退出程序
mov eax, 1
xor ebx, ebx
int 0x80
; 打印有符号整数
; 参数:ax - 待打印的有符号整数
print_signed_int:
cmp ax, 0
jge positive_int
neg ax
mov ah, 2
mov dx, negative_sign
int 0x21
positive_int:
mov cx, 10
xor bx, bx
xor dx, dx
div cx
push dx
inc bx
cmp ax, 0
jne positive_int
print_loop:
pop dx
add dl, '0'
mov ah, 2
int 0x21
dec bx
cmp bx, 0
jne print_loop
mov dl, 10
mov ah, 2
int 0x21
mov dl, 13
mov ah, 2
int 0x21
ret
section .bss
negative_sign resb 1
section .data
array_end:
```
该程序使用了冒泡排序算法对数组进行排序,然后使用打印有符号整数的子程序逐个输出排序好的整数。在程序开始时,你可以在数据区中添加更多的数据。
用c语言编程,输入10个整数,用选择法对这10个整数按从小到大的顺序排序并输出排序后的结果。
下面是用C语言编写的选择排序算法的实现:
```c
#include <stdio.h>
void selection_sort(int arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n - 1; i++) {
min_idx = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
// Swap the minimum element with the first element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
int main()
{
int arr[10];
int i;
printf("Enter 10 integers: ");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
selection_sort(arr, 10);
printf("Sorted array: ");
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
首先,我们定义了一个 `selection_sort` 函数来实现选择排序算法。该函数接受一个整数数组和其大小作为参数。在函数内部,我们使用两个循环来遍历数组并找到最小元素的索引。一旦找到最小元素,我们将其与第一个元素交换。然后,我们继续遍历剩余的元素,并重复这个过程,直到整个数组被排序。
在 `main` 函数中,我们首先定义一个大小为10的整数数组 `arr`。然后,我们使用一个 `for` 循环来读入10个整数。接下来,我们调用 `selection_sort` 函数来对数组进行排序,并使用另一个 `for` 循环来输出排序后的数组元素。
编译并运行程序,输入10个整数,程序将按从小到大的顺序对它们进行排序并输出结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)