【问题标题】:Insertion sort - counting of comparisons and swaps in C插入排序 - C 中比较和交换的计数
【发布时间】:2015-12-14 19:07:39
【问题描述】:

如何计算插入排序中的比较和交换次数?我有 10 个随机数的数组。如果有人帮助我如何在这个程序中输入 20、50、100、200、500、1000、2000 和 5000 个随机数,我会非常高兴。这个问题我想了很久,还是没找到解决办法。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>            
int main()
{

    int array[10];
    int i, j, n, temp;
    n = 10;
    for (i = 0; i < n; i++)
        array[i] = rand();

    /*Sort*/
    for (i = 1; i < n; i++) {
        j = i;
        while ((j > 0) && (array[j - 1] > array[j])) {
            temp = array[j - 1];
            array[j - 1] = array[j];
            array[j] = temp;
            j--;
        }
    }
    /* Print */
    printf("Sorted Array\n");
    for (i = 0; i < n; i++)
        printf("%d \n", array[i]);
    return 0;
}

【问题讨论】:

  • “如何计算插入排序中的比较和交换次数”。每次进行比较或交换时增加计数器?
  • 你能帮我写代码吗?

标签: c count comparison swap insertion-sort


【解决方案1】:

这是一个代码,您可以使用它找出插入排序中比较和交换的总数

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

   int main()
   {

      int array[10];
      int i, j, n, temp,no_swap=0,comp=0;//variables to find out swaps and comparisons 
      n = 10;
      for (i = 0; i < n; i++)
      array[i] = rand(10);

/*Sort*/
      for (i = 1; i < n; i++) {
      j = i;
      comp++;
      while ((j > 0) && (array[j - 1] > array[j])) {
            if(array[j-1]>array[j]){
            comp++;
        }
        temp = array[j - 1];
        array[j - 1] = array[j];
        array[j] = temp;
        j--;

        no_swap++;//increment swap variable when actually swap is done
    }
}
/* Print */

      printf("\nNo of swaps = %d",no_swap);
      printf("\nNo of comparisions  = %d",comp);
      printf("\nSorted Array\n");
      for (i = 0; i < n; i++)
          printf("%d \n", array[i]);
      return 0;
 } 

【讨论】:

  • 感谢您的帮助。有谁知道如何将 20 然后 50、100、200、500、1000、2000 和 5000 的数字放在那里?我知道我必须使用动态数组,但我不知道如何。我为我糟糕的英语和我可能愚蠢的问题感到抱歉,但我是初学者。
  • 只需让用户输入他/她想要多少数组元素,然后在 n 中取该值。并声明具有您想要的最大大小的数组。在您的情况下,它是 5000。然后使用 rand() 函数生成值直到 n。我希望你知道如何接受输入并使用 rand() 函数直到最大值.....
【解决方案2】:

这是使用插入排序对数组进行排序并计算最佳、平均和最差情况的比较次数的代码。

#include<iostream>
using namespace std;
int  insertionsort( int arr[ ], int n)
{
 int i,temp,j;
 int comp=0;

 for( i=1; i<n ; i++ )
{
  temp=arr[i];
  j = i - 1;

   while( j>=0 && temp<arr[j])
  {
   arr[j+1] = arr[j] ;
   j = j-1 ;
   comp++;
  }
  arr[j+1]=temp;
  if(temp>arr[j])
  {
    comp++;
  }
}
  return comp;
}
void display(  int arr[ ], int n, int comp )
{
  cout<<" Elements of array after sorting "<<endl;
  for( int i=0; i<n; i++ )
  {
     cout<<arr[i]<<"  ";
  }
  cout<<endl;
  cout<<" Number of comparisions "<<comp<<endl;

 }
int main()
{
   int size;
   int comp = 0;
   cout << " Enter the size of an array " << endl;
   cin >> size;
   int arr[size];
   int n= sizeof(arr) / sizeof(arr[0]);
   cout<<" Enter the elements of array "<<endl;
   for( int i=0; i<size; i++ )
   {
      cin>>arr[i];
   }
   cout<<" Elements of array before sorting "<<endl;
   for( int i=0; i<size; i++ )
   {
        cout<<arr[i]<<"  ";
   }
   cout<<endl;
   int compairsion = insertionsort( arr, n);
   display(  arr, n, compairson);
   return 0;
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 2015-07-09
    • 2017-08-06
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多