从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<high){
         while (low<high && vec[high]>=pivot)
             high--;
         vec[low]=vec[high];
         while (low<high && vec[low]<=pivot)
             low++;
         vec[high]=vec[low];
     }
     //此时low==high
     vec[low]=pivot;
     return low;
}
void QuickSortNotR(int* array,int left,int right)
{
	stack<int> 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++;//添加到队尾
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值