3

バブルソートとランダム化クイックソートの2つのソートアルゴリズムの実行時比較を行いたいと思います。私のコードは正常に機能しますが、いくつかの原始的な手法を使用していると思います。「時計」の計算はで行わintれるため、時間をマイクロ秒で取得しようとしても、20000.000マイクロ秒のようになります。

コード:バブルソート:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
int bubblesort(int a[], int n);

int main()
{
int arr[9999], size, i, comparisons;
clock_t start;
clock_t end;
float function_time;    

printf("\nBuBBleSort\nEnter number of inputs:");
scanf("%d", &size);
//printf("\nEnter the integers to be sorted\n");
for(i=0;i<size;i++)
    arr[i]= rand()%10000;

start = clock();    
comparisons= bubblesort(arr, size);
end = clock();
/* Get time in milliseconds */
function_time = (float)(end - start) /(CLOCKS_PER_SEC/1000000.0);

printf("Here is the output:\n");
for(i=0;i<size ;i++)
    printf("%d\t",arr[i]);
printf("\nNumber of comparisons are %d\n", comparisons);
printf("\nTime for BuBBle sort is: %f micros\n ", function_time);

return 0;
}


  int bubblesort(int a[], int n)
{
bool swapped = false;
int temp=0, counter=0;

for (int j = n-1; j>0; j--)
    {
        swapped = false;
        for (int k = 0; k<j; k++) 
            {
                counter++;
                if (a[k+1] < a[k]) 
                {
                    //swap (a,k,k+1)
                    temp= a[k];
                    a[k]= a[k+1];
                    a[k+1]= temp;
                    swapped = true;
                }
            }
        if (!swapped)
            break;

    }
return counter;
}

サンプル出力:

BuBBleSort
入力数を入力してください:2000
BuBBleソートの時間は:20000.000000マイクロ

クイックソート:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int n, counter=0;
void swap(int *a, int *b)
{
int x;
x = *a;
*a = *b;
*b = x;
}

void quicksort(int s[], int l, int h)
{
int p; /* index of partition */
if ((h- l) > 0) 
{
    p= partition(s, l, h);
    quicksort(s, l, p- 1);
    quicksort(s, p+ 1, h);
}
}

int partition(int s[], int l, int h)
{

int i;
int p; /* pivot element index */
int firsthigh; /* divider position for pivot element */

p= l+ (rand())% (h- l+ 1);
swap(&s[p], &s[h]);
firsthigh = l;

for (i = l; i < h; i++)
if(s[i] < s[h]) 
    {
        swap(&s[i], &s[firsthigh]);
        firsthigh++;
    }

swap(&s[h], &s[firsthigh]);

return(firsthigh);
}  

int main()
{
int arr[9999],i;
clock_t start;
clock_t end;
float function_time;    


printf("\nRandomized Quick Sort");
printf("\nEnter the no. of elements…");
scanf("%d", &n);
//printf("\nEnter the elements one by one…");

for(i=0;i<n;i++)
    arr[i]= rand()%10000;

start = clock();    
quicksort(arr,0,n-1);
end = clock();
/* Get time in milliseconds */
function_time = (float)(end - start) / (CLOCKS_PER_SEC/1000.0);



printf("\nCounter is %d\n\n", counter);
printf("\nAfter sorting…\n");

for(i=0;i<n;i++)
    printf("%d\t",arr[i]);

printf("\nTime for Randomized Quick Sort is: %f ms\n", function_time);

return 0;
}

サンプル出力:

ランダム化されたクイックソート
番号を入力します。要素の数…9999
ランダム化されたクイックソートの時間は次のとおりです:0.000000ミリ秒

ご覧のとおり、Quicksortは、Bubblesortよりもはるかに大きな入力サイズを使用しても、私の手法では実行時間を表示しません。

誰かが実行時の比較のその部分でそれを洗練するのを手伝ってもらえますか?

ps:コードはオンラインソースから自由に借用されています

4

1 に答える 1