C 使用指针对数组进行排序

C 使用指针对数组进行排序,c,C,我正在尝试使用指针按降序对数组中的数字进行排序。我现在所拥有的只是再次打印出数字,而没有对它们进行排序。如何获得对数字进行排序的函数 void new_sort(int nums[], int count) { int round; int i; int inorder; int temp; int *num_ptr = nums; inorder=0; for (round = count -1; (round>0)&&(!inorder);

我正在尝试使用指针按降序对数组中的数字进行排序。我现在所拥有的只是再次打印出数字,而没有对它们进行排序。如何获得对数字进行排序的函数

void new_sort(int nums[], int count) {
 int round; 
 int i;
 int inorder;

  int temp;
  int *num_ptr = nums;

  inorder=0;

  for (round = count -1; (round>0)&&(!inorder); round--) {
     inorder=1;
     for(i=0; i<round; i++) {
        if (*num_ptr<*(num_ptr+1)) {
           inorder = 0;
           temp = *num_ptr;
           *num_ptr = *(num_ptr+1);
           *(num_ptr+1) = temp;
        }
    }
  }
}
void new_排序(int nums[],int count){
整数轮;
int i;
int有序;
内部温度;
int*num_ptr=nums;
顺序=0;
对于(round=count-1;(round>0)和(!inoorder);round--){
顺序=1;

对于(i=0;i您只查看
*num\u ptr
*(num\u ptr+1)
。您应该使用
i
或“移动”指针随您移动。

正如John3136指出的,您只检查第一个元素比下一个元素小。我假设您不想使用随机数组索引,所以我使用了一种使用指针的解决方案。我并没有对您的代码做太多更改,我只是使用指针,而不是您使用的计数器。我用h是一个数组,我认为代码的其他部分工作得很好

void new_排序(int*const nums,int count){
int*const end_ptr=nums+计数;
整数*圆形;
int有序;
内部温度;
顺序=0;
int*num_ptr;
对于(round=end_ptr-1;round!=nums&&(!inoder);round--){
顺序=1;
for(num_ptr=nums;num_ptr!=round;num_ptr++){
如果(*num_ptr<*(num_ptr+1)){
顺序=0;
温度=*num_ptr;
*num_ptr=*(num_ptr+1);
*(num_ptr+1)=温度;
}
}
}
}
int main()
{
int nums[]={10,4,7,5,4,3,2,7,8,9,11,23,1};
新的排序(nums,sizeof(nums)/sizeof(*nums));
返回0;
}
希望这会有所帮助。
干杯!

根据您的设计意图,有几种不同的方法可以做到这一点,我们无法确定,因为没有评论。例如,
num_ptr
应该指向什么?值得一看。