从0开始学数据结构1

从0开始学数据结构1,第1张

从0开始学数据结构1

文章目录
冒泡排序
快速排序
队列

冒泡排序:
int i,j,temp,arr

//bubble sort 核心代码
for (i=0; i < len - 1; i++) {
	for (j=0; j < len - 1 - i; j++) {
		if (a[j+1] > a[j]) {
			//交换
		}
	}
}
//quick sort递归核心代码
void quicksort(int left, int right)
{
	int i, j, t, temp;
	if (left > right)
		return;
	temp = a[left];
	i = left;
	j = right;
	while (i != j)
	{
		//先从右边开始
		while(a[j] >= temp && i < j)
			j--;
		while(a[i] <= temp && i < j)
			i++;
		//否则就交换
		if (i < j)
		{
			t = a[i];
			a[i] = a[j];
			a[j] = t;
		}
		//基准数归位
		a[left] = a[i];
		a[i] = temp;

		quicksort(left, i-1);
		quicksort(i+1 , right);
}
//quicksort非递归
int partition(int* vec, int low, int high){
     int pivot=vec[low];  //任选元素作为轴,这里选首元素
     while (low=pivot)
             high--;
         vec[low]=vec[high];
         while (low s;
	s.push(left);
	s.push(right);//后入的right,所以要先拿right
	while(!s.empty)//栈不为空
	{
		int right = s.top();
		s.pop();
		int left = s.top();
		s.pop();
		
		int index = PartSort(array,left,right);
		if((index - 1) > left)//左子序列
		{
			s.push(left);
			s.push(index - 1);
		}
		if((index + 1) < right)//右子序列
		{
			s.push(index + 1);
			s.push(right);
		}
	}
}
struct queue
{
	int data[100];
	int head; //队首
	int tail; //队尾
}
q.head++;//队首出队
q.tail++;//添加到队尾

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5697544.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存