排序复习

  直接插入排序:O(N2)

void InsertSort(int *a, int size)
{
	for (int i = 0; i < size - 1; i++)
	{
		for (int j = i + 1; j > 0; j--)
		{
			if (a[j] < a[j - 1])
			{
				int tmp = a[j - 1];
				a[j - 1] = a[j];
				a[j] = tmp;
			}
		}
	}
}

  冒泡排序:O(N2)

void BubbleSort(int *a, int size)
{
	for (int i = 0; i < size; i++)
	{
		for (int j = 1; j < size-i; j++)
		{
			if (a[j] < a[j - 1])
			{
				int tmp = a[j - 1];
				a[j - 1] = a[j];
				a[j] = tmp;
			}
		}
	}
}

  希尔排序 O(N2)  设置步长,优化后的插入排序

void ShellSort(int *a, int size)
{
	int gap = size / 2;
	while (gap >= 1)
	{
		for (int i = 0; i < size; i ++)
		{
			for (int j = i + gap;  j<size; j = j - gap)
			{
				if (a[j] < a[j - gap])
				{
					int tmp = a[j - gap];
					a[j - gap] = a[j];
					a[j] = tmp;
				}
				break;
			}
		}
		gap = gap / 2;
	}
}

  简单选择排序 O(N2) 

void SelectSort(int *a,int size)
{
	for (int i = 0; i < size; i++)
	{
		for (int j = i + 1; j < size; j++)
		{
			if (a[j] < a[i])
			{
				int tmp = a[i];
				a[i] = a[j];
				a[j] = tmp;
			}
		}
	}
}

  快速排序 logN

void QuickSort(int *a, int size)
{
	if (size <=1)
		return;
	//哨兵i 哨兵j
	int first_val = a[0];
	int i = 1, j = size - 1;
	while (i != j)
	{
		//哨兵j开始行动
		while (j > i && a[j] >= first_val)
		{
			j--;
		}

		while (j > i && a[i] < first_val)
		{
			i++;
		}
		if(i<j)
		{
			int tmp = a[i];
			a[i] = a[j];
			a[j] = tmp;
		}
		

		/*for (int k = 0; k < size; k++)
		{
			std::cout << a[k] << std::endl;
		}
		std::cout << "\n";*/
	}
	if (a[0] > a[j])
	{
		int tmp = a[0];
		a[0] = a[j];
		a[j] = tmp;
	}
	

	for (int k = 0; k < size; k++)
	{
		std::cout << a[k] << std::endl;
	}
	std::cout << "\n";
	QuickSort(a, i);
	//这里很重要
	if(i==1)
		QuickSort(a+i, size-i);
	else
		QuickSort(a + i + 1, size - i - 1);
}

  归并排序 

void Merge(int *a, int begin_ptr, int mid, int end_ptr)
{
	//递归出口
	if (begin_ptr == end_ptr)
		return;
	int f_len = mid +1;
	int s_len = end_ptr +1 ;
	int len = end_ptr - begin_ptr + 1;
	int *array = new int[len];
	ZeroMemory(array, len);
	int i, j, count;
	i = j = count = 0;
	i = begin_ptr;
	j = mid+1;
	while (i < f_len || j < s_len)
	{
		if (a[i] <= a[ j])
			array[count++] = a[i++];
		else {
			array[count++] = a[j++];
		}

		if (j >= s_len)
		{
			while (i < f_len)
			{
				array[count++] = a[i++];
			}
		}

		if (i >= f_len)
		{
			while (j < s_len)
			{
				array[count++] = a[j++];
			}
		}

	}

	for (int k = begin_ptr; k <= end_ptr; k++)
	{
		a[k] = array[k- begin_ptr];
		std::cout << a[k] << std::endl;
	}
	std::cout << "------\n";
}

void MergeSort(int *a, int begin_ptr, int end_ptr)
{
	if (begin_ptr == end_ptr)
		return;
	int mid = (begin_ptr + end_ptr) / 2;

	MergeSort(a, begin_ptr, mid);
	
	MergeSort(a, mid + 1, end_ptr);
	
	Merge(a, begin_ptr, mid, end_ptr);
}

  

堆排序: 时间复杂度 n*lg(n)   需要再次计算。。。。。弱点

1.明确堆的概念,以最大堆举例,堆是一个二叉树,每个节点都比其子节点的值大。

2.每次取根结点,然后将其右结点逐个通过堆insertHeap,插入生成新的堆

3.重复此过程

重点在于如何建最大堆,无非就是通过队列,找到完全二叉树的插入位置,如果父节点小于插入结点,就一直交换上去,直到根节点

typedef struct node
{
	int value;
	node *lchild;
	node *rchild;
	node *father;
	node()
	{
		lchild = nullptr;
		rchild = nullptr;
		father = nullptr;
	};
}TNode;


void Print(TNode *root)
{
	std::list<TNode*> stack_node;
	stack_node.push_back(root);
	while (!stack_node.empty())
	{
		auto current_node = stack_node.front();
		stack_node.pop_front();
		std::cout << current_node->value << std::endl;

		if (current_node->lchild != nullptr)
		{
			stack_node.push_back(current_node->lchild);
			//return;
		}
		if (current_node->rchild != nullptr)
		{
			stack_node.push_back(current_node->rchild);
			//return;
		}
		
	}
}

TNode* SwapFather(TNode *node)
{
	int tmp = node->father->value;
	node->father->value = node->value;
	node->value = tmp;
	return node->father;
}

void Adjust(TNode *node)
{
	while (node->father !=nullptr &&node->father->value <node->value)
	{
		node = SwapFather(node);
	}
}

void InsertFullTree(TNode *&root, TNode *node)
{
	//树为空
	if (root == nullptr)
	{
		root = node;
		return;
	}
	std::list<TNode*> stack_node;
	stack_node.push_back(root);

	while (!stack_node.empty())
	{
		auto current_node = stack_node.front();
		stack_node.pop_front();
		if (current_node->lchild == nullptr)
		{
			current_node->lchild = node;
			node->father = current_node;
			Adjust(node);
			return;
		}
		else if (current_node->rchild == nullptr)
		{
			current_node->rchild = node;
			node->father = current_node;
			Adjust(node);
			return;
		}
		else
		{
			stack_node.push_back(current_node->lchild);
			stack_node.push_back(current_node->rchild);
		}
	}

}

void HeapSort(TNode *root)
{
	while (root != nullptr)
	{
		std::cout << root->value << std::endl;

		if (root->lchild == nullptr)
			break;
		
		std::stack<TNode*> stack;
		
		if (root->rchild != nullptr)
			stack.push(root->rchild);

		root = root->lchild;
		root->father = nullptr;
		while (!stack.empty())
		{
			auto tmp = stack.top();
			stack.pop(); 
			if (tmp->lchild != nullptr)
				stack.push(tmp->lchild);
			if (tmp->rchild != nullptr)
				stack.push(tmp->rchild);

			tmp->father = nullptr;
			tmp->lchild = nullptr;
			tmp->rchild = nullptr;
			InsertFullTree(root, tmp);
		}
		Print(root);
		std::cout << "--------------\n\n";
	}
}

  

 

posted @ 2019-02-20 11:42  仙7道  阅读(118)  评论(0)    收藏  举报