c++ - Insertion comparison #'s seem too big -


i confused if these comparison numbers supposed large in value vector 100 random two-digit numbers. full program --> safe link: https://ideone.com/oybdbd program output @ bottom page of link. appreciate input.

int insertionsort (vector<int> &v) {     int j, temp, counter = 0;      (int = 1; < v.size(); i++) {         j = i;          while (++counter && j > 0 && v[j] < v[j-1]){             temp = v[j];             v[j] = v[j-1];             v[j-1] = temp;             j--;         }     }     return counter; } 

the average case performance of insertion sort o(n^2) (see wiki entry). vector of 100 elements, expected number of comparisons therefore o(10000). coming out 2656 or, in second run, 4995, comparisons therefore lower might otherwise expect.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -