|
Hello,
i need to sort out this problem:
"The bubble sort represented in fig 6.15 of your text book is inefficient for large arrays. Make the following simple modifications to improve the performance of the bubble sort.
After the first pass, the largest number is guaranteed to be in the highest-numbered element of the array; after the second pass, the two highest numbers are “in place,” and so on. Instead of making nine comparisons on every pass, modify the bubble sort to make eight comparisons on the second pass, seven on the third pass and so on."
Could someone check my code? I should get an output like this but i don't :
After pass 0: 2 4 6 8 10 12 68 45 37 89
After pass 1: 2 4 6 8 10 12 45 37 68
After pass 2: 2 4 6 8 10 12 37 45
After pass 3: 2 4 6 8 10 12 37
After pass 4: 2 4 6 8 10 12
After pass 5: 2 4 6 8 10
After pass 6: 2 4 6 8
After pass 7: 2 4 6
After pass 8: 2 4
There is something wrong with the loop...
Thanks a lot!!!
raeiko
#include <stdio.h>
#define SIZE 10
int main ( void )
{
int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
int pass;
int i;
int hold;
int NumOfComparisons = 0;
printf( "Data items in original order:\n\n" );
for ( i = 0; i <SIZE; i++ ){
printf( "%4d", a[ i ] );
}
printf( "\n\n" );
for ( pass = 1; pass < 9; pass++ ){
for ( i = 0; i < 9 - pass; i++ ){
if ( a[ i ] > a[ i + 1] ){
hold = a[ i ];
a[ i ] = a[ i + 1];
a[ i + 1] = hold;
}
NumOfComparisons = NumOfComparisons + 1;
}
for ( i = 0; i < SIZE - pass; i++ ){
printf( "After pass %d: %4d\n", pass, a[ i ] );
printf( "\n " );
}
}
printf( "Data items in ascending order:\n\n" );
for ( i = 0; i < SIZE; i++ ){
printf( "%4d", a[ i ] );
}
printf( "\n\n" );
printf( "Number of comparisons: %d\n", NumOfComparisons );
return 0;
}
|
|
|
|
|
1. You've got the loop bounds for pass and i wrong (hint - think SIZE)
2. Your print loop contents is incorrect
Apart from that, the algorithm's correct.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Don't like to do exercise for others
Anyway, the following is the fastest possible bubble sort I know;
bool sorted = false;
for (int i=1; i<N && !sorted; ++i)
{
sorted = true;
for (int j=0; j<N-i; ++j)
{
int k = j + 1;
if (A[j] > A[k]) {
int t = A[j];
A[j] = A[k];
A[k] = t;
sorted = false;
}
}
}
--
Arman
|
|
|
|
|
Hi,
I have created one button control dynamically. i have one requirement of need to visible the button whenever i have clicked on check box.
If i uncheck the check box the button control should be un visible..
Can any one help me out how it is possible in the MFC..?
Thanks
Venki
|
|
|
|
|
does this help?[^]
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Please don't tell the secret to everybody!
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Can you help me out with a CEditView SDI sample? I tried to search the standard Superpad application.. I couldn't locate a downloadable version.
|
|
|
|
|
Disable and Visible are 2 different things.
So you have to be specific as to which you want to set.
'Cause reading a person's mind through this forum is impossible.
EnableWindow is for enable/disable and ShowWindow is for show/hide.
«_Superman_»
|
|
|
|
|
HELP PLEASE!!!
I have been stuck learning c programming for about 2 days. My code wont compile and here it is:
#include
main()
{
printf("Is your printer on (Y/N)?"); /*Request Y or N */
scanf(" %c",&ans);
if ((ans == 'Y') || (ans == 'N'))
{/*Gets here if user typed a correct answer*/
if (ans == 'N')
{printf("* * Turn the printer on now. * * *\n");
}
}
else
{
printf("You did not enter a Y or N.\n");
return 0;
}
-- I know that the problem is in line 4 but I haven't been able to learn how to use the "%c" because I cant find a book that completely explains it. PLEASE HELP
|
|
|
|
|
Where's char ans;?
You need to a good book for C like
"Beginnig C"
From Novice to Professional,
Fourth Edition
Ivor Horton
Of one Essence is the human race
thus has Creation put the base
One Limb impacted is sufficient
For all Others to feel the Mace
(Saadi )
|
|
|
|
|
I would strongly recommend this book - online tutorials can get you so far, but you really need a book to properly explain not only about the actual language itself, but the way in which to code certain tasks and explain methodologies and paradigms applicable with the language.
Regards,
--Perspx
"A refund for defective software might be nice, except it would bankrupt the entire software industry in the first year." -Andrew Tanenbaum
"Einstein argued that there must be simplified explanations of nature, because God is not capricious or arbitrary. No such faith comforts the software engineer." -Fred Brooks
|
|
|
|
|
Hi ya
I need a good class for saving / loading application settings. I have the following demands for how it should be built:
1) It should be able to handle groups of settings (One group for each view in the application)
2) It should maintain the settings in memory.
3) It should provide an open interface, so one can attach any serialization / deserialization logic (registry-, ini-, xml-file, etc.)
4) It should not have a restricting license like GPL or similar
I can find lots of articles about saving to ini-files, registry-files, xml-files, etc. But I haven't found an article that makes the abstraction I want.
Please let me know
modified on Saturday, February 14, 2009 9:51 AM
|
|
|
|
|
Did you think about your question you need to a class for save application settings of your program you need to make it yourself I think you found your answer previous(ini files,xml files and registry) you need to make a class for them then you need to shift your parameters to that class.
Of one Essence is the human race
thus has Creation put the base
One Limb impacted is sufficient
For all Others to feel the Mace
(Saadi )
|
|
|
|
|
I'm not asking for help to invent the wheel, but asking for help to find an article / library that can provide the implemention of the wheel I need.
I rather want to reuse than to remake it myself.
modified on Saturday, February 14, 2009 11:06 AM
|
|
|
|
|
Good idea: develop it and write a good article about.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thank you for the encouragement
|
|
|
|
|
Think I have found the main building block, that satisfies most of my needs:
* Can save to the registry
* Can save to an ini-file
* Can save to an xml-file
And it has a reasonable license requirements:
* You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise) when your product is released in binary form.
* You are allowed to modify the source code in any way you want except you cannot modify the copyright details at the top of each module.
* If you want to distribute source code with your application, then you are only allowed to distribute versions released by the author. This is to maintain a single distribution point for the source code.
http://www.naughter.com/appsettings.html
All I need now is to create my in-memory configuration-class, and make a thin wrapper around the AppSettings class.
|
|
|
|
|
In cmath or math.h, there are only float/double versions.
|
|
|
|
|
Why do you need it? If your exponent (or base) is real then you have to use anyway the double version, if both base and exponent are integers then, you know, repeated multiplication does the job.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
There's no power function for integers - your best bet is to try something like this:
int ipow(int x, int y)
{
return (int)pow((double)x, (double)y);
}
Doesn't protect against overflow or anything, but does the job for appropriate inputs.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
int main(void)
{
const char *buf = "PICK ANY TWO: 10 2 F F";
string name;
ostringstream oss;
istringstream iss(buf);
iss.get(*oss.rdbuf(), ':');
name = oss.str();
return 0;
}
modified on Saturday, February 14, 2009 8:53 AM
|
|
|
|
|
Depends what it's meant to do - if it's meant to display a message on the screen, there are plenty of flaws...
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Given the string "PICK ANY TWO: 10 2 F F",
get the token before ':', it's "PICK ANY TWO".
|
|
|
|
|
I'd probably do this:
int main(void)
{
const std::string s("PICK ANY TWO: 10 2 F F");
const string name(s.substr(0, s.find(":"));
return 0;
}
Little bit simpler...
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Sure
But when I want to parse the string to 5 parts, as
PICK ANY TWO
10
2
F
F
use stringstream seems more consistent,
I'm not sure if there is any simpler version.
C can do this in a very clear way, can C++:
char name[41] = {0};
int m,n;
char c,d;
int i = sscanf_s(buf, "%[^:]%*c%d %d %c %c",name, _countof(name), &m, &n, &c, 1, &d, 1);
Thanks.
|
|
|
|