-
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[5] = { 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[5] = { 50, 40, 30, 20, 10};
foo(&array[0]);
// 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
-
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.)
-
How does this differ from your previous question?
swapping array value
-
i was sorting the array in main function and now i want to create a function which can sort the array increasing order
-
Again, already showed you how to copy code from main to inside a function.
function for largest value
-
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 *) )
{
// YOUR code here
}