Pagini recente » Istoria paginii runda/asdfghjkl/clasament | Cod sursa (job #1318686) | Cod sursa (job #1318936) | Cod sursa (job #1996661) | Cod sursa (job #1418151)
#include <iostream>
#include <fstream>
void swap( int& a, int& b)
{
int tmp = a;
a = b;
b = tmp;
}
int choosePivot(int *a, int lo, int hi)
{
return lo;
}
int partition(int *a, int lo, int hi)
{
int pivotIndex = choosePivot(a,lo,hi);
int pivotValue = a[pivotIndex];
swap(a[pivotIndex], a[hi]);
int storeIndex = lo;
for ( int i = lo; i < hi; ++i )
{
if ( a[i] < pivotValue )
{
swap( a[i], a[storeIndex] );
storeIndex = storeIndex + 1;
}
}
swap( a[storeIndex], a[hi] );
return storeIndex;
}
void quicksort(int *a, int lo, int hi)
{
if ( lo < hi )
{
int p = partition(a,lo,hi);
quicksort(a,lo,p-1);
quicksort(a,p+1,hi);
}
}
static const int N = 500000;
int array[N];
int main(int argc, char* argv[])
{
std::ifstream input("algsort.in");
std::ofstream output("algsort.out");
int n;
input >> n;
for ( int i = 0; i < n; ++i )
{
input >> array[i];
}
quicksort(array,0,n-1);
for ( int i = 0; i < n; ++i )
{
output << array[i] << " ";
}
input.close();
output.close();
return 0;
}