【问题标题】:Selection sort program in CC中的选择排序程序
【发布时间】:2013-07-02 11:50:14
【问题描述】:

这是其中一个程序,其中一组数字按升序排序,方法是在数组的左点和结尾之间的范围内找到最大的数字,然后将该元素移动到其正确的索引位置切换元素。我的问题是它不是按升序排列的,因为中间的数字没有排序,我想知道如何在程序中使用它。 这是我目前的代码:

#include <stdio.h>                                    /* Library inclusions */ 
#include "genlib.h" 
#include "simpio.h"
#define size 7                                        /* Constants */

void sortArray (int numbers[]);                       /* prototypes */
int indexMax (int numbers[], int low, int high);
void swap (int numbers[], int loc, int loc1);
void getArray (int numbers[]);
void displayArray (int numbers[]);

main()
{
      int numbers[size];
      getArray(numbers); 
      sortArray(numbers ); 
      displayArray (numbers); 
      getchar();
}

void getArray (int numbers[])                         /*Function getArray*/
{
     int i;

     for (i=0; i<size; i++)
     {
         printf ("Enter an integer? ");
         numbers[i]=GetInteger();
     }
}

void displayArray (int numbers[])                     /*Function displayArray*/
{
     int i;

     printf ("\n The sorted list is: \n");
     for (i=0; i< size; i++)
     {
         printf ("%d\t", numbers[i]); 
     }
}

void sortArray (int numbers[])                        /*Function sortArray*/
{
     int i , maxInd;

     for (i=0; i<size;i++)
     {
         maxInd = indexMax (numbers, i, size-1);
         swap (numbers, size-1, maxInd);
     }
}

int indexMax (int numbers[], int low, int high)       /*Function indexMax*/
{
    int i, maxInd;

    maxInd=high;
    for (i=low;i<=high;i++)
    {
        if (numbers[i]>numbers[maxInd]) 
        {
                       maxInd =i;
        }
    }
    return (maxInd);
}

void swap (int numbers[], int loc, int loc1)         /*Function swap*/
{
     int temp;

     temp=numbers[loc];
     numbers[loc]=numbers[loc1];
     numbers[loc1]=temp;
}

非常感谢。 :)

【问题讨论】:

  • 今天承诺:一旦您不是初学者,您将更改显示名称。
  • 您是否尝试过在调试器中逐行单步执行代码,以帮助您查看可能出现的问题?用一个小数组来做,这样就不会花这么长时间。

标签: c arrays stdio selection-sort


【解决方案1】:

你的 SortArray 函数逻辑是错误的。您正在从ilast index 找到maxindex,并将其替换为last index,然后递增i。在第一次迭代中,最大数字在随后的迭代中到达末尾,last index 将仅被选为maxindex,并且数组不会发生任何变化。

相反,您总是需要从第一个索引迭代到比前一个最后一个索引少一个索引。

void sortArray (int numbers[])                        /*Function sortArray*/
{
     int i , maxInd;

     for (i=size-1; i>=0;i--)
     {
         maxInd = indexMax (numbers, 0, i);
         swap (numbers, i, maxInd);
     }
}

【讨论】:

    【解决方案2】:

    indexMax函数中将greater than (&gt;)改为less than (&gt;)

    【讨论】:

    • 非常感谢 :) 但它没有用 :( 但是,Shashwat Kumar 已经解决了这个问题,所以没关系。
    【解决方案3】:

    函数sortArray()可能有问题

    void sortArray (int numbers[])         
    {
         int i 
    
         int maxInd;
    
         for (i=0; i<size;i++)
         {
             maxInd = indexMax (numbers, i, size-i-1);
             swap (numbers, size-1-i, maxInd);
         }
    }
    

    我做了一个小改动,效果很好!!

    【讨论】:

      猜你喜欢
      • 2018-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 2013-03-15
      相关资源
      最近更新 更多