solving order of time in data structures - data-structures

I want the order time(theta) for these algorithms. Can you help me please?
1)
For ( i = 1 ; i <= n ; i++ )
{
For( j=1 ; j<=n ; j++ )
X++;
n--;
}
2)
For ( i = 1 ; i<=n ; i = i*2)
For( j= 1 ; j <=n ; j = j*2 )
For(k=1 ; k<=j ; K++)
X++;
3)
For ( i=5 ; i<n-10 ; i++ )
For ( j=i ; j>1 ; j-- )
For ( k=1 ; k<j ; k++ )
{
S=S+k+j;
S=S*2;
}

They answer for the above question is:
1) theta(n^2)
2)theta(nlgn)
3)theta(n^3)

Related

Calculate and list how many different 4 digit numbers can be done with the numbers 0 to 9?

There is something I want to learn.
Let's say we have some single digit numbers.
Example: 1-2-3-4-5-6-7-8-9-0
Example II: 1-2-4-6-0
And with these numbers, we want to get 4-digit numbers that are different from each other.
And we want to print them as lists.
Result:
4676
4236
1247
1236
....
Is it possible to do this?
You can write and run a macro like this:
// retrieve the selected text
str = document.selection.Text;
// check the input string format. The input must be something like: "1-2-4-6-0"
if( str.length == 0 ) {
alert( "Select the input string" );
Quit();
}
for( i = 0; i < str.length; ++i ) {
c = str.substr( i, 1 );
if( i % 2 == 0 ) {
if( c < '0' || c > '9' ) {
alert( "not digit" );
Quit();
}
}
else {
if( c != '-' ) {
alert( "not separated by '-'" );
Quit();
}
}
}
var arr = new Array();
j = 0;
for( i = 0; i < str.length; ++i ) {
if( i % 2 == 0 ) {
c = str.substr( i, 1 );
arr[j++] = c;
}
}
if( arr.length < 4 ) {
alert( "Input string should contain at least 4 digits" );
Quit();
}
// list all 4-digit combinations
len = arr.length;
str = "";
for( i = 0; i < len; ++i ) {
for( j = 0; j < len; ++j ) {
for( k = 0; k < len; ++k ) {
for( l = 0; l < len; ++l ) {
str += arr[i] + arr[j] + arr[k] + arr[l] + "\r\n";
}
}
}
}
// write the list in a new document
editor.EnableTab = true;
editor.NewFile();
document.write( str );
To run this, save this code as, for instance, GenCombinations.jsee, and then select this file from Select... in the Macros menu. Finally, select Run GenCombinations.jsee in the Macros menu after selecting an input string.

Using logical operations inside a for loop header

sIn the following code:
for( ( i = j = 0 ); ( i < 3 ) && ( j < 3 ); ( i ++ ) & ( j ++ ) )
{
/* Some code */
}
OR
for( ( i = 0 ) & ( j = 0 ); ( i < 3 ) && ( j < 3 ); ( i ++ ) & ( j ++ ) )
{
/* Some code */
}
When running the code, a warning message appears saying: value computed is not used. Why is that ? Providing that the code isn't functioning well due to the logical errors!
Please help ..
Is this something that you want :
for( i = 0,j = 0 ; i < 3 && j<3; i++ , j++ )
{
/* your code */
}
This should work fine in case you want to use two variables in one for loop.
This way both i and j will be initialized to 0 and both with get incremented by 1 and the less than condition will check for the limit for both variables
In case, you want to loop through two arrays you can do something like this :
int[] arr1 = new int[]{1,2,3,4,5};
int[] arr2= new int[]{6,7,8,9,0,11};
for( i = 0,j = 0 ; i< arr1.length && j<arr2.length; i++ , j++ )
{
System.out.println(arr1[i] + " " +arr2[j]);
}

Having trouble finding the Big-Oh value

Please refer to the code fragment below:
sum = 0;
for( i = 0; i < n; i++ )
for( j = 0; j < i * i; j++ )
for( k = 0; k < j; k++ )
sum++;
If one was to analyze the run-time of this fragment in Big-Oh Notation, what would be the most appropriate value? Would it be O(N^4)?
I have to say O(n^5). I maybe wrong. n is the length of the array in this question I am assuming. If you substitute it, i will stop when i < n j will stop when j < n*n and k will stop when k < n * n. Since these are nested for-loops, the run time is O(n^5)
I think it's O(n^5)
We know that
1 + 2 + 3 + ... + n = n(n+1) / 2 = O(n^2)
1^2 + 2^2 + ... + n^2 = n(n+1)(2n+1) / 6 = O(n^3)
I think that similary statement is true for sum of bigger powers.
Let's think about these two lines
for( j = 0; j < i * i; j++ )
for( k = 0; k < j; k++ )
This loop's time complexity for fixed i is
1 + 2 + ... + (i-1)^2 = (i-1)^2(1 + (i-1)^2) / 2 = O(i^4)
But in our code i changes from 0 to n-1, so we like add fourth powers, and as I said before
1^k + 2^k + ... + n^k = O(n^(k+1))
That's why time complexity is O(n^5)
Given this code, consider each loop separately.
sum = 0;
for( i = 0; i < n; i++ ) //L1
for( j = 0; j < i * i; j++ ) //L2
for( k = 0; k < j; k++ ) //L3
sum++; //
Consider the outermost loop (L1) alone. Clearly this loop is O(n).
for( i = 0; i < n; i++ ) //L1
L2(i);
Consider the next loop (L2). This one is harder, but the loop is O(n^2).
See this SO answer for why the loop is O(n^2): Big O, how do you calculate/approximate it?
for( j = 0; j < i * i; j++ ) //L2
L3(j);
Consider the next loop (L3). Since you know that the L2 loop is O(N^2), what is this loop? (leaving reader to complete their homework).
for( k = 0; k < j; k++ ) //L3
sum++; //

Analyzing programs

I was wondering if anyone could explain to me the semantics of analyzing programs. I get how to do simple ones, but there are some more complicated ones I am not sure how to do. For example here is a question from my book. We are given 6 little segments of code and told to analyze them:
(1). sum = 0;
for( i = 0; i < n; ++i )
++sum;
(2). sum = 0;
for( i = 0; i < n; ++i )
for( j = 0; j < n; ++j )
++sum;
(3). sum = 0;
for( i = 0; i < n; ++i )
for( j = 0; j < n * n; ++j )
++sum;
(4). sum = 0;
for( i = 0; i < n; ++i )
for( j = 0; j < i; ++j )
++sum;
(5). sum = 0;
for( i = 0; i < n; ++i )
for( j = 0; j < i * i; ++j )
for( k = 0; k < j; ++k )
++sum;
(6). sum = 0;
for( i = 1; i < n; ++i )
for( j = 1; j < i * i; ++j )
if( j % i == 0 )
for( k = 0; k < j; ++k )
++sum;
I understand 1, 2, and 4. Those ones are easy. The ones I don't get are 3, 5, and 6.
1 runs n times so that one is in Big-Oh(n). 2 has two for loops which both run n times each so this one is Big-Oh(n^2). 4 is something I have seen before. The inner loop runs as many times as the value of i. So if i = 1 then loop runs once, if i = 2 the loop runs twice for a patters of 1 + 2 + 3 + ... + n which is the pattern n(n + 1) / 2 which means this whole this is in Big-Oh(n^2). I am unsure how to go about 3 with the n * n in the conditional. That is also the reason I am not sure how to go about 5 with the i * i also in there. As for 6, not only do we have the i * i but there is also an if statement that may or may not run. What do I do with that? Can anyone help explain how to do those ones? Thanks.
UPDATE I had an idea about 3. The outer for loop in that one runs n times, the inner for loop runs n^2 times. So for that one, would we have n * n^2 which is n^3? So would that one be in Big-Oh(n^3)?
You figured third one out. The same reasoning applies to all of them. Actually your aim is to find the final value of sum in terms of n.
For the fifth one, that is to find the sum: ∑i=0n(∑j=0n^2 (j))
The inner summation is just (i2*(i2 + 1)) / 2, which is follows from the simple summation formula.
Then you have ∑i=0n(i2*(i2 + 1)) / 2 which is equal to [∑i=0n(i4) + ∑i=0n(i2)] / 2.
This is O(N5) looking at the dominating term ∑i=0n(i4), which you can think as an integral so you just increase the exponent and you get N5. Sixth one is left as an exercise, just imagine what sum will be in the end.

I'm programming sorting algorithm - Shellsort. Where is bug?

I'm programming sorting algorithm — Shellsort. Where is bug?
int shellsort( int ai_numbers[], const int ci_count ){
int i, j, temp, counter = 0, inc;
inc = ci_count / 2;
while ( inc > 0 )
{
for ( i = inc + 1; i < ci_count ; i++)
{
temp = ai_numbers[i];
j = i;
while ( j > inc && ai_numbers[j - inc] > temp )
{
ai_numbers[j] = ai_numbers[j - inc];
j = j - inc;
counter++;
}
ai_numbers[j] = temp;
}
inc = (int) (inc / 2.2);
}
return counter;
}
The condition in the inner loop,
while ( j > inc && ai_numbers[j - inc] > temp )
causes the algorithm to never even look at ai_numbers[0]. The algorithm seems to be written with 1-based array indices in mind.
The loop controls should be
for ( i = inc; i < ci_count ; i++)
and
while ( j >= inc && ai_numbers[j - inc] > temp )
to properly incorporate ai_numbers[0].

Resources