#include <cstdlib>
#include <iostream>
int partition (int[], int, int);
using namespace std;
void swap (int a[], int i, int j)
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
int Quicksort (int a[], int low, int high){
if(low<high){
int pivot=partition(a, low, high);
Quicksort (a,low,pivot-1);
Quicksort (a,pivot+1, high);
}
}
int partition (int a[], int low, int high){
int pivot, p_pos,i,j;
p_pos=low;
pivot=a[p_pos];
for (i=low+1; i<=high; i++){
if (a[i]<pivot){
p_pos++;
swap(a,p_pos,i);
}
}
swap(a,low,p_pos);
return p_pos;
}
int main(int argc, char *argv[])
{
int a[10];
int i,j,n=10;
for (i=0; i<n; i++)
{
a[i]=rand()%100;
cout<<a [i]<<" ";
}cout<<endl;
Quicksort(a,0,10);
for(i=0; i<n; i++)
{
cout<<a[i]<<" ";
}cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Админ Ээгий
No comments:
Post a Comment