void Bubblesort(int* arr,size_t size)
{
assert(arr);
for(int i=0;i<size;++i)
{
bool exchange=false;
for(int j=1;j<size-i;j++)
{
if(arr[j-1]>arr[j])
{
exchange=true;
std::swap(arr[j-1],arr[j]);
}
}
if(!exchange)
{
return;
}
}
}
int portion(int* arr,int left,int right)
{
int begin=left;
int end=right;
int key=right;
while(begin<end)
{
while(begin<end && arr[begin]<=key)
begin++;
if(begin<end)
{
arr[end]=arr[begin];
end--;
}
while(begin<end && arr[end]>=key)
end--;
if(begin<end)
{
arr[begin]=arr[end];
begin++;
}
}
arr[begin]=key;
return begin;
}
int getmidindex(int *arr,int left,int right)
{
assert(arr);
int mid=left+(right-left)/2;
if(arr[left]<arr[right])
{
if(arr[left]>arr[mid])
{
return left;
}
else if(arr[mid]>arr[right])
{
return right;
}
else
{
return mid;
}
}
else
{
if(arr[mid]>arr[left])
{
return left;
}
else if(arr[mid]<arr[right])
{
return right;
}
else
{
return mid;
}
}
}
int portion2(int* arr,int left,int right)
{
assert(arr);
int begin=left;
int end=right;
int key=arr[right];
int prev=begin-1;
/*int midindex=getmidindex(arr,left,right);
if(midindex!=right)
{
std::swap(arr[midindex],arr[right]);
}*/
while(begin<right)
{
if(arr[begin]<key && begin!=++prev)
{
std::swap(arr[begin],arr[prev]);
}
begin++;
}
std::swap(arr[++prev],arr[right]);
return prev;
}
/*void Quicksort(int* arr,int left,int right)
{
assert(arr);
if(left<right)
{
int boundary=portion2(arr,left,right);
Quicksort(arr,left,boundary-1);
Quicksort(arr,boundary+1,right);
}
}*/
void Quicksort(int* arr,int left,int right)
{
assert(arr);
stack<int> s;
s.push(right);
s.push(left);
while(!s.empty())
{
int begin=s.top();
s.pop();
int end=s.top();
s.pop();
if(begin<end)
{
int boundary=portion2(arr,begin,end);
s.push(end);
s.push(boundary+1);
s.push(boundary-1);
s.push(begin);
}
}
}
int getmidindex(int *arr,int left,int right)
{
assert(arr);
int mid=left+(right-left)/2;
if(arr[left]<arr[right])
{
if(arr[left]>arr[mid])
{
return left;
}
else if(arr[mid]>arr[right])
{
return right;
}
else
{
return mid;
}
}
else
{
if(arr[mid]>arr[left])
{
return left;
}
else if(arr[mid]<arr[right])
{
return right;
}
else
{
return mid;
}
}
}
{
assert(arr);
for(int i=0;i<size;++i)
{
bool exchange=false;
for(int j=1;j<size-i;j++)
{
if(arr[j-1]>arr[j])
{
exchange=true;
std::swap(arr[j-1],arr[j]);
}
}
if(!exchange)
{
return;
}
}
}
int portion(int* arr,int left,int right)
{
int begin=left;
int end=right;
int key=right;
while(begin<end)
{
while(begin<end && arr[begin]<=key)
begin++;
if(begin<end)
{
arr[end]=arr[begin];
end--;
}
while(begin<end && arr[end]>=key)
end--;
if(begin<end)
{
arr[begin]=arr[end];
begin++;
}
}
arr[begin]=key;
return begin;
}
int getmidindex(int *arr,int left,int right)
{
assert(arr);
int mid=left+(right-left)/2;
if(arr[left]<arr[right])
{
if(arr[left]>arr[mid])
{
return left;
}
else if(arr[mid]>arr[right])
{
return right;
}
else
{
return mid;
}
}
else
{
if(arr[mid]>arr[left])
{
return left;
}
else if(arr[mid]<arr[right])
{
return right;
}
else
{
return mid;
}
}
}
int portion2(int* arr,int left,int right)
{
assert(arr);
int begin=left;
int end=right;
int key=arr[right];
int prev=begin-1;
/*int midindex=getmidindex(arr,left,right);
if(midindex!=right)
{
std::swap(arr[midindex],arr[right]);
}*/
while(begin<right)
{
if(arr[begin]<key && begin!=++prev)
{
std::swap(arr[begin],arr[prev]);
}
begin++;
}
std::swap(arr[++prev],arr[right]);
return prev;
}
/*void Quicksort(int* arr,int left,int right)
{
assert(arr);
if(left<right)
{
int boundary=portion2(arr,left,right);
Quicksort(arr,left,boundary-1);
Quicksort(arr,boundary+1,right);
}
}*/
void Quicksort(int* arr,int left,int right)
{
assert(arr);
stack<int> s;
s.push(right);
s.push(left);
while(!s.empty())
{
int begin=s.top();
s.pop();
int end=s.top();
s.pop();
if(begin<end)
{
int boundary=portion2(arr,begin,end);
s.push(end);
s.push(boundary+1);
s.push(boundary-1);
s.push(begin);
}
}
}
int getmidindex(int *arr,int left,int right)
{
assert(arr);
int mid=left+(right-left)/2;
if(arr[left]<arr[right])
{
if(arr[left]>arr[mid])
{
return left;
}
else if(arr[mid]>arr[right])
{
return right;
}
else
{
return mid;
}
}
else
{
if(arr[mid]>arr[left])
{
return left;
}
else if(arr[mid]<arr[right])
{
return right;
}
else
{
return mid;
}
}
}