#include<iostream>
#include <stdio.h>
using namespace std;
void QuickSort(int *a,int left,int right)
{
if ( left < right )
{
int i = left,t;
int j = right + 1;
int pivot = a[left];
do
{
do
{
i++;
} while ( a[i] < pivot );
do
{
j--;
} while ( a[j] > pivot );
if ( i < j )
{
t=a[i];a[i]=a[j];a[j]=t;
}
} while ( i < j );
if (left != j)
{
t=a[left];a[left]=a[j];a[j]=t;
}
QuickSort(a,left,j-1);
QuickSort(a,j+1,right);
}
}
void print(const int *const pHead,int max)
{
cout<<"============================="<<endl;
for(int i = 0; i < max; ++i)
{
cout<<pHead[i]<<" ";
}
cout<<endl<<"============================"<<endl;
}
void quickSort1(int *const pHead, int begin, int end)
{
printf("begin is %d end is %d\r\n",begin,end);
if(begin < end)
{
int i = begin + 1, j = end, temp;
int key = pHead[begin];
//while(i<j)
/*
do while will exec noce at least
but while may won't exec!!
*/
while(i <= j)//Note:when it has two we have to compare once
{
while(pHead[i] < key)
{
i++;
}
while(pHead[j] > key)
{
j--;
}
if(i < j)
{
temp = pHead[i];
pHead[i] = pHead[j];
pHead[j] = temp;
}
}
if(begin != j)
{
temp = pHead[begin];
pHead[begin] = pHead[j];
pHead[j] = temp;
}
print(pHead, 7);
quickSort1(pHead, begin, j - 1);
quickSort1(pHead, j + 1, end);
}
}
int main(void)
{
int array[7] = {5,6,1,4,8,7,2};
cout<<"===========before quick sort =========="<<endl;
print(array,7);
// QuickSort(array,0,6);
quickSort1(array,0,6);
cout<<"===========after quick sort============"<<endl;
print(array,7);
return 0;
}
#include <stdio.h>
using namespace std;
void QuickSort(int *a,int left,int right)
{
if ( left < right )
{
int i = left,t;
int j = right + 1;
int pivot = a[left];
do
{
do
{
i++;
} while ( a[i] < pivot );
do
{
j--;
} while ( a[j] > pivot );
if ( i < j )
{
t=a[i];a[i]=a[j];a[j]=t;
}
} while ( i < j );
if (left != j)
{
t=a[left];a[left]=a[j];a[j]=t;
}
QuickSort(a,left,j-1);
QuickSort(a,j+1,right);
}
}
void print(const int *const pHead,int max)
{
cout<<"============================="<<endl;
for(int i = 0; i < max; ++i)
{
cout<<pHead[i]<<" ";
}
cout<<endl<<"============================"<<endl;
}
void quickSort1(int *const pHead, int begin, int end)
{
printf("begin is %d end is %d\r\n",begin,end);
if(begin < end)
{
int i = begin + 1, j = end, temp;
int key = pHead[begin];
//while(i<j)
/*
do while will exec noce at least
but while may won't exec!!
*/
while(i <= j)//Note:when it has two we have to compare once
{
while(pHead[i] < key)
{
i++;
}
while(pHead[j] > key)
{
j--;
}
if(i < j)
{
temp = pHead[i];
pHead[i] = pHead[j];
pHead[j] = temp;
}
}
if(begin != j)
{
temp = pHead[begin];
pHead[begin] = pHead[j];
pHead[j] = temp;
}
print(pHead, 7);
quickSort1(pHead, begin, j - 1);
quickSort1(pHead, j + 1, end);
}
}
int main(void)
{
int array[7] = {5,6,1,4,8,7,2};
cout<<"===========before quick sort =========="<<endl;
print(array,7);
// QuickSort(array,0,6);
quickSort1(array,0,6);
cout<<"===========after quick sort============"<<endl;
print(array,7);
return 0;
}