简体   繁体   English

如何排序指针数组所指向的数组?

[英]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: 但排序不佳...

  • Before: 3 9 3 7 之前:3 9 3 7
  • After: 3 3 9 7 之后:3 3 9 7
  • Before: 3 6 1 7 之前:3 6 1 7
  • After: 3 1 6 7 之后:3 1 6 7
  • Before: 4 9 7 1 7 之前:4 9 7 1 7
  • After: 4 9 7 1 7 之后:4 9 7 1 7

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM