-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataStructAndAlgorithm.cpp
162 lines (151 loc) · 2.61 KB
/
DataStructAndAlgorithm.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#define _CRT_SECURE_NO_WARNINGS
#include "DataStructAndAlgorithm.h"
#include <string>
using namespace std;
void bubble(int *arr, int n) {
bool flag = false;
for (int i = n; i > 0; i--)
{
for (int j = 1; j < i; j++)
{
if (arr[j] < arr[j - 1])
{
swap(arr[j], arr[j - 1]);
flag = true;
}
}
if (!flag)
{
break;
}
flag = false;
}
}
void insert_sort(int *arr, int n)
{
int j;
for (int i = 1; i < n; i++)
{
int temp = arr[i];
for (j = i; j > 0 && arr[j - 1] > temp; j--)
{
arr[j] = arr[j - 1];
}
arr[j] = temp;
}
}
void shell_sort(int *arr, int n)
{
int j;
for (int D = n / 2; D > 0; D /= 2)
{
for (int i = 1; i < n; i+=D)
{
int temp = arr[i];
for (j = i; j > 0 && arr[j - D] > temp; j-= D)
{
arr[j] = arr[j - D];
}
arr[j] = temp;
}
}
}
void merge(int *arr, int *temp, int L, int R, int RightEnd)
{
int length = RightEnd - L + 1;
int Ti = L, LeftEnd = R - 1;
while(L <= LeftEnd && R <= RightEnd)
{
if (arr[L] <= arr[R])
{
temp[Ti++] = arr[L ++];
}
else
{
temp[Ti++] = arr[R ++];
}
}
while (L <= LeftEnd)
{
temp[Ti++] = arr[L++];
}
while (R <= RightEnd)
{
temp[R++] = arr[R++];
}
for (int i = 0; i < length; i++ , RightEnd --)
{
arr[RightEnd] = temp[RightEnd];
}
}
void merge_sort(int *arr , int *temp, int start, int end)
{
if (start < end)
{
int center = (end + start) / 2;
merge_sort(arr,temp, start, center);
merge_sort(arr,temp,center + 1, end);
merge(arr,temp, start, center + 1, end);
}
}
int median_three(int *arr,int left,int right)
{
int center = (left + right) / 2;
if (arr[left] > arr[center])
{
swap(arr[left], arr[center]);
}
if (arr[left] > arr[right])
{
swap(arr[left], arr[right]);
}
if (arr[center] > arr[right])
{
swap(arr[center], arr[right]);
}
swap(arr[center], arr[right - 1]);
return arr[right - 1];
}
int cutoff = 3;
void quick_sort(int *arr,int left , int right)
{
if (cutoff < right - left)
{
int pivot = median_three(arr,left,right);
int i = left;
int j = right - 1;
while (arr[++i] <= pivot) {}
while (arr[--j] >= pivot) {}
if (i < j) {
swap(arr[i], arr[j]);
}
swap(arr[i], arr[right - 1]);
quick_sort(arr, left, i - 1);
quick_sort(arr, i + 1, right);
}
else
{
insert_sort(arr + left,right - left + 1);
}
}
int main() {
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
int n;
cin >> n;
int *arr = new int[n];
int temp;
for (int i = 0; i < n; i++)
{
cin >> temp;
arr[i] = temp;
}
int *temp_arr = new int[n];
quick_sort(arr, 0, n - 1);
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
delete[] temp_arr;
return 0;
}