Comb sort for sorting algorithms

Source: Internet
Author: User

Comb sort (cpp_comb_sort.cc)
========================================================== ==========================================================
Best time complexity O (?)
Average time complexity O (?)
Worst time complexity O (?)
Space complexity O (1)
Stability or not

Comb sort and shell sort have the same idea, but they use Bubble sorting with variable step sizes.
Most of the features of Comb sort are the same as those of shell sort, and the complexity is difficult to determine. However, comb sort does not compare data in the same way as shell sort, which reduces the time overhead of CPU update cache and operating system page feed, so it is more efficient than shell sort. At the same time, comb sort is the simplest of all advanced sorting algorithms.

 1 #include <cstdio>
2 #include <cstdlib>
3 #include <ctime>
4
5 static unsigned int set_times = 0;
6 static unsigned int cmp_times = 0;
7
8 template<typename item_type> void setval(item_type& item1, item_type& item2) {
9 set_times += 1;
10 item1 = item2;
11 return;
12 }
13
14 template<typename item_type> int compare(item_type& item1, item_type& item2) {
15 cmp_times += 1;
16 return item1 < item2;
17 }
18
19 template<typename item_type> void swap(item_type& item1, item_type& item2) {
20 item_type item3;
21
22 setval(item3, item1);
23 setval(item1, item2);
24 setval(item2, item3);
25 return;
26 }
27
28 template<typename item_type> void comb_sort(item_type* array, int size) {
29 int noswap = 1;
30 int delta = size;
31 int i;
32
33 while(!noswap || delta > 1) {
34 if(delta > 1) {
35 delta /= 1.25;
36 }
37 for(noswap = 1, i = 0; i + delta < size; i++) {
38 if(compare(array[i + delta], array[i])) {
39 noswap = 0;
40 swap(array[i + delta], array[i]);
41 }
42 }
43 }
44 return;
45 }
46
47 int main(int argc, char** argv) {
48 int capacity = 0;
49 int size = 0;
50 int i;
51 clock_t clock1;
52 clock_t clock2;
53 double data;
54 double* array = NULL;
55
56 // generate randomized test case
57 while(scanf("%lf", &data) == 1) {
58 if(size == capacity) {
59 capacity = (size + 1) * 2;
60 array = (double*)realloc(array, capacity * sizeof(double));
61 }
62 array[size++] = data;
63 }
64
65 // sort
66 clock1 = clock();
67 comb_sort(array, size);
68 clock2 = clock();
69
70 // output test result
71 fprintf(stderr, "comb_sort:\t");
72 fprintf(stderr, "time %.2lf\t", (double)(clock2 - clock1) / CLOCKS_PER_SEC);
73 fprintf(stderr, "cmp_per_elem %.2lf\t", (double)cmp_times / size);
74 fprintf(stderr, "set_per_elem %.2lf\n", (double)set_times / size);
75 for(i = 0; i < size; i++) {
76 fprintf(stdout, "%lf\n", array[i]);
77 }
78 free(array);
79 return 0;
80 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.