•配列のサイズを100, 1000, 10000, 100000と増やしていって、入れ替えの回数を比べる
という内容の課題に取り組んでいるのですが、
クイックソートによる入れ替えの回数を表示する方法が分かりません><
このプログラムをどのように改良すれば入れ替えの回数を表示できるか教えていただきたいです><
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
# define X 10
# define Y 100/*課題通りサイズを100,1000,10000,100000で結果を試す*/
void quick_sort(int *data, int head, int tail);
int countQ=0;
int main()
{
int b[X][Y];/*クイック用配列*/
int a[X][Y];/*バブル用配列*/
int m,n,i,j,k,l;
unsigned int count;
srand( (unsigned int)time( NULL ) );
int number=sizeof(b)/sizeof(b[0]);
int number2=sizeof(b[0])/sizeof(b[0][0]);
printf("左の回数がバブルソート,右の回数がクイックソートによる\n");
for(n=0;n<number;n++)
{
printf("%d回目:",n+1);
count=0;
countQ=0;
for(m=0;m<number2;m++)
{
b[n][m]=rand();
a[n][m]=b[n][m];
}
/*バブルソート*/
for (j = number2-1; j > 0; j --)
{
for (i = 0; i < j; i ++)
{
if(b[n][i]>b[n][i+1])
{
k=b[n][i];
b[n][i]=b[n][i+1];
b[n][i+1]=k;
count++;
}
}
}
quick_sort(a[n], 0, number2-1);/*クイックソート*/
printf("%ld回 ",count);
printf("%d回\n",countQ);
}
return 0;
}
void quick_sort(int *data, int head, int tail)
{
int x, y;
int s, t;
s = data[head];
x = head;
y = tail;
do
{
while (data[x] < s)
x++;
while (data[y] > s)
y--;
if (x <= y)
{
countQ++;
t = data[x];
data[x] = data[y];
data[y] = t;
x++;
y--;
}
}
while (x <= y);
{
if (head < y)
quick_sort(data, head, y);
if (x < tail)
quick_sort(data, x, tail);
}
}