Let say I have a line for numbers

2 4 5 8 7 9 1 3

and i want to sort them in numerical order how would i do this? I found a similar topic but it was of no help someone said to do bubble sort and then everyone bashed him for doing so and no provided what a bubble sort or any other sorting algo structure looked like it

If you just want to get numbers sorted a convenient solution is to use the STL sort() function. Look it up here
STL Sort

However if you want to implement your own algorithm, well, there are several algorithms with pros and cons which would depend on your requirements. The quick sort is a good place to start.

Heres a basic snippet

while(exit == false)

flag = true

for(x = 0; x = length-1; x++) //length as in actual length; for a[5] it is 4
     if(array[x] < array[x+1]) {
        temp = array[x];
        array[x] = array[x+1];
        array[x+1] = temp;
}

for(x = 0; x < length-1; x++)
      if(array[x] < array[x+1])
          flag = false;

if(flag == true)
exit = true;
}

Here's a shell sort algorithm:

template<typename T >
void prog< T >::shellSort(T * const array, int size)
{
	int i, j, increment;
	T temp;
	increment = size / 2;
	while (increment > 0) {
		for (i = increment; i < size; i++) {
			j = i;
			temp = array[i];
			while ((j >= increment) && (array[j-increment] > temp)) {
				array[j] = array[j - increment];
				j = j - increment;
			}
			array[j] = temp;

		}

	if (increment == 2)
		increment = 1;
	else
		increment = (int) (increment / 2.2);
	}
}//shellSort

The sorting algorithm you should choose depends on the number of elements you want to sort:

A bubble sort is good if you have few elements, let say less than 30-40. Theorically, it is faster that more complex sorts (like the quicksort). If you want a good sorting strategy for much greater sets of data, I suggest the quicksort. It is easy to understand and it runs in O(n lg n) polynomial time.

This is more information on the quicksort: http://en.wikipedia.org/wiki/Quicksort

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.