Sort.h 4.9 KB
Newer Older
7
Update  
7wc98#14 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#pragma once

// 直接插入排序
void InsertSortDirectly(int a[], int len)
{
	int i, j;
	for (i = 2; i <= len; i++)
	{
		if (a[i] < a[i - 1])
		{
			// 复制为哨兵
			a[0] = a[i];
			for (j = i - 1; a[0] < a[j]; --j)
				a[j + 1] = a[j];
			a[j + 1] = a[0];
		}
	}
7
Update  
7wc98#14 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
}
// 折半插入排序
void InsertSortUndirectly(int a[], int len)
{
	int i, j, low, high, mid;
	for (i = 2; i <= len; i++)
	{
		a[0] = a[i];
		low = 1;
		high = i - 1;
		while (low <= high)
		{
			mid = (low + high) / 2;
			if (a[mid] > a[0]) high = mid - 1;
			else low = mid + 1;
		}
		for (j = i - 1; j >= high + 1; --j)
			a[j + 1] = a[j];
		a[high + 1] = a[0];
	}
7
Update  
7wc98#14 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
}
// 希尔排序
void ShellSort(int a[], int len)
{
	int dk, i, j;
	for (dk = len / 2; dk >= 1; dk = dk / 2) // dk 表示步长
	{
		for (i = dk + 1; i <= len; ++i)
		{
			if (a[i] < a[i - dk]) // 将 a[i] 插入有序增量子表
			{
				a[0] = a[i];
				for (j = i - dk; j > 0 && a[0] < a[j]; j -= dk)
					a[j + dk] = a[j];   //记录后移,查找插入位置
				a[j + dk] = a[0];
			}
		}
	}
7
7wc98#14 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
}
// 冒泡排序
void BubbleSort(int a[], int len)
{
	// 标志是否发生交换
	bool flag = false;
	for (int i = 0; i < len - 1; i++)
	{
		flag = false;
		for (int j = len - 1; j > i; j--)
		{
			if (a[j - 1] > a[j])
			{
				int t = a[j - 1];
				a[j - 1] = a[j];
				a[j] = t;
			}
			flag = true;
		}
		if (flag == false)
			return;
	}
7
7wc98#14 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
}
// 快速排序
int Partition(int a[], int low, int high)
{
	int pivot = a[low];
	while (low < high)
	{
		while (low < high && a[high] >= pivot) --high;
		a[low] = a[high];
		while (low < high && a[low] <= pivot) ++low;
		a[high] = a[low];
	}
	a[low] = pivot;
	return low;
}
void QuickSort(int a[], int low, int high)
{
	if (low < high)
	{
		// 划分
		int pivotpos = Partition(a, low, high);
		QuickSort(a, low, pivotpos - 1);
		QuickSort(a, pivotpos + 1, high);
	}
7
7wc98#14 已提交
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
}
// 双向冒泡排序
void DoubleBubbleSort(int a[], int n)
{
	int low = 0, high = n - 1;
	bool flag = true;
	while (low < high&& flag)
	{
		flag = false;
		for (int i = low; i < high; i++)
		{
			if (a[i] > a[i + 1])
			{
				int t = a[i];
				a[i] = a[i + 1];
				a[i + 1] = t;
				flag = true;
			}
		}
		high--;
		for (int j = high; j > low; j--)
		{
			if (a[j] < a[j - 1])
			{
				int t = a[j];
				a[j] = a[j - 1];
				a[j - 1] = t;
				flag = true;
			}
		}
		low++;
	}
}
// 将所有奇数移动到偶数前边
void Move(int a[], int len)
{
	int i = 0, j = len - 1;
	while (i < j)
	{
		while (i < j && a[i] % 2 != 0)i++;
		while (i < j && a[i] % 2 != 1)j--;
		if (i < j)
		{
			int t = a[i];
			a[i] = a[j];
			a[j] = t;
		}
		i++;
		j--;
	}
}
//随机选取轴枢元素的快速排序
int Partition2(int a[], int low, int high)
{
	int rand_index = low + rand() % (high - low + 1);
	int t = a[rand_index];
	a[rand_index] = a[low];
	a[low] = t;
	int i = low;
	int pivot = a[low];
	for (int j = low + 1; j <= high; j++)
	{
		if (a[j] < pivot)
		{
			t = a[++i];
			a[i] = a[j];
			a[j] = t;
		}
	}
	t = a[i];
	a[i] = a[low];
	a[low] = t;
	return i;
}
void QuickSort2(int a[], int low, int high)
{
	if (low < high)
	{
		// 划分
		int pivotpos = Partition2(a, low, high);
		QuickSort(a, low, pivotpos - 1);
		QuickSort(a, pivotpos + 1, high);
	}
7
Update  
7wc98#14 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
}
// 简单选择排序
void SelectSort(int a[], int n)
{
	int min;
	for (int i = 0; i < n - 1; i++)
	{
		min = i;
		for (int j = i + 1; j < n; j++)
		{
			if (a[j] < a[min])min = j;
		}
		if (min != i)
		{
			int t = a[i];
			a[i] = a[min];
			a[min] = t;
		}
	}
7
Update  
7wc98#14 已提交
204
}
7
Update  
7wc98#14 已提交
205
//小堆跟
7
Update  
7wc98#14 已提交
206
// 堆排序:①调整
7
Update  
7wc98#14 已提交
207
void MinHeapAdjust(int a[], int k, int len)
7
Update  
7wc98#14 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
{
	a[0] = a[k];
	int i;
	for (i = 2 * k; i <= len; i *= 2)
	{
		if (i < len&& a[i] < a[i + 1])
			i++;
		if (a[0] >= a[i])break;
		else {
			a[k] = a[i];
			k = i;
		}
	}
	a[k] = a[0];
}
//堆排序:②构建初始堆
7
Update  
7wc98#14 已提交
224
void CreateMinHeap(int a[], int len)
7
Update  
7wc98#14 已提交
225 226
{
	for (int i = len / 2; i > 0; i--)
7
Update  
7wc98#14 已提交
227
		MinHeapAdjust(a, i, len);
7
Update  
7wc98#14 已提交
228 229
}
//堆排序:③排序
7
Update  
7wc98#14 已提交
230
void MinHeapSort(int a[], int len)
7
Update  
7wc98#14 已提交
231
{
7
Update  
7wc98#14 已提交
232
	CreateMinHeap(a, len);
7
Update  
7wc98#14 已提交
233 234 235 236
	for (int i = len; i > 1; i--)
	{
		int t = a[i];
		a[i] = a[1];
7
Update  
7wc98#14 已提交
237 238
		a[1] = t;
		MinHeapAdjust(a, 1, i - 1);
7
Update  
7wc98#14 已提交
239 240
	}
}
7
Update  
7wc98#14 已提交
241
// 大堆根
7
Update  
7wc98#14 已提交
242
// 调整
7
Update  
7wc98#14 已提交
243
void MaxHeapAdjust(int a[], int k, int len)
7
Update  
7wc98#14 已提交
244 245 246 247 248
{
	a[0] = a[k];
	int i;
	for (i = 2 * k; i <= len; i *= 2)
	{
7
Update  
7wc98#14 已提交
249
		if (i < len && a[i] > a[i + 1])
7
Update  
7wc98#14 已提交
250
			i++;
7
Update  
7wc98#14 已提交
251
		if (a[0] <= a[i])break;
7
Update  
7wc98#14 已提交
252 253 254 255 256 257 258 259
		else {
			a[k] = a[i];
			k = i;
		}
	}
	a[k] = a[0];
}
// 构造初始堆
7
Update  
7wc98#14 已提交
260
void CreateMaxHeap(int a[], int len)
7
Update  
7wc98#14 已提交
261 262
{
	for (int i = len / 2; i > 0; i--)
7
Update  
7wc98#14 已提交
263
		MaxHeapAdjust(a, i, len);
7
Update  
7wc98#14 已提交
264
}
7
Update  
7wc98#14 已提交
265 266
// 大堆根排序
void MaxHeapSort(int a[], int len)
7
Update  
7wc98#14 已提交
267
{
7
Update  
7wc98#14 已提交
268
	CreateMaxHeap(a, len);
7
Update  
7wc98#14 已提交
269 270 271 272
	for (int i = len; i > 1; i--)
	{
		int t = a[i];
		a[i] = a[1];
7
Update  
7wc98#14 已提交
273 274
		a[1] = t;
		MaxHeapAdjust(a, 1, i - 1);
7
Update  
7wc98#14 已提交
275
	}
7
update  
7wc98#14 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
}
// 二路归并排序
int* b = new int[20];
void Merge(int a[], int low, int mid, int high)
{
	int i, j, k;
	for (k = low; k <= high; k++)
		b[k] = a[k];
	for (i = low, j = mid + 1, k = i; i <= mid && j <= high; k++)
	{
		if (b[i] <= b[j])
			a[k] = b[i++];
		else
			a[k] = b[j++];
	}
	while (i <= mid) a[k++] = b[i++];
	while (j <= high)a[k++] = b[j++];
}
void MergeSort(int a[], int low, int high)
{
	if (low < high)
	{
		int mid = (low + high) / 2;
		MergeSort(a, low, mid);
		MergeSort(a, mid + 1, high);
		Merge(a, low, mid, high);
	}
7
Update  
7wc98#14 已提交
303
}