[英]how to sort arrays that pointed by pointers array?
I need to sort arrays that pointed by pointers array with this function (i need only with pointers, without these [ ]) 我需要使用此功能对由指针数组指向的数组进行排序(我只需要使用指针,而无需使用这些[])
void getSort(int** p2a, int arrSizes[]) //p2a is a array of pointers to diffrent arrays
I tried bubble sort: 我尝试了冒泡排序:
if (i < 5)
{
for (j = 1; j < arrSizes[i]; j++) // I started with 1 bcz i dont want to sort the first value
{
for (k = j + 1; k < arrSizes[i]; k++)
{
if (*(*(p2a + i) + j) > *(*(p2a + i) + k))
{
temp = *(*(p2a + i) + j);
*(*(p2a + i) + j) = *(*(p2a + i) + k);
*(*(p2a + i) + k) = temp;
}
}
i++;
}
}
but it not sorting well... exampels: 但排序不佳...
change the if with for solved the problem 更改if与for解决问题
for (i = 0; i < NUM; i++)
{
for (j = 1; j < arrSizes[i]; j++)
{
for (k = j + 1; k < arrSizes[i]; k++)
{
if (*(*(p2a + i) + j) > *(*(p2a + i) + k))
{
temp = *(*(p2a + i) + j);
*(*(p2a + i) + j) = *(*(p2a + i) + k);
*(*(p2a + i) + k) = temp;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.