【问题标题】:Sorting an array with pointers in C在 C 中使用指针对数组进行排序
【发布时间】:2018-10-04 21:34:49
【问题描述】:

在代码中应该发生的是用户输入数组的数字,然后使用函数以正确的顺序使用指针对数组进行排序。问题是,输出没有按预期输出,我不知道我做错了什么。任何帮助,将不胜感激。谢谢!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MINIMUM_QUANTITY  2
#define MAXIMUM_QUANTITY  10
#define QUIT              0

int quantityValue();
float obtainInfo(int data);
float dataSort(float array1[], float newArray[]);

int main()
{
   int   quantity;
   float *fdata,

   while(quantityValue(quantity)) != QUIT)
   {
      if (fdata = (float*)malloc(sizeof(*fdata) * quantity) == NULL)
      {  
         exit(0);
      }

      obtainInfo(fdata, quantity);
      dataSort(fdata, quantity);

      free(fdata);
   }
   return 0;
}

int quantityValue()
{
   int choice;

   do
   {
      printf("\nHow many numbers are there?");
      scanf("%d", &choice);
   }
   while(choice > MINIMUM_QUANTITY || choice < MAXIMUM_QUANTITY);
   return choice;
}

float obtainInfo(int data)
{
   float array[data],
         value;

   for(counter = 0; counter <= array[data]; counter++)
   {
      printf("\nvalue %d", counter + 1);
      scanf("%f", value);     
      array[data] = value;
      array++;

   return array[data];
}

float dataSort(float array1[], float newArray[])
{
   int *startOfData
       *data
       *startOfSort
       *sort
       *biggestNumber
       temp;

   if(p_sort = (float*)malloc(sizeof(*p_sort) == NULL)
   {
      exit(0);
   }

   for(startOfData = array1[]; startOfData < biggestNumber; startOfData++
   {
      for(startOfSort = newArray; startOfSort < startOfData; startOfSort++)
      {
         temp = *data; 
         *data = *sort;
         *sort = temp;

    free(p_sort)
    return;
}

【问题讨论】:

  • 哦,等等,我刚刚注意到我的一个函数中缺少一些代码。我很抱歉。
  • 编译器给出了许多警告和错误。
  • 您使用函数的方式甚至与您在main() 中的使用方式不匹配。例如,您如何期望quanityValue() 将其结果保存在本地quantity 中?你没有像 quantity = quanityValue(); 这样的东西 dataSort() 的签名也是不匹配的。这甚至可以编译吗?
  • @infixed:不,它不能编译。第一个标记的错误是第 18 行的括号不匹配。Paul,我们需要查看您的真实代码。
  • 考虑逐步开发它。保存您当前的工作,但开始一个新文件,其中只有main()quanityValue(),然后使用调试器来使其工作。一个你拥有它,加载你的数组,然后调试它,在它工作之后,进行排序。您不必一次完成所有操作。

标签: c arrays pointers


【解决方案1】:

要对数组进行排序,有多种算法可以帮助您,其中最常用的一种是快速排序。下面是对数组进行排序的完整代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Swap(int array[], int i, int j)
{
  int temp;
  temp = array[i];
  array[i] = array[j];
  array[j] = temp;
}


void QuickSort(int array[], int beg, int end)
{
  int pivot, k, b, e;

    while(beg < end)
    {
      b = beg; k = (beg+end)/2; e = end;
      pivot = array[k];

      while(1)
      {
        while((b <= e) && (array[b] <= pivot)) b++;
        while((b <= e) && (array[e] > pivot)) e--;

        if(b > e) break;


      Swap(array, b, e);

        if(k == e) k = b;

        b++; e--;

      }
      array[k]=array[e];
      array[e]=pivot;
      e--;

      if((e-beg)<(end-b))
      {
        QuickSort(array, beg, e);
        beg = b;
      }
      else
      {
        QuickSort(array, b, end);
        end = e;
      }

    }

  return ;
}

void display(int * array, int n)
{
 for(int i = 0; i < n ; i++)
 {
   printf("%d\n", array[i]);
 }
}


int main()
{
  int array[] = {1,2,77,5,42,33,0,4,12,21,55}; //array to sort, instead you can use a function that fills an array, and then pass it to QuickSort function

  QuickSort(array,0,sizeof(array)/sizeof(int));

  display(array, sizeof(array)/sizeof(int));


  return 0;
}

【讨论】:

    猜你喜欢
    • 2013-08-05
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 2014-05-16
    • 1970-01-01
    相关资源
    最近更新 更多