首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选择排序和冒泡排序之间的比较次数一直保持不变

选择排序和冒泡排序之间的比较次数一直保持不变
EN

Stack Overflow用户
提问于 2013-02-19 05:58:43
回答 1查看 1.4K关注 0票数 0

我已经编写了这个程序来比较使用选择和冒泡排序对随机数进行排序所需的操作数量。然而,这些数字一直都是一样的,我找不到我的代码哪里出错了。

代码语言:javascript
复制
static int num_comps;   

public static void main(String[] args) 
{
    Random rnd = new Random();

    // max size of array
    // number of N inputs
    int array_size = 32768;
    int num_datasets = 12;

    // allocate array once to the max size
    int[] vals = new int[array_size];

    // temp array with allocated array to max size
    int[] tvals = new int[array_size];

    // array to hold operation counts
    int[] op_counts = new int[num_datasets];
    int[] op_counts2 = new int[num_datasets];

    // array to hold the size of each array
    //
    int[] arraySizes = new int[num_datasets];

    int i;
    int j;
    int sz;

    for (i = 0, sz = 16; i < num_datasets; i++, sz *= 2)
        arraySizes[i] = sz;

    for (int iter = 0; iter < num_datasets; iter++)
    {
        int curr_size = arraySizes[iter];

        // load array with random values
        //
        for (i = 0; i < curr_size; i++)
            vals[i] = rnd.nextInt(4999);

        for (i = 0; i < curr_size; i++)
            tvals[i] = vals[i];

        // run the bubble sort algorithm
        //
        num_comps = 0;
        bubbleSort(tvals, curr_size);
        op_counts[iter] = num_comps;
        //System.out.println("Num comps at " + iter + " is " + num_comps);

        // run the selection-sort algorithm
        num_comps = 0;
        selectionSort(tvals, curr_size);
        op_counts2[iter] = num_comps;
        //System.out.println("Num comps at " + iter + " is " + num_comps);
    }

    System.out.println("Operation Counts (N vs. op Count): ");
    for (int k = 0; k < num_datasets; k++)
        System.out.println(arraySizes[k] + "\t\t" + op_counts[k] + "\t\t" + op_counts2[k]);
}

static void bubbleSort(int vals[], int curr_size)
{
    int temp;
    for (int i = 0; i < curr_size - 1; i++)
    {
        for (int j = 0; j < curr_size - i - 1; j++)
        {
            // swap
            num_comps = num_comps + 1;
            if (vals[j+1] < vals[j])
            {
                temp = vals[j];
                vals[j] = vals[j+1];
                vals[j+1] = temp;
            }
        }
    }
}

static void selectionSort(int vals[], int curr_size)
{
    int temp;
    for(int i=0; i<curr_size - 1; i++)
     {
        for(int j=i+1; j<curr_size; j++)
        {
            num_comps = num_comps + 1;
            if(vals[i] > vals[j] )
            {
                temp = vals[j];
                vals[j] = vals[i];
                vals[i] = temp;
            }
        }
     }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-19 06:14:02

您的selection sort算法不会搜索列表中的最低值。并随后将其与外部循环的索引交换。

你应该这样做:

代码语言:javascript
复制
static void selectionSort(int vals[], int curr_size)
{
    int temp;
    for(int i=0; i<curr_size - 1; i++)
    {
        int lowest = i;
        for(int j=i+1; j<curr_size; j++)
        {
            num_comps = num_comps + 1;
            if(vals[lowest] > vals[j] )
            {
                lowest = j;
            }
        }

        // swap lowest with current index
        temp = vals[lowest];
        vals[lowest] = vals[i];
        vals[i] = temp;
     }
}

(当然,这可以进一步优化)这个算法的优势不是比较的数量,而是交换的数量(我认为这是最小的)。

你的bubble sort算法在我看来没问题。

两者都有相同的两个循环,因此比较当前实现的计数确实会产生相同的值。但是,我认为您可以优化冒泡排序,以便更早地停止(当没有发现交换时)。同样,排序算法的强度取决于所使用的算法,并且不一定是最小数量的比较。因此,为您的特定任务使用正确的算法,从而规避特定于任务的高成本操作,是很重要的!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14946049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档