📄 e20-03.cpp
字号:
// =======================================================
// Chapter 20, Example 3
// The Quicksort
// =======================================================
#include "Array.h"
#include "Sorts.h"
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
// compare integers
int compareint( int l, int r )
{
return l - r;
}
// compare the integers in reverse order
int compareintreverse( int l, int r )
{
return r - l;
}
// compare floats
int comparefloat( float l, float r )
{
if( l < r )
return -1;
if( l > r )
return 1;
return 0;
}
template<class DataType>
void PrintArray( Array<DataType>& p_array )
{
int index;
for( index = 0; index < p_array.Size(); index++ )
{
cout << p_array[index] << ", ";
}
}
void main()
{
Array<int> iarray( 16 );
Array<float> farray( 16 );
int index;
// seed the randomizer, see Chapter 22.
srand( time(0) );
// fill up the arrays with random values
for( index = 0; index < 16; index++ )
{
// 0-255
iarray[index] = rand() % 256;
// 0-1
farray[index] = (float)(rand() % 256) / 256.0f;
}
QuickSort( iarray, 0, 16, compareint );
QuickSort( farray, 0, 16, comparefloat );
cout << "Integer Array: ";
PrintArray( iarray );
cout << endl;
cout << "Float Array: ";
PrintArray( farray );
cout << endl;
// reverse the integer array
QuickSort( iarray, 0, 16, compareintreverse );
cout << "Integer Array Reversed: ";
PrintArray( iarray );
cout << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -