数据结构之排序

1.插入排序

第一种:直接插入排序–稳定,时间复杂度为O( n 2 n^2 n2

直接插入排序示例代码一:有哨兵

void InsertSort(int a[], int size)
{//a[0]是哨兵
	int i,j;
	for (i = 2;i <= size;++i)
	{
		if (a[i] < a[i - 1])
		{
			a[0] = a[i];
			a[i] = a[i - 1];
			for (j = i - 2;a[0] < a[j];--j)
			{
				a[j + 1] = a[j];
			}
			a[j + 1] = a[0];
		}
	}
}
void main()
{
	int a[11] = { 0,1,233,4,2,45,3,56,7,1,12 };
	InsertSort(a,10);
	for (int i = 0;i < 11;i++)
		cout << a[i] << "  ";
}

直接插入排序示例代码二:重要

#include <iostream>

using namespace std;

void insert_sort(int a[], int n)
{
	int i, j, temp;

	for (i = 1;i < n;i++)
	{
		temp = a[i];
		for (j = i - 1;j >= 0 && temp < a[j];j--)
		{
			a[j + 1] = a[j];
		}
		a[j + 1] = temp;
	}
}

void print_array(int a[], int len)
{
	for (int i = 0;i < len;i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}

int main(int agrc, char* argv[])
{
	int a[] = { 7,3,5,8,9,1,2,4,6 };
	cout << "before insert sort: ";
	print_array(a, 9);
	insert_sort(a, 9);
	cout << "after insert sort: ";
	print_array(a, 9);
	return 0;
}

第二种:折半插入排序–时间复杂度为O( n 2 n^2 n2

折半插入排序示例代码一:了解

#include <iostream>
using namespace std;
void InsertSort(int a[], int size)
{//a[0]是哨兵
	int i;
	int low, high,mid;
	for (i = 2;i <= size;++i)
	{
		a[0] = a[i];
		low = 1;high = i - 1;
		while (low <= high)
		{
			mid = (low + high) / 2;
			if (a[0] < a[mid])
				high = mid - 1;
			else
				low = mid + 1;
		}
		for (int z = i - 1;z >= high + 1;--z)
			a[z + 1] = a[z];
		a[high + 1] = a[0];
		
	}
}

void main()
{
	int a[11] = { 0,1,233,4,2,45,3,56,7,1,12 };
	InsertSort(a,10);
	for (int i = 0;i < 11;i++)
		cout << a[i] << "  ";
}

第三种:希尔排序–不稳定,时间复杂度O( n 1.3 − 2 n^{1.3-2} n1.32

希尔排序示例代码一:有暂存空间

#include <iostream>
using namespace std;
void ShellInsert(int L[],int length, int dk)
{//L[0]只是暂存单元,不是哨兵。当就<=0时,插入位置已找到
	int i,j;
	for (i = 1 + dk;i <= length;++i)
	{
		if (L[i] < L[i - dk])
		{
			L[0] = L[i];
			for (j = i - dk;(j > 0) && (L[0] < L[j]);j -= dk)
			{
				L[j + dk] = L[j];
			}
			L[j + dk] = L[0];
		}
	}
}
void ShellSort(int L[], int size, int dlta[], int t)
{
	int k;
	for (k = 0;k < t;k++)
	{
		ShellInsert(L,size, dlta[k]);
	}
}

void main()
{
	int a[11] = { 0,1,233,4,2,45,3,56,7,1,12 };
	int dk[] = {5,3,1};
	ShellSort(a, 10, dk, 3);
	for (int i = 0;i < 11;i++)
		cout << a[i] << "  ";
}

希尔排序示例代码二:重要

#include <iostream>

using namespace std;

void shell_sort(int a[], int len)
{
	int h, i, j, temp;
	for (h = len / 2;h > 0;h = h / 2)
	{
		for (i = h;i < len;i++)
		{
			temp = a[i];
			for (j = i - h;j >= 0 && temp < a[j];j -= h)
			{
				a[j + h] = a[j];
			}
			a[j + h] = temp;
		}
	}
}

void print_array(int a[], int len)
{
	for (int i = 0;i < len;i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}

int main(int agrc, char* argv[])
{
	int a[] = { 7,3,5,8,9,1,2,4,6 };
	cout << "before shell sort: ";
	print_array(a, 9);
	shell_sort(a, 9);
	cout << "after shell sort: ";
	print_array(a, 9);
	return 0;
}

2.交换排序

第一种:起泡排序–稳定,时间复杂度为O( n 2 n^2 n2

起泡排序示例代码一:重要

#include <iostream>
using namespace std;
void BubbleSort(int a[], int n)
{
	bool flag;
	int i, j;
	for ( i = 0;i < n - 1;i++)
	{
		flag = false;
		for (j = n - 1;j>i;j--)
		{
			if (a[j - 1] > a[j])
			{
				swap(a[j - 1], a[j]);
				flag = true;
			}
		}
		if (flag == false)
			return;
	}
}
void main()
{
	int a[11] = { 0,1,233,4,2,45,3,56,7,1,12 };
	BubbleSort(a, 11);
	for (int i = 0;i < 11;i++)
		cout << a[i] << "  ";
}

第二种:快速排序–不稳定,时间复杂度O( n l o g 2 n n{log_2{n}} nlog2n

快速排序示例代码一:有暂存空间

#include <iostream>
using namespace std;
int Partition(int L[], int low, int high)
{
	int pivotkey;
	L[0] = L[low];
	pivotkey = L[low];
	while (low < high)
	{
		while (low < high&&L[high] >= pivotkey)
			--high;
		L[low] = L[high];
		while (low < high&&L[low] <= pivotkey)
			++low;
		L[high] = L[low];
	}
	L[low] = L[0];
	return low;
}
void QSort(int L[], int low, int high)
{
	int pivotloc;
	if (low < high)
	{
		pivotloc = Partition(L, low, high);
		QSort(L,low,pivotloc-1);
		QSort(L, pivotloc + 1, high);
	}
}
void main()
{
	int a[11] = { 0,1,233,4,2,45,3,56,7,1,12 };
	QSort(a, 1, 10);
	for (int i = 0;i < 11;i++)
		cout << a[i] << "  ";
}

快速排序示例代码二:重要,随机产生比较对象

#include <iostream>
#include <time.h>
#include <algorithm>

using namespace std;

int RandomInRange(int start, int end)
{
	srand(time(NULL));
	return rand() % (end - start + 1) + start;
}

int Partition(int data[], int length, int start, int end)
{
	if (data == nullptr || length <= 0 || start < 0 || end >= length)
	{
		throw new std::exception("Invalid Parameters.");
	}
	int index = RandomInRange(start, end);
	swap(data[index], data[end]);

	int small = start - 1;
	for (index = start;index < end;++index)
	{
		if (data[index] < data[end])
		{
			++small;
			if (small != index)
			{
				swap(data[small], data[index]);
			}
		}
	}
	++small;
	swap(data[small], data[end]);
	return small;
}

void QuickSort(int data[],int length ,int start, int end)
{
	if (start == end)
	{
		return;
	}
	int index = Partition(data, length, start, end);
	if (index > start)
	{
		QuickSort(data, length, start, index - 1);
	}
	if (index < end)
	{
		QuickSort(data, length, index + 1, end);
	}
}

int main()
{
	int a[11] = { 0,1,233,4,2,45,3,56,7,1,12 };
	QuickSort(a, 11, 1, 10);
	for (int i = 0;i < 11;i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
	return 0;
}

快速排序示例代码三:重要,三点中值

#include <iostream>
#include <time.h>
#include <algorithm>
using namespace std;

int MidOfThree(int a[],int start, int end)
{
	int mid = start + (end - start) / 2;
	return a[start] > a[mid] ? (a[mid]>a[end]?mid:(a[start]>a[end]?end:start)) : (a[start]>a[end]?start:(a[mid]>a[end]?end:mid));
}

int Partition(int data[], int length, int start, int end)
{
	if (data == nullptr || length <= 0 || start < 0 || end >= length)
	{
		throw new exception("Invalid Parameters.");
	}
	int index = MidOfThree(data, start, end);
	swap(data[index], data[end]);
	int small = start - 1;
	for (index = start;index < end;++index)
	{
		if (data[index] < data[end])
		{
			++small;
			if (small != index)
			{
				swap(data[small], data[index]);
			}
		}
	}
	++small;
	swap(data[small], data[end]);
	return small;
}

void QuickSort(int data[], int length, int start, int end)
{
	if (start == end)
	{
		return;
	}
	int index = Partition(data, length, start, end);
	if (index > start)
	{
		QuickSort(data, length, start, index - 1);
	}
	if (index < end)
	{
		QuickSort(data, length, index + 1, end);
	}
}

void quick_sort(int a[], int length)
{
	QuickSort(a, length, 0, length - 1);
}

void main()
{
	int a[10] = { 0,111,233,14,10,45,13,56,17,1};
	quick_sort(a, 10);
	for (int i = 0;i < 10;i++)
		cout << a[i] << "  ";
	cout << endl;
	int b[11] = { 0,111,233,14,10,45,13,56,17,1,12 };
	quick_sort(b, 11);
	for (int i = 0;i < 11;i++)
		cout << b[i] << "  ";
	cout << endl;
	int c[8] = { 8,6,1,3,5,2,7,4 };
	quick_sort(c, 8);
	for (int i = 0;i < 8;i++)
		cout << c[i] << "  ";
	cout << endl;

}

快速排序示例代码四:一个函数

#include <iostream>

using namespace std;

void quick_sort(int a[], int low, int high)
{
	int i, j, pivot;
	if (low < high)
	{
		pivot = a[low];
		i = low;
		j = high;
		while (i < j)
		{
			while (i<j&&a[j]>=pivot)
				--j;
			if (i < j)
				a[i++] = a[j];
			while (i < j&&a[i] <= pivot)
				++i;
			if (i < j)
				a[j--] = a[i];
		}
		a[i] = pivot;
		quick_sort(a, low, i - 1);
		quick_sort(a, i + 1, high);
	}
}

int main(int agrc, char* argv[])
{
	int data[9] = { 54,38,96,23,15,72,60,45,83 };
	quick_sort(data, 0, 8);
	for (int i = 0;i < 9;i++)
	{
		cout << data[i] << " ";
	}
	return 0;
}

3.选择排序

第一种: 简单选择排序–不稳定L={2,2,1}->{1,2,2} O( n 2 n^2 n2)

简单选择排序示例代码一:重要

#include <iostream>
using namespace std;
void SelectSort(int A[], int n)
{
	int min,i,j;
	for (i = 0;i < n - 1;i++)
	{
		min = i;
		for (j = i + 1;j < n;j++)
			if (A[j] < A[min]) min = j;
		if (min != i)
			swap(A[i], A[min]);
	}
}
void main()
{
	int a[11] = { 0,1,233,4,2,45,3,56,7,1,12 };
	SelectSort(a, 11);
	for (int i = 0;i < 11;i++)
		cout << a[i] << "  ";
}

第二种:堆排序–不稳定 时间复杂度O( n l o g 2 n n{log_2{n}} nlog2n)

堆排序示例代码一:a[0]是辅助空间,堆顶的序号为1

#include <iostream>

using namespace std;

#include <iostream>
using namespace std;
void HeapAdjustDown(int a[], int k, int len)//大根堆
{
	a[0] = a[k];
	for (int i = 2 * k;i < len;i *= 2)
	{
		if ((i + 1) < len&&a[i] < a[i + 1])
			i++;
		if (a[0] >= a[i])
			break;
		else
		{
			a[k] = a[i];
			k = i;
		}
	}
	a[k] = a[0];
}
void HeapSort(int a[], int len)
{
	for (int i = (len - 1) / 2;i > 0;i--)
	{
		HeapAdjustDown(a, i, len);
	}
	for (int j = len - 1;j > 1;j--)
	{
		swap(a[j], a[1]);
		HeapAdjustDown(a, 1, j );
	}
}
void main()
{
	int a[10] = { 0,111,233,14,10,45,13,56,17,1};
	HeapSort(a, 10);
	for (int i = 0;i < 10;i++)
		cout << a[i] << "  ";
}

堆排序示例代码二:堆顶的序号为0

#include <iostream>

using namespace std;

#include <iostream>
using namespace std;
void HeapAdjustDown(int a[], int k, int len)//大根堆
{
	int temp = a[k];
	for (int i = 2 * k + 1;i < len;i = 2 * i + 1)
	{
		if ((i + 1) < len&&a[i] < a[i + 1])
			i++;
		if (temp >= a[i])
			break;
		else
		{
			a[k] = a[i];
			k = i;
		}
	}
	a[k] = temp;
}
void HeapSort(int a[], int len)
{
	for (int i = (len - 2) / 2;i >= 0;i--)
	{
		HeapAdjustDown(a, i, len);
	}
	for (int j = len - 1;j >0;j--)
	{
		swap(a[j], a[0]);
		HeapAdjustDown(a, 0, j);
	}
}
void main()
{
	int a[10] = { 0,111,233,14,10,45,13,56,17,1};
	HeapSort(a, 10);
	for (int i = 0;i < 10;i++)
		cout << a[i] << "  ";
	cout << endl;
	int b[11] = { 0,111,233,14,10,45,13,56,17,1,12 };
	HeapSort(b, 11);
	for (int i = 0;i < 11;i++)
		cout << b[i] << "  ";
	cout << endl;
}

堆排序示例代码三:递归的方法,堆顶的序号为0

#include <iostream>

using namespace std;

int Left(int index) { return ((index << 1) + 1); }
int Right(int index) { return ((index << 1) + 2); }
void swap(int *a, int *b) { int temp = *a;*a = *b;*b = temp; }

void maxHeapify(int array[], int index, int heapSize)//大根堆
{
	int largest = 0;
	int left = Left(index);
	int right = Right(index);

	if ((left <= heapSize) && (array[left] > array[index]))
	{
		largest = left;
	}
	else
	{
		largest = index;
	}
	if ((right <= heapSize) && (array[right] > array[largest]))
	{
		largest = right;
	}
	if (largest != index)
	{
		swap(&array[index], &array[largest]);
		maxHeapify(array, largest, heapSize);
	}
}
void heap_sort(int array[], int length)
{
	for (int i = ((length - 2) >>1);i >= 0;i--)
	{
		maxHeapify(array, i, length-1);
	}
	for (int j = length - 1;j >0;j--)
	{
		swap(&array[j], &array[0]);
		maxHeapify(array, 0, j-1);
	}
}
void main()
{
	int a[10] = { 0,111,233,14,10,45,13,56,17,1};
	heap_sort(a, 10);
	for (int i = 0;i < 10;i++)
		cout << a[i] << "  ";
	cout << endl;
	int b[11] = { 0,111,233,14,10,45,13,56,17,1,12 };
	heap_sort(b, 11);
	for (int i = 0;i < 11;i++)
		cout << b[i] << "  ";
	cout << endl;
	int c[8] = { 45,68,20,39,88,97,46,59 };
	heap_sort(c, 8);
	for (int i = 0;i < 8;i++)
		cout << c[i] << "  ";
	cout << endl;

}

4.归并排序

第一种:归并排序–稳定 时间复杂度O( n l o g 2 n n{log_2{n}} nlog2n)

归并排序示例代码一:要用户自己申请暂存空间

#include <iostream>
using namespace std;
void Merge(int a[],int b[], 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 b[], int low, int high)
{
	if (low < high)
	{
		int mid = (low + high) / 2;
		MergeSort(a,b,low,mid);
		MergeSort(a,b,mid+1,high);
		Merge(a,b,low,mid,high);
	}
}
void main()
{
	int a[11] = { 0,1,233,4,2,45,3,56,7,1,12 };
	int b[11];
	MergeSort(a,b,0,10);
	for (int i = 0;i < 11;i++)
		cout << a[i] << "  ";
}

归并排序示例代码二:不需要用户自己申请暂存空间,智能版

#include <iostream>

using namespace std;

void Merge(int a[], int tmp[], int lPos, int rPos, int rEnd)
{
	int i, lEnd, NumElements, tmpPos;
	lEnd = rPos - 1;
	tmpPos = lPos;
	NumElements = rEnd - lPos + 1;
	while (lPos <= lEnd&&rPos <= rEnd)
	{
		if (a[lPos] <= a[rPos])
		{
			tmp[tmpPos++] = a[lPos++];
		}
		else
		{
			tmp[tmpPos++] = a[rPos++];
		}
	}
	while (lPos <= lEnd)
	{
		tmp[tmpPos++] = a[lPos++];
	}
	while (rPos <= rEnd)
	{
		tmp[tmpPos++] = a[rPos++];
	}
	for (i = 0;i < NumElements;i++, rEnd--)
	{
		a[rEnd] = tmp[rEnd];
	}
}

void msort(int a[], int tmp[], int low, int high)
{
	if (low < high)
	{
		int middle = (low + high) / 2;
		msort(a, tmp, low, middle);
		msort(a, tmp, middle + 1, high);
		Merge(a, tmp, low, middle + 1, high);
	}
}

void merge_sort(int a[], int len)
{
	int *tmp = NULL;
	tmp = new int[len];
	if (tmp != NULL)
	{
		msort(a, tmp, 0, len - 1);
		delete[]tmp;
	}
}

void main()
{
	int a[10] = { 0,111,233,14,10,45,13,56,17,1};
	merge_sort(a, 10);
	for (int i = 0;i < 10;i++)
		cout << a[i] << "  ";
	cout << endl;
	int b[11] = { 0,111,233,14,10,45,13,56,17,1,12 };
	merge_sort(b, 11);
	for (int i = 0;i < 11;i++)
		cout << b[i] << "  ";
	cout << endl;
	int c[8] = { 8,6,1,3,5,2,7,4 };
	merge_sort(c, 8);
	for (int i = 0;i < 8;i++)
		cout << c[i] << "  ";
	cout << endl;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值