I wrote following code for insertion sort. The code was compiled using
gcc-4.1.1-30 on my fedora core 6 system. For this code output is not
correct {output is 2,5,76,76,76}. It seems, "line #2" is not working.
Meanwhile no problem occurs when
1. line #1 is changed to T key = arr[i]; or
2. line #2 is changed to T& q = arr[j+1]; q = key.
Am I missing something ?
Regards,
Reetesh Mukul
-------------------------------------------------------------------------------------------------------------------
//insertion_sort.cpp
#include <iostream>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/array.hpp>
using namespace boost;
using namespace boost::lambda;
using namespace std;
template<typename T, size_t N>void insertion_sort(array<T,N>& arr)
{
for(size_t i = 2; i < N ; ++i)
{
T& key = arr[i]; //line #1
for(int j = i - 1; j >=0; --j)
{
if( key < arr[j] )
arr[j+1] = arr[j];
else{
arr[j+1] = key; //line #2
break;
}
}
}
}
int main()
{
array<int,5>a = {2,5,76,11,3};
insertion_sort(a);
for_each(a.begin(),a.end(),cout << _1<<" ");
return 0;
} 3 1139
Reetesh Mukul wrote:
I wrote following code for insertion sort. The code was compiled using
gcc-4.1.1-30 on my fedora core 6 system. For this code output is not
correct {output is 2,5,76,76,76}. It seems, "line #2" is not working.
Meanwhile no problem occurs when
1. line #1 is changed to T key = arr[i]; or
Are you sure "or" here is right?
2. line #2 is changed to T& q = arr[j+1]; q = key.
Am I missing something ?
You can never have a reference(T&) to hold a "tmp". Always use T as the
type of the "tmp".
--
Best Regards
Barry
On Feb 3, 4:26 pm, Barry <dhb2...@gmail.comwrote:
Reetesh Mukul wrote:
I wrote following code for insertion sort. The code was compiled using
gcc-4.1.1-30 on my fedora core 6 system. For this code output is not
correct {output is 2,5,76,76,76}. It seems, "line #2" is not working.
Meanwhile no problem occurs when
1. line #1 is changed to T key = arr[i]; or
Are you sure "or" here is right?
2. line #2 is changed to T& q = arr[j+1]; q = key.
Am I missing something ?
You can never have a reference(T&) to hold a "tmp". Always use T as the
type of the "tmp".
--
Best Regards
Barry
Yes it is correct that T& cannot hold "tmp". But where it is happening
inside the code? Please explain further.
With Regards,
Reetesh Mukul
On Feb 3, 4:40 pm, Reetesh Mukul <reetesh.mu...@gmail.comwrote:
On Feb 3, 4:26 pm, Barry <dhb2...@gmail.comwrote:
Reetesh Mukul wrote:
I wrote following code for insertion sort. The code was compiled using
gcc-4.1.1-30 on my fedora core 6 system. For this code output is not
correct {output is 2,5,76,76,76}. It seems, "line #2" is not working.
Meanwhile no problem occurs when
1. line #1 is changed to T key = arr[i]; or
Are you sure "or" here is right?
2. line #2 is changed to T& q = arr[j+1]; q = key.
Am I missing something ?
You can never have a reference(T&) to hold a "tmp". Always use T as the
type of the "tmp".
--
Best Regards
Barry
Yes it is correct that T& cannot hold "tmp". But where it is happening
inside the code? Please explain further.
With Regards,
Reetesh Mukul
Oh! I got it. Sorry for this innocent looking line,
T& key = arr[i]; //line #1
The "key" is changing with arr[i], which I overlooked. Sorry again.
Regards,
Reetesh Mukul This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Steven O. |
last post by:
First, this may not be the correct newsgroup. I have some relatively
basic questions on SQL. I tried to find a newsgroup that was
specifically just about SQL, and was surprised to find that all...
|
by: teddysnips |
last post by:
Application is a Work Tracking/Timesheet database.
The increments of work are stored in the TimesheetItem table. This
contains, inter alia, the Work Code, the Start and the Duration that
the...
|
by: Jay |
last post by:
Using a custom mda file that holds the code for the login sequence for
multiple Access 2000 databases. The problem occurs when the reference
path is changed to a UNC path. This happens when...
|
by: clintonG |
last post by:
When using Visual Studio.NET I observe adding a new Web Form may have
default References added such as...
References
o- System
o- System.Data
o- System.Drawing
o- System.Web
o- System.XML
|
by: Burton Wilkins |
last post by:
Dear Associaties:
I asked this question last Friday, and here it is Wednesday and NOBODY has replied. Come on. Help a little. All that I am looking for is 2 lines of code. One of you must...
|
by: tony |
last post by:
Hello!!
I use VS 2003 and C# for all class library except MeltPracCommon.dll which
is C++.NET
The problem is that I get these warnings when building the exe file and use
my class libraries....
|
by: DWoood |
last post by:
Background:
We have been developing a Web Services Project (let's call it "MyServices")
for several months and have had no problems with it. In fact other members
of my team are still...
|
by: Bruce |
last post by:
I am using VB in Vs2005. Am I missing something or does VB not have the
concept of "builds" (release/debug) like in VC? I wrote an assembly
and I would like to have a debug version of the DLL...
|
by: Jess |
last post by:
Hello,
It seems both static_cast and dynamic_cast can cast a base class
pointer/reference to a derived class pointer/reference. If so, is
there any difference between them?
In addition, if I...
|
by: Jim Langston |
last post by:
I remember there was a thread a while back that was talking about using the
return value of a function as a reference where I had thought the reference
would become invalidated because it was a...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |