+ 8
How to sort an array ?
5 Respuestas
+ 14
https://www.sololearn.com/learn/774/?ref=app
+ 12
don't forget to write import statememt "import java.util.Arrays;"
Arrays.sort(array_name); //to sort array directly
//in java
+ 8
That depends much on the language you are using. For the ones on your profile:
Python:
It is as simple as to call "sort" on your array (list)
a = [4,2,3]
a.sort()
C++:
You can use the sorting algorithms in the algorithm header:
#include <algorithm>
int a[] = {4,2,3};
sort(begin(a), end(a));
C:
It is a little more complicated because you have to pass the comparison function along with it. There is a qsort algorithm in the stdlib header:
#include <stdlib.h>
// comparison function
int cmp(void*x, void*y)
{
return *(int*)x - *(int*)y;
}
int a[] = {4,2,3};
qsort(a, 3, sizeof(int), cmp);
+ 6
In Java:
Arrays.sort(arr);
In C#:
Array.Sort(arr);