# Thread: function to sort array

1. ## function to sort array

Hi,
I have written code to sort array numbers in increasing order

Code:
```#include <stdio.h>

int main()
{
int i = 0;
int j = 0;
int temp;

int array = { 50, 40, 30, 20, 10};

for ( i = 0; i < 5; i++)
{
for ( j = i + 1; j < 5; j++)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}

}

for ( i = 0; i < 5; i++)
{
printf(" %d ", array[i]);
}

return 0;

}```

I am now trying to make a function to sort array in increasing order

Code:
```#include <stdio.h>

void foo ( int *ptr)
{
// How to sort array in increasing order in function

}

int main()
{
int i = 0;
int j = 0;
int temp;

int array = { 50, 40, 30, 20, 10};

foo(&array);

// print array after calling function
for ( i = 0; i < 5; i++)
{
printf(" %d ", array[i]);
}

return 0;

}```

I am looking help to make function so how to make function that sort array in increasing order 2. Before you do that, you should first test your main program. Try mixing up the values in the array you already have -- 20, 40, 50, 30, 10; then 40, 10, 30, 20, 50; etc. This will make sure that your algorithm doesn't have any subtle bugs in it, before you start making changed.

(PS: your algorithm has bugs in it.) 3. How does this differ from your previous question?
swapping array value 4. i was sorting the array in main function and now i want to create a function which can sort the array increasing order 5. Again, already showed you how to copy code from main to inside a function.
function for largest value 6. Oh, it is very simple:
Code:
```// Generic selection sort function.
// Works like qsort().
void selection_sort( void *ptr,
size_t elems, size_t elem_size,
int (*cmp)(const void *, const void *) )
{ Popular pages Recent additions array, function, int, order, sort 