[英]counting swap and comparison of quicksort
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAX_ELEMENTS 1000
#define OFFSET 100
#define MAX_LINES 10
int a[MAX_ELEMENTS] ;
int comp, swap ;
void quicksort(int [],int ,int);
void init_step(void);
void print_step(int);
void print_header(char *);
int sorted(int);
void swap1(int*,int*);
int main(void){
int i;
int n;
srandom(time(0));
print_header("random");
for (n = OFFSET; n <= MAX_ELEMENTS; n += OFFSET) {
init_step();
for (i = 0; i < n; i++) {
a[i] = random() % n ;
}
quicksort(a,0,n-1);
print_step(n);
}
print_header("ascending order");
for (n = OFFSET; n <= MAX_ELEMENTS; n += OFFSET) {
init_step();
for (i = 0; i < n; i++) {
a[i] = i ;
}
quicksort(a,0,n-1);
print_step(n);
}
print_header("descending order");
for (n = OFFSET; n <= MAX_ELEMENTS; n += OFFSET) {
init_step();
for (i = 0; i < n; i++) {
a[i] = n - i ;
}
quicksort(a,0,n-1);
print_step(n);
}
return 0;
}
void init_step(void){
swap = 0; comp = 0;
}
void print_header(char *s) {
printf("%s\n n, comparison, swap, check", s);
printf("\n");
}
void print_step(int n){
printf("%4d, %8d, %8d", n, comp, swap);
if (sorted(n)) {
printf(", sorted\n");
} else {
printf(", unsorted\n");
}
}
int sorted(int n) {
int i;
for (i=0; i < n-1; i++)
if (a[i] > a[i+1]) return 0;
return 1;
}
void swap1(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
swap++;
}
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
comp++;
while(number[j]>number[pivot])
j--;
comp++;
if(i<j){
swap1(&number[i], &number[j]);
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
所以我有這段代碼來計算 c max 元素處快速排序的比較和交換意味着最大數據,偏移量意味着數據數量的增量值。 我要問的是隨機情況是完全計數的,但對於降序和升序情況,比較和交換的值都是一樣的。 並且交換值始終為 0 更具體地說,我將在下面發布結果
random
| n |comparison| swap| check|
|:---: |:--------:|:---:|:----:|
|100 | 316 | 89 |sorted|
|200 | 756 | 244 |sorted|
|300 | 1156 | 375 |sorted|
|400 | 1630 | 552 |sorted|
|500 | 2164 | 745 |sorted|
|600 | 2682 | 932 |sorted|
|700 | 3202 |1125 |sorted|
|800 | 3776 |1351 |sorted|
|900 | 4286 |1539 |sorted|
|1000 | 4732 |1678 |sorted|
ascending order
| n |comparison| swap| check |
|:---:|:--------:|:---:|:-----:|
|100 | 198 | 0 | sorted|
|200 | 398 | 0 | sorted|
|300 | 598 | 0 | sorted|
|400 | 798 | 0 | sorted|
|500 | 998 | 0 | sorted|
|600 | 1198 | 0 | sorted|
|700 | 1398 | 0 | sorted|
|800 | 1598 | 0 | sorted|
|900 | 1798 | 0 | sorted|
|1000 | 1998 | 0 | sorted|
descending order
| n |comparison| swap| check |
|:---:|:--------:|:---:|:-----:|
|100 | 198 | 0 | sorted|
|200 | 398 | 0 | sorted|
|300 | 598 | 0 | sorted|
|400 | 798 | 0 | sorted|
|500 | 998 | 0 | sorted|
|600 | 1198 | 0 | sorted|
|700 | 1398 | 0 | sorted|
|800 | 1598 | 0 | sorted|
|900 | 1798 | 0 | sorted|
|1000 | 1998 | 0 | sorted|
有誰知道為什么我的升序和降序情況顯示相同的比較值和交換計數? 而且我認為對於升序比較計數不應該這么高,不是嗎?
你有:
temp = number[pivot];
number[pivot] = number[j];
number[j] = temp;
這看起來像一個交換,但你不把它算作一個。 你還有:
while (number[i] <= number[pivot] && i < last)
i++;
comp++;
comp++;
在循環之外,所以你也算錯了比較。 這發生了兩次。
使用此版本的代碼,我得到了更合理的結果:
/* SO 7273-8321 */
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAX_ELEMENTS 1000
#define OFFSET 100
#define MAX_LINES 10
int a[MAX_ELEMENTS];
int comp, swap;
void quicksort(int[], int, int);
void init_step(void);
void print_step(int);
void print_header(char *);
int sorted(int);
void swap1(int *, int *);
int main(void)
{
int i;
int n;
srandom(time(0));
print_header("random");
for (n = OFFSET; n <= MAX_ELEMENTS; n += OFFSET)
{
init_step();
for (i = 0; i < n; i++)
{
a[i] = random() % n;
}
quicksort(a, 0, n - 1);
print_step(n);
}
print_header("ascending order");
for (n = OFFSET; n <= MAX_ELEMENTS; n += OFFSET)
{
init_step();
for (i = 0; i < n; i++)
{
a[i] = i;
}
quicksort(a, 0, n - 1);
print_step(n);
}
print_header("descending order");
for (n = OFFSET; n <= MAX_ELEMENTS; n += OFFSET)
{
init_step();
for (i = 0; i < n; i++)
{
a[i] = n - i;
}
quicksort(a, 0, n - 1);
print_step(n);
}
return 0;
}
void init_step(void)
{
swap = 0;
comp = 0;
}
void print_header(char *s)
{
printf("%s\n n, comparison, swap, check", s);
printf("\n");
}
void print_step(int n)
{
printf("%4d, %8d, %8d", n, comp, swap);
if (sorted(n))
{
printf(", sorted\n");
}
else
{
printf(", unsorted\n");
}
}
int sorted(int n)
{
int i;
for (i = 0; i < n - 1; i++)
if (a[i] > a[i + 1])
return 0;
return 1;
}
void swap1(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
swap++;
}
void quicksort(int number[25], int first, int last)
{
int i, j, pivot, temp;
if (first < last)
{
pivot = first;
i = first;
j = last;
while (i < j)
{
while (number[i] <= number[pivot] && i < last)
{
i++;
comp++;
}
while (number[j] > number[pivot])
{
j--;
comp++;
}
if (i < j)
{
swap1(&number[i], &number[j]);
}
}
temp = number[pivot];
number[pivot] = number[j];
number[j] = temp;
swap++;
quicksort(number, first, j - 1);
quicksort(number, j + 1, last);
}
}
示例輸出:
random
n, comparison, swap, check
100, 644, 172, sorted
200, 1703, 367, sorted
300, 2845, 592, sorted
400, 3493, 840, sorted
500, 5020, 1094, sorted
600, 6206, 1327, sorted
700, 7279, 1617, sorted
800, 8784, 1827, sorted
900, 9342, 2143, sorted
1000, 10185, 2470, sorted
ascending order
n, comparison, swap, check
100, 5049, 99, sorted
200, 20099, 199, sorted
300, 45149, 299, sorted
400, 80199, 399, sorted
500, 125249, 499, sorted
600, 180299, 599, sorted
700, 245349, 699, sorted
800, 320399, 799, sorted
900, 405449, 899, sorted
1000, 500499, 999, sorted
descending order
n, comparison, swap, check
100, 4999, 99, sorted
200, 19999, 199, sorted
300, 44999, 299, sorted
400, 79999, 399, sorted
500, 124999, 499, sorted
600, 179999, 599, sorted
700, 244999, 699, sorted
800, 319999, 799, sorted
900, 404999, 899, sorted
1000, 499999, 999, sorted
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.