Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ sorting numbers from smallest to biggest

If I have the user enter 10 random numbers and I want to order them from smallest to biggest what is the best method to do this using the most basic C++ language.

like image 644
Oliver Avatar asked Feb 24 '11 08:02

Oliver


People also ask

How do you sort an array by smallest to largest?

The sort() method allows you to sort elements of an array in place. Besides returning the sorted array, the sort() method changes the positions of the elements in the original array. By default, the sort() method sorts the array elements in ascending order with the smallest value first and largest value last.

What is ascending order in C?

Each element is identified by using an "array index". Accessing array elements is easy by using the array index. The logic we use to sort the array elements in ascending order is as follows − for (i = 0; i < n; ++i){ for (j = i + 1; j < n; ++j){ if (num[i] > num[j]){ a = num[i]; num[i] = num[j]; num[j] = a; } } }


1 Answers

std::vector<int> numbers;

// get the numbers from the user here.    

std::sort(numbers.begin(), numbers.end());
like image 97
Jerry Coffin Avatar answered Oct 19 '22 19:10

Jerry Coffin