Hi,
I think this has to do with algorithm analysis & Big O notation, so maybe the title is a bit unfortunate.
Search on wiki for Big O notation.
Some basic ideas, with examples:
O(1) : return the
i
'th value (known) in an array or similar container that has subscript
MyContainer[i]
. This is done by subtracting pointers, so is very direct.
O(n) : search through a container as above for a value , but the subscript isn't known - so each item in the container must compared - worst case is
n
the number of elements in the container. Average case is
n/2
O(log n) : Search in a binary sort tree (BST). It is log n because each time a node is traversed, it represents a halving of the remaining data to search.
There are other very inefficient examples, such as n
2, n
3, exponential etc.
When I was at uni 20 years ago, we had an entire textbook about this.
To start you all off, first there is
while (i < 100)
, so that is O(n). But then there is
if (i mod 2 == 0)
, so now it is O(n/2).
It is important to establish variables for the size of the array - even though it is 100 by 100. In O(n/2) above n is the number of rows in the array, but the sort function operates on all 100 columns, so I think it is important not to get confused by this, so create a variable for the number of columns.
Hope all is well :+)
Edit:
Not relevant to the problem, but helpful nonetheless.
I always uses braces, even when the is 1 statement, so I would write that code like this:
1 2 3 4 5 6 7 8
|
int arr[100][100]
while (i < 100) {
if (i mod 2 == 0) {
SelSort(arr[][], i)
}
}
| |
And I dislike using variable like
i
and
j
or
m
and
n
and others, because their similarity can cause readability issues and lead to errors. Especially for 2 dimensional Containers.
Years ago I was helping someone who had 2 pages of code full of
i
and
j
subscripts. The code compiled, but had a nasty runtime error. So I suggested changing
i
to
Row
and
j
to
Column
. The error became fairly obvious, because there was a
Row, Row
, which it should have been
Row, Column
This would have still taken some time to fix even with a debugger - it only segfaulted sometimes when Row > Column (subscript out of bounds), so I guess it was a really good lesson on how good coding practices can make one life much easier.