已执行多个功能但未正确运行

时间:2014-06-07 07:40:01

标签: c++ sorting

我正在尝试使用c ++进行排序算法,其中我执行插入,冒泡和放大选择排序在一个主要方法。我的目标是打印掉交换内部的复制数量。但是,当我一次执行1个函数并一起执行3时,我无法理解为什么结果看起来有所不同。

同时执行3个功能的结果:

  

冒泡排序:复制的数量 - > 78

     

选择排序:复制的数量 - > 15

     

插入排序:已复制的数量 - > 0

同时执行1个功能的结果:

  

冒泡排序:复制的数量 - > 78

     

选择排序:复制的数量 - > 21

     

插入排序:已复制的数量 - > 57

感谢。

int main()
{       
    cout<<"\n"<<"Sorting Functions"<<"\n\n";

    bubblesort(values, size);
    selectionsort(values, size);
    insertionsort(values, size);

}

void bubblesort(int arr[], int size)
{
//sorts descending
    int countbubble=0; // Initialize number of comparisons at 0
    int copiedbubble = 0; // Initialize number of copied at 0
    bool flag;
    for (int m=size-1; m>0; m--)
    {
        flag=false;
        for (int j=0; j<m; j++)
        { 
            countbubble++; // Increase 1 whenever it loop in.
            if (arr[j] < arr[j+1])
            {
            //swap value at index j with value at index j+1
            swap(arr,j,j+1);
            copiedbubble +=3; // Increase 3     
            flag = true; 
            }
        }   

        if (!flag)  //already sorted; nothing to swap
        break;
    }
    // Display the number of comparison and the number of copied
    cout<<"Bubble sort : Number of comparisons -> "<<countbubble<<" | Number of copied -> "<<copiedbubble<<"\n";        
    //display(arr,size);
}

void selectionsort(int arr[], int size)
{
    //sorts ascending
    int countselection=0; // Initialize number of comparisons at 0
    int copiedselection = 0; // Initialize number of copied at 0
    int max;
    for (int m=size-1; m>0; m--)
    {
        max=0;
        for (int j=1; j<=m; j++)
        {
            countselection++;
            if (arr[j] > arr[max])
            {
                max = j;
            }

        }
        if (m!=max) 
        {    
            int tmp = arr[m];
            arr[m] = arr[max];
            arr[max] = tmp;
            copiedselection +=3; // Increase 3
        }

        //display(arr,size);
    }
    // Display the number of comparison and the number of copied
    cout<<"Selection sort : Number of comparisons -> "<<countselection<<" | Number of copied -> "<<copiedselection<<"\n";
}

void insertionsort(int arr[], int size){
//sorts ascending
        int i, tmp, k;
    int count=0; // Initialize number of comparisons at 0
    int copied = 0; // Initialize number of copied at 0
     for( i = 0; i < size; i++ ) {
        tmp = arr[ i ];
        k = i - 1;
        count++;
        while( k >= 0 && tmp < arr[k] ) { 
            arr[ k+1 ] = arr[ k ];
            k--;
            copied +=3;
         }
        arr[ k+1 ] = tmp;


      }//display(arr,size);
// Display the number of comparison and the number of copied
    cout<<"Insertion sort : Number of comparisons -> "<<count<<" | Number of copied -> "<<copied<<"\n";
}

1 个答案:

答案 0 :(得分:2)

这是因为插入排序不会对已排序的列表执行任何复制。因此,如果您首先使用选择排序对列表进行排序,则会因为此效果而看到0个副本。单独进行将实际按照您的意愿进行排序。

回想一下插入排序的复杂性在排序列表上是O(n)(最好的情况)。

相关问题