| | |
newbie and i need some help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2007
Posts: 2
Reputation:
Solved Threads: 0
well i have 2 weeks reading and testing some C++ but unfortunately i fell into a wall.
I succeed making an array with random length and random elements (yeah i did it)and i want to help me making those elements sorted. eg quicksort
I read "algorithms in C++ " written by SEDGEWICK and here his proposal
1st --How do i declare quicksort and partition in my main function. I tried to copy paste it (i know its wrong) and compile it but no luck
2nd--in the code above "a" will be my arrays name instead?
thanks
I'm looking forward to hearing from you
I succeed making an array with random length and random elements (yeah i did it)and i want to help me making those elements sorted. eg quicksort
I read "algorithms in C++ " written by SEDGEWICK and here his proposal
C++ Syntax (Toggle Plain Text)
template<class Item> void quicksort(Item a[],int l,int r) { if (r<=l) return; int m=partition(a,l,r); quicksort(a,l,m-1); quicksort(a,m+1,r); } template<class Item> int partition(Item a[],int l, int r) { int m=l-1, j=r; Item v=a[r]; for(;;) { while(a[++m]<v); while(v<a[--j]) if (j==1) break; if(m>=j)break; exch(a[m],a[j]); } exch(a[m],a[r]); return m; }
2nd--in the code above "a" will be my arrays name instead?
thanks
I'm looking forward to hearing from you
Last edited by Narue; Dec 25th, 2007 at 10:49 am. Reason: Added code tags.
hi rizoshar,
-first of all try to use code tags and indent your code to make it more readable....
read here to learn how to use code tags
- SEDGEWICK uses templates, you seem to be a beginner so try not to use templates until you learn a bit more....
-before you try to program an algorithm{i.e. quicksort} it would be useful to know what the algorithm is supposed to do {except from the apparent... i.e. sort} and then try to understand a simple implemenation in pseudocode. Afterwards you can procede and code it in your language of choice and in a second attempt you could use some facilities the language offers {i.e. templates} to make it more reusable, efficient etc.....
-there are tons of internet sites and books that explain quicksort. For example you could try wikipedia or this. Also a nice book is Introduction to Algorithms
After all these steps, if you have any question post back and we will help!
merry christmas
-nicolas
1st:: yeap, it is bad to copy paste code that you don't understand so don't do it
.Of course it won't compile sedgewick uses some of his own functions{if i recall correctly} to make the code more readable
2nd:: yes a will be your array's name....
-first of all try to use code tags and indent your code to make it more readable....
read here to learn how to use code tags
- SEDGEWICK uses templates, you seem to be a beginner so try not to use templates until you learn a bit more....
-before you try to program an algorithm{i.e. quicksort} it would be useful to know what the algorithm is supposed to do {except from the apparent... i.e. sort} and then try to understand a simple implemenation in pseudocode. Afterwards you can procede and code it in your language of choice and in a second attempt you could use some facilities the language offers {i.e. templates} to make it more reusable, efficient etc.....
-there are tons of internet sites and books that explain quicksort. For example you could try wikipedia or this. Also a nice book is Introduction to Algorithms
After all these steps, if you have any question post back and we will help!
merry christmas

-nicolas
•
•
•
•
Originally Posted by rizoshar
1st --How do i declare quicksort and partition in my main function. I tried to copy paste it (i know its wrong) and compile it but no luck
2nd--in the code above "a" will be my arrays name instead?
thanks
.Of course it won't compile sedgewick uses some of his own functions{if i recall correctly} to make the code more readable2nd:: yes a will be your array's name....
Last edited by n.aggel; Dec 25th, 2007 at 7:21 am.
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.
by Robert Frost the "The Road Not Taken"
by Robert Frost the "The Road Not Taken"
First and foremost, don't ever use Robert Sedgewick's code. He knows his stuff when it comes to algorithms, he's a smart guy, and I recommend his books wholeheartedly for the descriptions and insights. But his code sucks ass! It's poorly written and often contains subtle or not so subtle bugs. I learned long ago that it's easier to understand the algorithm and write it myself than to fix Sedgewick's broken examples.
Try this instead, but keep that book and learn from it as long as you don't use the code.
Try this instead, but keep that book and learn from it as long as you don't use the code.
New members chased away this month: 3
•
•
Join Date: Dec 2007
Posts: 2
Reputation:
Solved Threads: 0
Well thank you my friends for your advice;
I did what aggel said and i made my own version. It was a bit difficult but my algo seems to work wright. I love C programming more than C++ but the second one has more abilities i think. I will find them time to time. Narue the book you posted is a very good one. I didnt read it in detail but i think it will help me understand algo's. Sedgewicks book is very good too with full explanation but no very good code i think.
Merry Christmass and a happy new year
I did what aggel said and i made my own version. It was a bit difficult but my algo seems to work wright. I love C programming more than C++ but the second one has more abilities i think. I will find them time to time. Narue the book you posted is a very good one. I didnt read it in detail but i think it will help me understand algo's. Sedgewicks book is very good too with full explanation but no very good code i think.
Merry Christmass and a happy new year
>I love C programming more than C++ but the second one has more abilities i think.
C++ has more features, but it's no more powerful than C. In fact, simplicity is one of C's biggest strengths. While I would question whether anyone in the world knows C++ in its entirety, I would be surprised if any good C programmer wasn't familiar with all of C.
Personally, I'm not a big fan of C++.
>Narue the book you posted is a very good one.
Thank you.
C++ has more features, but it's no more powerful than C. In fact, simplicity is one of C's biggest strengths. While I would question whether anyone in the world knows C++ in its entirety, I would be surprised if any good C programmer wasn't familiar with all of C.
Personally, I'm not a big fan of C++.
>Narue the book you posted is a very good one.
Thank you.
New members chased away this month: 3
![]() |
Similar Threads
- As a newbie, where i should start from in linux? (Getting Started and Choosing a Distro)
- Questions about building a system (was: newbie) (Troubleshooting Dead Machines)
- Best free C/C++ compiler for a newbie? (C++)
- help newbie alert needs help with login page (ASP.NET)
- newbie needs help, basic mfc stuff (C++)
- Hello, newbie here... (Geeks' Lounge)
- Book For Newbie (C++)
- Newbie - how do I start C++ programming? (C++)
- PHP newbie, project feasibility (PHP)
- How to network two Win98 machines (Networking Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: even and odd problem
- Next Thread: prime numbers question
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






