Not Yet Answered # A DOUBTFUL QUESTION..

Ancient Dragon 5,243 Schol-R-LEA 1,003 David W 119 Banfa 597 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

0

There are lots of sorting algorithms, c++ even has one. Are you allowed to use the standard c++ std::sort function? If not, then probably the simplest one to code is the bubble sort. The bubble sort is among the slowest of the sort algorithms, but for small sets of data and for data that's already almost sorted it is ok.

2

I would actually recommend Gnome Sort, as it is a little smaller marginally faster than Bubble Sort, though it is a little harder to understand. The main reason bubble sort is such a topic of fascination for computer scientists is that it appears to be the 'naive' algorithm for sorting, that is to say, it is the one which most people, if asked to sort a list by computer and not given any algorithms to follow, will come up with on their own.

FYI, in case you are wondering, in practice there are no ideal sorting methods that work fastest for all input. Every practical sorting algorithm is sensitive to the order of the original unsorted list, and even where there is a clear difference in the optimal performance (i.e., *O(n log n)* for the average case of quicksort versus *O(n^2)* for Bubblesort), the theoretically slower sorting algorithm may perform better in practice, either because of the order of the input, or because the overhead is smaller for smaller inputs.

In theory, the worst 'practical' sorting algorithm is Bogosort, which consists of repeatedly shuffling the list and testing to see if it has randomly entered a sorted state. The average performance for this is *O((n-1) n!* (that is to say, the number of items in the list minus one, times the product of every number between 1 and the size of the list - a very, very bad performance); the worst case is unbounded (that is to say, it is not guaranteed to ever find a sorted list). Ironically, the theorectically *best* performing sort is the parallel quantum-mechanical variant of Bogosort, in which you perform the sort in *n* parallel universes and select the one in which it is sorted. The theoretical time overhead is *O(1)*; providing the resources need to perform this task are left as an exercise for the reader.

*Edited 2 Years Ago by Schol-R-LEA*

0

You may like to see this ref link to the sorts that already come in the C++ library

The example at the above link is for sorting a vector of integers.

Below is an example of sorting a C++ STL list of C++ strings:

```
// sort_list_of_string.cpp //
#include <iostream>
//#include <algorithm> // not needed for list sort
#include <string>
#include <list>
using namespace std;
ostream& operator << (ostream& os, const list< string >& ml )
{
list< string >::const_iterator it;
for( it = ml.begin() ; it != ml.end() ; ++ it )
os << *it << ' ';
return os;
}
bool myLenCmp( const string& a, const string& b )
{
return a.size() < b.size() ;
}
int main ()
{
list< string > myList; // construct an empty list ...
myList.push_back( "Joe" );
myList.push_back( "Sue" );
myList.push_back( "Annie" );
myList.push_back( "Zoe" );
myList.push_back( "Lynne" );
cout << "myList unsorted: \n" << myList << endl;
// call list sort and compare strings in dictionary order
myList.sort();
cout << "myList dictionary sorted: \n" << myList << endl;
// call list sort and compare strings in len order
myList.sort( myLenCmp );
cout << "myList length sorted: \n" << myList << endl;
return 0;
}
```

*Edited 2 Years Ago by David W*: cleaned up comments

0

Personally I would recomend the Comb Sort algorithm rather than bubble sort which tends to be more efficient in the average case than Bubble sort but isn't that much more complex.

I have used it on a microprocessor (PIC) implementation with limited flash and ram (the list being sorted took around 75% of the available ram).

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...