利用指针交换数组中多个数的顺序
时间: 2023-05-11 09:05:28 浏览: 71
可以使用指针来交换数组中多个数的顺序。具体实现方法是,定义两个指针,一个指向数组的第一个元素,另一个指向数组的最后一个元素,然后交换它们所指向的元素,再将指针向中间移动,直到它们相遇为止。以下是示例代码:
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void reverse(int *arr, int size) {
int *start = arr;
int *end = arr + size - 1;
while (start < end) {
swap(start, end);
start++;
end--;
}
}
注意,这里的代码只是一个示例,实际应用中需要根据具体情况进行修改和优化。
相关问题
设计一个函数,利用冒泡法对若干个字符串进行排序,利用C语言,使用指针数组存储字符串,
这是一个有效问题。冒泡排序是一种排序算法,其基本思想是对相邻的元素进行比较和交换,以此将小的元素逐步“冒泡”到数组的前面。对于字符串的排序,可以利用指针数组存储多个字符串,然后按照字符串的字典序进行比较和交换。以下是一个利用冒泡排序对字符串进行排序的C语言示例代码:
```
#include <stdio.h>
#include <string.h>
#define MAX_STR_LEN 100
#define MAX_STR_NUM 10
void bubble_sort(char *str[], int n)
{
int i, j;
char *temp;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1 - i; j++)
{
if (strcmp(str[j], str[j+1]) > 0)
{
temp = str[j];
str[j] = str[j+1];
str[j+1] = temp;
}
}
}
}
int main()
{
char str[MAX_STR_NUM][MAX_STR_LEN];
char *str_ptr[MAX_STR_NUM];
int n, i;
printf("Enter the number of strings: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter string %d: ",i+1);
scanf("%s", str[i]);
str_ptr[i] = str[i];
}
bubble_sort(str_ptr, n);
printf("Sorted strings:\n");
for (i = 0; i < n; i++)
{
printf("%s\n", str_ptr[i]);
}
return 0;
}
```
该程序通过定义一个指针数组 `str_ptr` 来存储多个字符串,然后使用 `strcmp()` 函数来比较字符串的大小关系,完成字符串的排序。在程序运行时,先输入要排序的字符串数量 `n`,然后输入 `n` 个字符串,程序将自动按照字典序递增的顺序对字符串进行排序,并输出排序后的字符串。
) 编写一个利用冒泡排序法对多个数组中的元素,按照指定数组值排序的函数 myBubbleSort。具体要求为: a) 该函数是一个无返回值的函数; b) 该函数的输入参数为六个:数组元素个数,排序方向、整型数组 1、字符型指针数组、整型数组 2、浮点数组 c) 该函数头为: void myBubbleSort(int num, int dirct,int iarr1[ ], char *country[ ], int iarr2[ ], float farr[ ]) d) 要求: 在 dirct = 1 时, 在该函数内部按照数组 iarr1 元素从大到小的顺序,对所有的四个数组: iarr1[ ], *country[ ], iarr2[ ], farr[ ] 进行排序。 在 dirct = -1 时, 在该函数内部按照数组 iarr1 元素从小到大的顺序,对所有的四个数组: iarr1[ ], *country[ ], iarr2[ ], farr[ ] 进行排序。
下面是一个符合上述要求的 myBubbleSort 函数的示例代码:
```
void myBubbleSort(int num, int dirct, int iarr1[], char *country[], int iarr2[], float farr[]) {
int i, j;
int tempInt;
char *tempChar;
float tempFloat;
for(i = 0; i < num - 1; i++) {
for(j = 0; j < num - 1 - i; j++) {
if((dirct == 1 && iarr1[j] < iarr1[j+1]) || (dirct == -1 && iarr1[j] > iarr1[j+1])) {
// 对 iarr1 数组进行冒泡排序
tempInt = iarr1[j];
iarr1[j] = iarr1[j+1];
iarr1[j+1] = tempInt;
// 对 country 数组进行冒泡排序
tempChar = country[j];
country[j] = country[j+1];
country[j+1] = tempChar;
// 对 iarr2 数组进行冒泡排序
tempInt = iarr2[j];
iarr2[j] = iarr2[j+1];
iarr2[j+1] = tempInt;
// 对 farr 数组进行冒泡排序
tempFloat = farr[j];
farr[j] = farr[j+1];
farr[j+1] = tempFloat;
}
}
}
}
```
该函数使用了冒泡排序的算法,通过双重循环对四个数组进行排序。在每一次循环中,通过比较 iarr1 元素的大小,根据参数 dirct 的取值决定冒泡排序的方向。如果 dirct 等于 1,则按照从大到小的顺序排序,否则按照从小到大的顺序排序。在对四个数组进行排序时,使用了交换变量的方法来实现元素的交换。最后,函数中没有返回值,而是直接修改了输入参数所对应的数组。
相关推荐
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)