#include <iostream>
using namespace std;
void choice_sort(int *arr, int len);
void quicksort(int *arr, int start, int pivot);
int main()
{
int arr[10] = {10, 9, 8, 7, 6, 5, 4, 3,2, 1};
quicksort(arr, 0, 9);
for(int i=0; i<10; i++) //不对称边界
{
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}
void bubble_sort(int *arr, int len)
{
int temp;
for(int i=0; i<len-1; i++)
{
for(int j=0; j<len-1-i; j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
//插入排序想法是:一条if语句包含两种情况.
//每次求最小值,都要和数组所有其它的值进行一下比较.只能是这样做.
void insert_sort(int *arr, int len)
{
int temp;
int max;
//控制外层大循环的次数,不是吗?
for(int i=0; i<len-1; i++)
{
max = i;
for(int j= i+1; j<len; j++)
{
if(arr[max] > arr[j])
{
max = j;
}
}
if(max != i)
{
temp = arr[i];
arr[i] = arr[max];
arr[max] = temp;
}
}
}
void choice_sort(int *arr, int len)
{
//确定大循环次数,循环n-1次
int j=0;
int temp;
for(int i=0; i<len-1; i++)
{
j=i;
temp = arr[j+1];
while(j>=0 && temp<arr[j])
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}
int partition(int *arr, int start, int pivot)
{
int i= start;
int j = start;
int temp;
for(;j<pivot;j++)
{
if(arr[j]<arr[pivot])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
}
temp = arr[i];
arr[i] = arr[pivot];
arr[pivot] = temp;
return i; //..返回轴点位置
}
void quicksort(int *arr, int start, int pivot)
{
int q;
if(start<pivot)
{
q = partition(arr, start, pivot);
quicksort(arr, start, q-1);
quicksort(arr, q+1, pivot);
}
}
//二分查找
//每次将问题的规模减少一半.
int binary_search(int arr, int len, int target)
{
int top=0,bottom=len-1,mid=-1;
while(top<=bottom)
{
mid = (top+bottom)>>1;
if(mid > target)
{
bottom = mid-1;
} else if(mid < target)
{
top = mid+1;
}else
{
return mid;
}
}
return -1;
}