-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquick.h
71 lines (63 loc) · 1.43 KB
/
quick.h
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef QUICK_H
#define QUICK_H
#define SWAPInt(x,y) {int t; t = x; x = y; y = t;}
void SWAPString(char *x, char *y)
{
char *tmp = malloc(sizeof(char)*101);
strcpy(tmp, x);
strcpy(x,y);
strcpy(y,tmp);
free(tmp);
}
int partitionInt(int *number, int left, int right)
{
int i = left - 1;
int j;
for(j = left; j < right; j++)
{
if(*(number+j) <= *(number+right))
{
i++;
SWAPInt(*(number+i), *(number+j));
}
}
SWAPInt(*(number+i+1), *(number+right));
return i+1;
}
int partitionString(char **str, int left, int right)
{
int i = left - 1;
int j;
for(j = left; j < right; j++)
{
if(strcmp(*(str+j),*(str+right))<=0)
{
i++;
SWAPString(*(str+i), *(str+j));
}
}
SWAPString(*(str+i+1), *(str+right));
return i+1;
}
void quickSortInt(int *number, int left, int right)
{
if(left < right)
{
int q = partitionInt(number, left, right);
quickSortInt(number, left, q-1);
quickSortInt(number, q+1, right);
}
}
void quickSortString(char **str, int left,int right)
{
if(left<right)
{
int q = partitionString(str, left, right);
quickSortString(str, left, q-1);
quickSortString(str, q+1, right);
}
}
#endif