Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

###Bug###

Bug

When I saw your results, I was skeptical that insertion sort would be faster than quick sort. The problem is that there is a bug in this line in duplicate():

 return memcpy(new, v, dim);

This line should have been:

 return memcpy(new, v, dim * sizeof(*v));

Due to this bug, only the first 25% of the original array was being duplicated, and the rest was left filled with zeroes. This definitely would have an effect on the sorting times of your various sorts. Here are the times I got before and after I fixed the above line:

Before the fix (50000 elements)
-------------------------------
Naive sort: 0.718000s
Bubble sort: 0.905000s
Insert sort: 0.296000s
Quick sort: 0.406000s
After the fix (50000 elements)
------------------------------
Naive sort: 0.733000s
Bubble sort: 3.978000s
Insert sort: 0.359000s
Quick sort: 0.015000s

###Bug###

When I saw your results, I was skeptical that insertion sort would be faster than quick sort. The problem is that there is a bug in this line in duplicate():

 return memcpy(new, v, dim);

This line should have been:

 return memcpy(new, v, dim * sizeof(*v));

Due to this bug, only the first 25% of the original array was being duplicated, and the rest was left filled with zeroes. This definitely would have an effect on the sorting times of your various sorts. Here are the times I got before and after I fixed the above line:

Before the fix (50000 elements)
-------------------------------
Naive sort: 0.718000s
Bubble sort: 0.905000s
Insert sort: 0.296000s
Quick sort: 0.406000s
After the fix (50000 elements)
------------------------------
Naive sort: 0.733000s
Bubble sort: 3.978000s
Insert sort: 0.359000s
Quick sort: 0.015000s

Bug

When I saw your results, I was skeptical that insertion sort would be faster than quick sort. The problem is that there is a bug in this line in duplicate():

 return memcpy(new, v, dim);

This line should have been:

 return memcpy(new, v, dim * sizeof(*v));

Due to this bug, only the first 25% of the original array was being duplicated, and the rest was left filled with zeroes. This definitely would have an effect on the sorting times of your various sorts. Here are the times I got before and after I fixed the above line:

Before the fix (50000 elements)
-------------------------------
Naive sort: 0.718000s
Bubble sort: 0.905000s
Insert sort: 0.296000s
Quick sort: 0.406000s
After the fix (50000 elements)
------------------------------
Naive sort: 0.733000s
Bubble sort: 3.978000s
Insert sort: 0.359000s
Quick sort: 0.015000s
Source Link
JS1
  • 28.8k
  • 3
  • 41
  • 83

###Bug###

When I saw your results, I was skeptical that insertion sort would be faster than quick sort. The problem is that there is a bug in this line in duplicate():

 return memcpy(new, v, dim);

This line should have been:

 return memcpy(new, v, dim * sizeof(*v));

Due to this bug, only the first 25% of the original array was being duplicated, and the rest was left filled with zeroes. This definitely would have an effect on the sorting times of your various sorts. Here are the times I got before and after I fixed the above line:

Before the fix (50000 elements)
-------------------------------
Naive sort: 0.718000s
Bubble sort: 0.905000s
Insert sort: 0.296000s
Quick sort: 0.406000s
After the fix (50000 elements)
------------------------------
Naive sort: 0.733000s
Bubble sort: 3.978000s
Insert sort: 0.359000s
Quick sort: 0.015000s
lang-c

AltStyle によって変換されたページ (->オリジナル) /