冒泡排序
快速排序
队列
冒泡排序:
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++;//添加到队尾
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)