Is this Insertion Sort implementation worst case O(n)?
I know that Insertion Sort is supposed to be worst case O(n^2), but I'm wondering why the following implementation isn't O(n).
void main()
//insertion sort runs from i = 1 to i = n, thus is worst case O(n)
for (
int i = 1,
placeholder = 0,
A = 10,9,8,7,6,5,4,3,2,1 ,
j = i;
i <= 10;
j-- > 0 && A[j - 1] > A[j]
? placeholder = A[j], A[j] = A[j - 1], A[j - 1] = placeholder
: j = ++i
)
for (
int x = 0;
x < 10; x++
)
cout << A[x] << ' ';
cout << endl;
system("pause");
There is only one for loop involved here and it runs from 1 to n. It seems to me that this would be the definition of O(n). What exactly am I missing here?
c++ sorting insertion-sort
add a comment |
I know that Insertion Sort is supposed to be worst case O(n^2), but I'm wondering why the following implementation isn't O(n).
void main()
//insertion sort runs from i = 1 to i = n, thus is worst case O(n)
for (
int i = 1,
placeholder = 0,
A = 10,9,8,7,6,5,4,3,2,1 ,
j = i;
i <= 10;
j-- > 0 && A[j - 1] > A[j]
? placeholder = A[j], A[j] = A[j - 1], A[j - 1] = placeholder
: j = ++i
)
for (
int x = 0;
x < 10; x++
)
cout << A[x] << ' ';
cout << endl;
system("pause");
There is only one for loop involved here and it runs from 1 to n. It seems to me that this would be the definition of O(n). What exactly am I missing here?
c++ sorting insertion-sort
1
You have two for loops hidden in the definition of the first for. i goes from 1 to array.size, j potentially goes from i to 0 for each i.
– darxsys
Nov 12 at 17:30
6
The trained eye might say "don't write code like that".
– Neil Butterworth
Nov 12 at 17:30
@NeilButterworth i decided to take away my comment, it looked overly snarky :)
– SergeyA
Nov 12 at 17:31
add a comment |
I know that Insertion Sort is supposed to be worst case O(n^2), but I'm wondering why the following implementation isn't O(n).
void main()
//insertion sort runs from i = 1 to i = n, thus is worst case O(n)
for (
int i = 1,
placeholder = 0,
A = 10,9,8,7,6,5,4,3,2,1 ,
j = i;
i <= 10;
j-- > 0 && A[j - 1] > A[j]
? placeholder = A[j], A[j] = A[j - 1], A[j - 1] = placeholder
: j = ++i
)
for (
int x = 0;
x < 10; x++
)
cout << A[x] << ' ';
cout << endl;
system("pause");
There is only one for loop involved here and it runs from 1 to n. It seems to me that this would be the definition of O(n). What exactly am I missing here?
c++ sorting insertion-sort
I know that Insertion Sort is supposed to be worst case O(n^2), but I'm wondering why the following implementation isn't O(n).
void main()
//insertion sort runs from i = 1 to i = n, thus is worst case O(n)
for (
int i = 1,
placeholder = 0,
A = 10,9,8,7,6,5,4,3,2,1 ,
j = i;
i <= 10;
j-- > 0 && A[j - 1] > A[j]
? placeholder = A[j], A[j] = A[j - 1], A[j - 1] = placeholder
: j = ++i
)
for (
int x = 0;
x < 10; x++
)
cout << A[x] << ' ';
cout << endl;
system("pause");
There is only one for loop involved here and it runs from 1 to n. It seems to me that this would be the definition of O(n). What exactly am I missing here?
c++ sorting insertion-sort
c++ sorting insertion-sort
asked Nov 12 at 17:21
cyber_punkVCR
1
1
1
You have two for loops hidden in the definition of the first for. i goes from 1 to array.size, j potentially goes from i to 0 for each i.
– darxsys
Nov 12 at 17:30
6
The trained eye might say "don't write code like that".
– Neil Butterworth
Nov 12 at 17:30
@NeilButterworth i decided to take away my comment, it looked overly snarky :)
– SergeyA
Nov 12 at 17:31
add a comment |
1
You have two for loops hidden in the definition of the first for. i goes from 1 to array.size, j potentially goes from i to 0 for each i.
– darxsys
Nov 12 at 17:30
6
The trained eye might say "don't write code like that".
– Neil Butterworth
Nov 12 at 17:30
@NeilButterworth i decided to take away my comment, it looked overly snarky :)
– SergeyA
Nov 12 at 17:31
1
1
You have two for loops hidden in the definition of the first for. i goes from 1 to array.size, j potentially goes from i to 0 for each i.
– darxsys
Nov 12 at 17:30
You have two for loops hidden in the definition of the first for. i goes from 1 to array.size, j potentially goes from i to 0 for each i.
– darxsys
Nov 12 at 17:30
6
6
The trained eye might say "don't write code like that".
– Neil Butterworth
Nov 12 at 17:30
The trained eye might say "don't write code like that".
– Neil Butterworth
Nov 12 at 17:30
@NeilButterworth i decided to take away my comment, it looked overly snarky :)
– SergeyA
Nov 12 at 17:31
@NeilButterworth i decided to take away my comment, it looked overly snarky :)
– SergeyA
Nov 12 at 17:31
add a comment |
2 Answers
2
active
oldest
votes
Thats a very odd way to write code.But You have 2 for loops in the definition. It is not always necessary to have nested loops to have O(n^2), you can have it with recursion also.
In simple terms O(n^2)n simply means number of operations performed when the input size is n.
add a comment |
The code given is not a correct c++ code and not even close to a pseudocode.
The correct code should be like this:
void main()
int i,j,key;
int A=10,9,8,7,6,5,4,3,2,1;
//cout<<"Array before sorting:"<<endl;
//for(i=0;i<10;i++)
//cout<<A[i]<<"t";
//cout<<endl;
for(i=1;i<10;i++)
key=A[i];
for(j=i-1;j>=0 && A[j]>key;j--)
A[j+1]=A[j];
A[j+1]=key;
//cout<<"Array after sorting:"<<endl;
//for(i=0;i<10;i++)
//cout<<A[i]<<"t";
//cout<<endl;
See, insertion sort has two loops. Outer loop is to maintain the key variable and the inner loop is to compare the elements prior to key variable with the key variable. And therefore the worst case time complexity is O(n^2) and not O(n), as the basic algorithm of insertion sort contains two loops, both of which eventually iterate n times in case of worst case i.e. when the array is inverted.
1
I don't know what you mean by incorrect c++ code. It compiles and sorts the array. I don't care about other ways of writing the sort, I'm curious about the one I wrote.
– cyber_punkVCR
Nov 12 at 18:03
The code is very oddly written but seems like legal c++. Notice that most lines don't end with a semi colon and most of the code belongs to aforstatement. Edit : Never mind, noticed thatmainhasvoidreturn type.
– François Andrieux
Nov 12 at 18:07
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267108%2fis-this-insertion-sort-implementation-worst-case-on%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thats a very odd way to write code.But You have 2 for loops in the definition. It is not always necessary to have nested loops to have O(n^2), you can have it with recursion also.
In simple terms O(n^2)n simply means number of operations performed when the input size is n.
add a comment |
Thats a very odd way to write code.But You have 2 for loops in the definition. It is not always necessary to have nested loops to have O(n^2), you can have it with recursion also.
In simple terms O(n^2)n simply means number of operations performed when the input size is n.
add a comment |
Thats a very odd way to write code.But You have 2 for loops in the definition. It is not always necessary to have nested loops to have O(n^2), you can have it with recursion also.
In simple terms O(n^2)n simply means number of operations performed when the input size is n.
Thats a very odd way to write code.But You have 2 for loops in the definition. It is not always necessary to have nested loops to have O(n^2), you can have it with recursion also.
In simple terms O(n^2)n simply means number of operations performed when the input size is n.
answered Nov 12 at 17:54
kerastf
28610
28610
add a comment |
add a comment |
The code given is not a correct c++ code and not even close to a pseudocode.
The correct code should be like this:
void main()
int i,j,key;
int A=10,9,8,7,6,5,4,3,2,1;
//cout<<"Array before sorting:"<<endl;
//for(i=0;i<10;i++)
//cout<<A[i]<<"t";
//cout<<endl;
for(i=1;i<10;i++)
key=A[i];
for(j=i-1;j>=0 && A[j]>key;j--)
A[j+1]=A[j];
A[j+1]=key;
//cout<<"Array after sorting:"<<endl;
//for(i=0;i<10;i++)
//cout<<A[i]<<"t";
//cout<<endl;
See, insertion sort has two loops. Outer loop is to maintain the key variable and the inner loop is to compare the elements prior to key variable with the key variable. And therefore the worst case time complexity is O(n^2) and not O(n), as the basic algorithm of insertion sort contains two loops, both of which eventually iterate n times in case of worst case i.e. when the array is inverted.
1
I don't know what you mean by incorrect c++ code. It compiles and sorts the array. I don't care about other ways of writing the sort, I'm curious about the one I wrote.
– cyber_punkVCR
Nov 12 at 18:03
The code is very oddly written but seems like legal c++. Notice that most lines don't end with a semi colon and most of the code belongs to aforstatement. Edit : Never mind, noticed thatmainhasvoidreturn type.
– François Andrieux
Nov 12 at 18:07
add a comment |
The code given is not a correct c++ code and not even close to a pseudocode.
The correct code should be like this:
void main()
int i,j,key;
int A=10,9,8,7,6,5,4,3,2,1;
//cout<<"Array before sorting:"<<endl;
//for(i=0;i<10;i++)
//cout<<A[i]<<"t";
//cout<<endl;
for(i=1;i<10;i++)
key=A[i];
for(j=i-1;j>=0 && A[j]>key;j--)
A[j+1]=A[j];
A[j+1]=key;
//cout<<"Array after sorting:"<<endl;
//for(i=0;i<10;i++)
//cout<<A[i]<<"t";
//cout<<endl;
See, insertion sort has two loops. Outer loop is to maintain the key variable and the inner loop is to compare the elements prior to key variable with the key variable. And therefore the worst case time complexity is O(n^2) and not O(n), as the basic algorithm of insertion sort contains two loops, both of which eventually iterate n times in case of worst case i.e. when the array is inverted.
1
I don't know what you mean by incorrect c++ code. It compiles and sorts the array. I don't care about other ways of writing the sort, I'm curious about the one I wrote.
– cyber_punkVCR
Nov 12 at 18:03
The code is very oddly written but seems like legal c++. Notice that most lines don't end with a semi colon and most of the code belongs to aforstatement. Edit : Never mind, noticed thatmainhasvoidreturn type.
– François Andrieux
Nov 12 at 18:07
add a comment |
The code given is not a correct c++ code and not even close to a pseudocode.
The correct code should be like this:
void main()
int i,j,key;
int A=10,9,8,7,6,5,4,3,2,1;
//cout<<"Array before sorting:"<<endl;
//for(i=0;i<10;i++)
//cout<<A[i]<<"t";
//cout<<endl;
for(i=1;i<10;i++)
key=A[i];
for(j=i-1;j>=0 && A[j]>key;j--)
A[j+1]=A[j];
A[j+1]=key;
//cout<<"Array after sorting:"<<endl;
//for(i=0;i<10;i++)
//cout<<A[i]<<"t";
//cout<<endl;
See, insertion sort has two loops. Outer loop is to maintain the key variable and the inner loop is to compare the elements prior to key variable with the key variable. And therefore the worst case time complexity is O(n^2) and not O(n), as the basic algorithm of insertion sort contains two loops, both of which eventually iterate n times in case of worst case i.e. when the array is inverted.
The code given is not a correct c++ code and not even close to a pseudocode.
The correct code should be like this:
void main()
int i,j,key;
int A=10,9,8,7,6,5,4,3,2,1;
//cout<<"Array before sorting:"<<endl;
//for(i=0;i<10;i++)
//cout<<A[i]<<"t";
//cout<<endl;
for(i=1;i<10;i++)
key=A[i];
for(j=i-1;j>=0 && A[j]>key;j--)
A[j+1]=A[j];
A[j+1]=key;
//cout<<"Array after sorting:"<<endl;
//for(i=0;i<10;i++)
//cout<<A[i]<<"t";
//cout<<endl;
See, insertion sort has two loops. Outer loop is to maintain the key variable and the inner loop is to compare the elements prior to key variable with the key variable. And therefore the worst case time complexity is O(n^2) and not O(n), as the basic algorithm of insertion sort contains two loops, both of which eventually iterate n times in case of worst case i.e. when the array is inverted.
answered Nov 12 at 17:58
Aniket Chattopadhyay
114
114
1
I don't know what you mean by incorrect c++ code. It compiles and sorts the array. I don't care about other ways of writing the sort, I'm curious about the one I wrote.
– cyber_punkVCR
Nov 12 at 18:03
The code is very oddly written but seems like legal c++. Notice that most lines don't end with a semi colon and most of the code belongs to aforstatement. Edit : Never mind, noticed thatmainhasvoidreturn type.
– François Andrieux
Nov 12 at 18:07
add a comment |
1
I don't know what you mean by incorrect c++ code. It compiles and sorts the array. I don't care about other ways of writing the sort, I'm curious about the one I wrote.
– cyber_punkVCR
Nov 12 at 18:03
The code is very oddly written but seems like legal c++. Notice that most lines don't end with a semi colon and most of the code belongs to aforstatement. Edit : Never mind, noticed thatmainhasvoidreturn type.
– François Andrieux
Nov 12 at 18:07
1
1
I don't know what you mean by incorrect c++ code. It compiles and sorts the array. I don't care about other ways of writing the sort, I'm curious about the one I wrote.
– cyber_punkVCR
Nov 12 at 18:03
I don't know what you mean by incorrect c++ code. It compiles and sorts the array. I don't care about other ways of writing the sort, I'm curious about the one I wrote.
– cyber_punkVCR
Nov 12 at 18:03
The code is very oddly written but seems like legal c++. Notice that most lines don't end with a semi colon and most of the code belongs to a
for statement. Edit : Never mind, noticed that main has void return type.– François Andrieux
Nov 12 at 18:07
The code is very oddly written but seems like legal c++. Notice that most lines don't end with a semi colon and most of the code belongs to a
for statement. Edit : Never mind, noticed that main has void return type.– François Andrieux
Nov 12 at 18:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267108%2fis-this-insertion-sort-implementation-worst-case-on%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You have two for loops hidden in the definition of the first for. i goes from 1 to array.size, j potentially goes from i to 0 for each i.
– darxsys
Nov 12 at 17:30
6
The trained eye might say "don't write code like that".
– Neil Butterworth
Nov 12 at 17:30
@NeilButterworth i decided to take away my comment, it looked overly snarky :)
– SergeyA
Nov 12 at 17:31