如何使用C中的指针对未知大小的数字数组进行排序

时间:2016-04-30 15:06:11

标签: c arrays sorting pointers

这是我到目前为止提出的代码。这可能不是扫描由空格分隔的数组的最佳方法。所以我需要做的是将推入的数组按升序排序并打印出来。希望有人可以帮助我!

int main()

char vect1[25];
char *num1;


//First Vector
printf("Enter first Vector seperated by spaces\n");
fgets(vect1, 25, stdin);
printf("The unsorted vector is:\n");

double v1[25];
int count1=0;
num1 = strtok(vect1, " ");

while (num1 != NULL)
{
    sscanf(num1, "%lf", &v1[count1]);
    printf("%.2f\n", v1[count1]); 
    num1 = strtok(NULL, " ");
    count1++;
}

printf("Size of Array= %d\n\n", count1);

输出是:

Enter first Vector separated by spaces

用户输入向量(例如5 4 9 3 8 2 1)

5
4
9
3
8
2
1

size of array= 7

2 个答案:

答案 0 :(得分:0)

冒泡排序,对于小型阵列来说简单而快速。

int main()

char vect1[25];
char *num1;
char swap;

//First Vector
printf("Enter first Vector seperated by spaces\n");
fgets(vect1, 25, stdin);
printf("The unsorted vector is:\n");

double v1[25];
int count1=0;
num1 = strtok(vect1, " ");

while (num1 != NULL)
{
    sscanf(num1, "%lf", &v1[count1]);
    printf("%.2f\n", v1[count1]); 
    num1 = strtok(NULL, " ");
    count1++;
}
  for (int c = 0 ; c < ( count1 - 1 ); c++)
  {
    for (int d = 0 ; d <  - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }

答案 1 :(得分:0)

这是你编写的添加了quicksort()函数的代码,它将为你返回一个已排序的数组。我只是选择了quicksort,因为我喜欢它,但可以使用任何排序算法。

我添加了所有相关函数以使quicksort正常工作,并且我已将所有函数原型添加到main()之上。我还添加了一个print_array函数,它以与打印它们相同的格式打印数组中的元素。

另外,我添加了一行

#define VECSIZE 25

在文件顶部的include语句之后。您经常使用值25作为常量,因此如果您想将值更改为另一个数字,则必须在许多不同的位置更改它。现在,如果你想改变矢量的大小,你所要做的就是改变VECSIZE的值。

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

#define VECSIZE 25 

/* function prototypes */
void quicksort(double *vector, int low, int high);
int partition(double *vector, int low, int high);
void swap(double *array, int i, int j);
void print_array(double *array, int size);

int main() {

  char vect1[VECSIZE];
  char *num1;


  //First Vector
  printf("Enter first Vector seperated by spaces\n");
  fgets(vect1, VECSIZE, stdin);

  double v1[VECSIZE];
  int count1=0;
  num1 = strtok(vect1, " ");

  while (num1 != NULL)
  {
    sscanf(num1, "%lf", &v1[count1]);
    num1 = strtok(NULL, " ");
    count1++;
  }

  printf("The unsorted vector is: \n");
  print_array(v1, count1);
  printf("Size of Array= %d\n\n", count1);

  quicksort(v1, 0, count1-1); // count1-1 is the index of the last  element in the array 
  printf("The sorted vector is: \n");
  print_array(v1, count1);
}

void print_array(double *array, int size){
  int i;
  for (i = 0; i < size; i++) 
    printf("%.2f\n", array[i]); 
  printf("\n");
}

/*
  x and y are indices into the array, and the values
  at those indexs will be swapped
*/
void swap(double *array, int x, int y) {
  int placeholder = array[x];
  array[x] = array[y];
  array[y] = placeholder;
}

/*
  Sorts a single element in the array and returns its
  sorted index.
 */
int partition(double *array, int low, int high) {
  int i = low-1;
  int j = low;
  double pivot = array[high];  
  for (j; j < high; j++) {
    if (array[j] <= pivot) {
      i++;
      swap(array, i, j);
    }
  }
  i++;
  swap(array, i, high);
  return i;
}

/*
  recursively sorts an array by sorting the values in the
  array that are left of 'mid' and then sorting the values
  that are greater than 'mid'. The brains of this sorting 
  algorithm is in the partition function.
 */
void quicksort(double *array, int low, int high) {
  int mid;
  if (low < high) {
    mid = partition(array, low, high);
    quicksort(array, low, mid-1);
    quicksort(array, mid+1, high);
  }
}