-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sort.cpp
80 lines (72 loc) · 1006 Bytes
/
Sort.cpp
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
#include "Sort.h"
#include "Sort.h"
int * BubbleSort(int * data, int len)
{
int t = 0;
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (data[j]<data[j + 1])
{
t = data[j + 1];
data[j + 1] = data[j];
data[j] = t;
}
}
}
return data;
}
int * QuickSort(int * data, int len)
{
if (len == 1)
{
return data;
}
if (len == 2)
{
int t = data[0];
if (data[0] > data[1])
{
t = data[0];
data[0] = data[1];
data[1] = t;
}
return data;
}
int pivot = data[0];
int hole = 0;
int p = 1;
int q = len - 1;
while (q!=hole)
{
for (int j = q; j > 0; j--)
{
if (data[j] < pivot)
{
data[hole] = data[j];
hole = j;
p++;
break;
}
else
{
q--;
}
}
for (int i = p-1; i < q; i++)
{
if (data[i] > pivot)
{
data[hole] = data[i];
hole = i;
q--;
break;
}
}
}
data[hole] = pivot;
QuickSort(data, hole + 1);
QuickSort(data+hole+1, len-hole-1);
return data;
}