which sorting strategy do you prefer? heap,sort... how to sort by calling a class?

harrylennox

Junior Member
May 20, 2008
2
0
0
Hi...

I am C++ beginner and I want to know something regarding to this:
how can I sort a 20 integers?
which sorting techniques do you prefer?
and
how can I sort a list of numbers by just calling a class to sort the user input numbers?

somebody could share about sorting codes and calling a class....

many thanks....





 

SJP0tato

Senior member
Aug 19, 2004
267
0
76
While not c++ specific, almost every programming language has some ability to implement the algorithms described here:

http://en.wikipedia.org/wiki/Sorting_algorithms

"Preference" usually is dictated by ease of implementation, and the workload of the algorithm (are you sorting a deck of cards, 1000 orders, or 500-billion entries in a database).
 

Dimmu

Senior member
Jun 24, 2005
890
0
0
With a list of length 20, you are not going to notice a major difference in speed/efficiency between the usage of the popular sorting methods that SJP0tato linked to. Since this is the case, it would be logical to choose the one which is easiest to implement. The insertion sort method is rather painless:

void insertion_sort( apvector <int> &array)
{
int i, j, key, array_length=array.length( );
for(j = 1; j < array_length; j++) //Notice starting with 1 (not 0)
{
key = array[j];
for(i = j - 1; (i >= 0) && (array < key); i--) //Move smaller values up one position
{
array[i+1] = array;
}
array[i+1] = key; //Insert key into proper position
}
return;
}


 

deveraux

Senior member
Mar 21, 2004
284
0
71
The wikipedia article linked by SJP0tato covers most of the basics for the common sorting algorithm. Having said that, I believe STL provides some sorting algorithms for its container classes and unless you have an academic reason for learning how to make a sorting algorithm, its probably better to just use those are they are likely more efficient.

It is always good to familiarize yourself with the advantages/disadvantages of each sorting method however as they will sometimes come into play when deciding what you wish to sort.

@OP: What do you mean by "sort by calling a class"? Do you mean sorting a user defined class or using a class to sort some array?

Edit: Removed some erroneous statements.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
I would probably just us the STL sort and not worry about how it is done. (especially for a problem this small)
 

Extrarius

Senior member
Jul 8, 2001
259
0
0
I typically used the C++ function std::sort, but if you can't use the standard library for whatever reason, bubble sort is good enough for your purposes (or, if you must be fancy, go for comb sort).
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |