I am trying to develop a C module for applying a selection sort algorithm to a list of data and returning the sorted list back to the Python program as a Python list format.
However, I found that the result of the program below will be something like
[431863472]
#include <Python.h>
int sorting(int n[])
{
for (int i=0; i < sizeof(n)-1; i++){
int minpos = i;
for (int j=0; j < sizeof(n)-1; j++){
if (n[j] < n[minpos]){
minpos = j;
}
int temp = n[i];
n[i] = n[minpos];
n[minpos] = temp;
}
}
return n;
}
static PyObject* selectSort(PyObject* self, PyObject* args)
{
// instantiate our `n` value
int n[20000];
// if our `n` value
if(!PyArg_ParseTuple(args, "O", &n))
return NULL;
return Py_BuildValue("[i]", sorting(n));
}
static PyMethodDef myMethods[] = {
{ "selectSort", selectSort, METH_VARARGS, "Applying selection sort" },
{ NULL, NULL, 0, NULL }
};
static struct PyModuleDef myModule = {
PyModuleDef_HEAD_INIT,
"myModule",
"Test Module",
-1,
myMethods
};
PyMODINIT_FUNC PyInit_myModule(void)
{
return PyModule_Create(&myModule);
}
Is it the problem of passing the array to the python? Or I should not use "O" or "[i]"?