-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsortP.c
144 lines (136 loc) · 3.37 KB
/
sortP.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <Python.h>
#include <math.h>
static PyObject *bubblesort(PyObject *self, PyObject *args){
PyObject* list;
int i, L;
double res;
double tmp;
if (!PyArg_ParseTuple(args, "O", &list)){
return NULL;
}
L = PyList_GET_SIZE(list);
double *A = (double**)malloc(L * sizeof(double*));
for (i=0; i<L; i++){
PyObject* item = PySequence_Fast_GET_ITEM(list, i);
res = PyFloat_AsDouble(item);
A[i] = res;
}
_Bool noSwap;
for (int i = L - 1; i >= 0; i--){
noSwap = 1;
for (int j = 0; j < i; j++){
if (A[j] > A[j + 1]){
tmp = A[j];
A[j] = A[j + 1];
A[j + 1] = tmp;
noSwap = 0;
}
}
if (noSwap == 1)
break;
}
PyObject *my_list = PyList_New(L);
for (i=0; i<L; i++)
PyList_SetItem(my_list, i, PyFloat_FromDouble(A[i]));
return my_list;
free(A);
}
static PyObject *insertionsort(PyObject *self, PyObject *args){
PyObject* list;
int i, N, location;
double res;
double newElement;
if (!PyArg_ParseTuple(args, "O", &list)){
return NULL;
}
N = PyList_GET_SIZE(list);
double *mass = (double**)malloc(N * sizeof(double*));
for (i=0; i<N; i++){
PyObject* item = PySequence_Fast_GET_ITEM(list, i);
res = PyFloat_AsDouble(item);
mass[i] = res;
}
for (i = 1; i < N; i++){
newElement = mass[i];
location = i - 1;
while(location >= 0 && mass[location] > newElement){
mass[location+1] = mass[location];
location = location - 1;
}
mass[location+1] = newElement;
}
PyObject *my_list = PyList_New(N);
for (i=0; i<N; i++)
PyList_SetItem(my_list, i, PyFloat_FromDouble(mass[i]));
return my_list;
free(mass);
}
static PyObject *shellsort(PyObject *self, PyObject *args){
PyObject* list;
int i, j, N, step;
double res;
double tmp;
if (!PyArg_ParseTuple(args, "O", &list)){
return NULL;
}
N = PyList_GET_SIZE(list);
double *mass = (double**)malloc(N * sizeof(double*));
for (i=0; i<N; i++){
PyObject* item = PySequence_Fast_GET_ITEM(list, i);
res = PyFloat_AsDouble(item);
mass[i] = res;
}
for (step = N / 2; step > 0; step /= 2){
for (i = step; i < N; i++){
tmp = mass[i];
for (j = i; j >= step; j -= step){
if (tmp < mass[j - step])
mass[j] = mass[j - step];
else
break;
}
mass[j] = tmp;
}
}
PyObject *my_list = PyList_New(N);
for (i=0; i<N; i++)
PyList_SetItem(my_list, i, PyFloat_FromDouble(mass[i]));
return my_list;
free(mass);
}
static PyMethodDef ownmod_methods[] = {
{
"shellsort",
shellsort,
METH_VARARGS,
"shellsort"
},
{
"bubblesort",
bubblesort,
METH_VARARGS,
"bubblesort"
},
{
"insertionsort",
insertionsort,
METH_VARARGS,
"insertionsort"
},
{ NULL, NULL, 0, NULL }
};
static PyModuleDef simple_module = {
PyModuleDef_HEAD_INIT,
"sortP",
"sortP",
-1,
ownmod_methods, // methods are here
NULL,NULL,NULL
};
PyMODINIT_FUNC PyInit_sortP(void)
{
PyObject* m;
m = PyModule_Create(&simple_module);
if (m == NULL)
return NULL;
return m;}