/* Use the insertion sort technique to sort the "data" array in
ascending order. This ro utine assumes that data[ firstElement ] is
not the first element in data and that data[ firstElement-1 ] can be
accessed. */ public void InsertionSort( int[] data, int firstElement,
int lastElement ) { /* Replace element at lower boundary with an
element guaranteed to be first in a sorted list. */ int lowerBoundary
= data[ firstElement-1 ]; data[ firstElement-1 ] = SORT_MIN; /* The
elements in positions firstElement through sortBoundary-1 are always
sorted. In each pass through the loop, sortBoundary is increased, and
the element at the position of the new sortBoundary probably isn't in
its sorted place in the array, so it's inserted into the proper place
somewhere between firstElement and sortBoundary. */ for ( int
sortBoundary = firstElement+1; sortBoundary <= lastElement;
sortBoundary++ ) { int insertVal = data[ sortBoundary ]; int insertPos
= sortBoundary; while ( insertVal < data[ insertPos-1 ] ) { data[
insertPos ] = data[ insertPos-1 ]; insertPos = insertPos-1; } data[
insertPos ] = insertVal; } /* Replace original lower-boundary element
*/ data[ firstElement-1 ] = lowerBoundary; }
/* Use the insertion sort technique to sort the "data" array in ascending order.
This routine assumes that data[ firstElement ] is not the first element in data
and that data[ firstElement-1 ] can be accessed. */
public void InsertionSort( int[] data, int firstElement, int lastElement ) {
/* Replace element at lower boundary with an element guaranteed to be first in a
sorted list. */
int lowerBoundary = data[ firstElement-1 ];
data[ firstElement-1 ] = SORT_MIN;
/* The elements in positions firstElement through sortBoundary-1 are always
sorted. In each pass through the loop, sortBoundary is increased, and the element
at the position of the new sortBoundary probably isn't in its sorted place in the
array, so it's inserted into the proper place somewhere between firstElement
and sortBoundary. */
for (int sortBoundary = firstElement+1; sortBoundary <= lastElement; sortBoundary++) {
int insertVal = data[ sortBoundary ];
int insertPos = sortBoundary;
while ( insertVal < data[ insertPos-1 ] ) {
data[ insertPos ] = data[ insertPos-1 ];
insertPos = insertPos-1;
}
data[ insertPos ] = insertVal;
}
/* Replace original lower-boundary element */
data[ firstElement-1 ] = lowerBoundary;
}
/**
* Use the insertion sort technique to sort the "data" array in ascending
* order. This routine assumes that data[ firstElement ] is not the
* first element in data and that data[ firstElement-1 ] can be accessed.
*/
public void InsertionSort( int[] data, int firstElement, int lastElement ) {
//
// Replace element at lower boundary with an element guaranteed to be
// first in a sorted list.
//
int lowerBoundary = data[ firstElement-1 ];
data[ firstElement-1 ] = SORT_MIN;
//
// The elements in positions firstElement through sortBoundary-1 are always
// sorted. In each pass through the loop, sortBoundary is increased, and the
// element at the position of the new sortBoundary probably isn't in its
// sorted place in the array, so it's inserted into the proper place
// somewhere between firstElement and sortBoundary.
//
for ( int sortBoundary = firstElement + 1; sortBoundary <= lastElement; sortBoundary++ ) {
int insertVal = data[ sortBoundary ];
int insertPos = sortBoundary;
while ( insertVal < data[ insertPos - 1 ] ) {
data[ insertPos ] = data[ insertPos - 1 ];
insertPos = insertPos - 1;
}
data[ insertPos ] = insertVal;
}
// Replace original lower-boundary element
data[ firstElement - 1 ] = lowerBoundary;
}
Dobré vizuální uspořádání kódu zdůrazňuje logickou strukturu programu.
// swap left and right elements for whole array
for (i = 0; i < MAX_ELEMENTS; i++)
leftElement = left [i];
left [i] = right [i];
right [i] = leftElement;
x = 3+4 * 2+7;
// --------------------------------------------------------------------
// ********************************************************************
///////////////////////////////////////////////////////////////////////
//
/* *******************************************************************
* INTERFACE METHODS: DataSource
* *******************************************************************/
12 + 4 % 3 * 7 / 8
v C++12 + 4 mod 3 * 7 \ 8
ve Visual BasicuA
B
C
D
if
blokuIf pixelColor = Color_Red Then
statement1
statement2
...
End If
while
blokuWhile pixelColor = Color_Red
statement1
statement2
...
Wend
switch
blokuSelect Case pixelColor
Case Color_Red
statement1
statement2
...
Case Color_Green
statement1
statement2
...
Case Else
statement1
statement2
...
End Select
A {
B
C
D }
if
blokuif (pixelColor == Color.RED) {
statement1
statement2
...
}
while
blokuwhile (pixelColor == Color.RED) {
statement1
statement2
...
}
switch
blokuswitch (pixelColor) {
case Color.RED:
statement1
statement2
...
break;
case Color.GREEN:
statement1
statement2
...
break;
default:
statement1
statement2
...
break;
}
A
{
B
C
}
if
blokuif (pixelColor == Color.RED)
{
statement1
statement2
...
}
while
blokuwhile (pixelColor == Color.RED)
{
statement1
statement2
...
}
switch
blokuswitch (pixelColor)
{
case Color.RED:
statement1
statement2
...
break;
case Color.GREEN:
statement1
statement2
...
break;
default:
statement1
statement2
...
break;
}
for (int i = 0; i < MAX_LINES; i++)
{
ReadLine (i);
ProcessLine (i);
}
A
B
C
D
E
for (int i = 0; i < MAX_LINES; i++)
{
ReadLine (i);
ProcessLine (i);
}
A
B
C
D
E
Přestože je první způsob poměrně často používaný, porušuje hlavní zásadu formátování – neukazuje totiž logickou strukturu kódu. Při použití tímto způsobem není begin/end součástí řídící struktury, ale ani podřízených příkazů, které následují.
Druhý způsob formátování je o něco méně častější, ale stejně jako předchozí způsob porušuje hlavní zásadu tím, že přesně neodráží logickou strukturu programu. Volání funkcí budí dojem, že jsou podřízené begin/end, což nejsou. Celkově tedy tento způsob formátování zveličuje složitost konstrukce.
A
B
C
D
while
blokuWhile pixelColor = Color_Red
statement1
statement2
...
Wend
switch
blokuSelect Case pixelColor
Case Color_Red statement1
statement2
...
Case Color_Green statement1
statement2
...
Case Else statement1
statement2
...
End Select
if
blokuIf pixelColor = Color_Red Then
statement1
statement2
...
End If
Elementy tohoto způsobu formátování jsou používány spíše výjimečně,
a přestože v některých případech mohou vypadat hezky, je lepší se
jim velkým obloukem vyhnout. Mezi hlavní důvody patří velká
náročnost na údržbu, ale především fakt, že při vnořování buď
dochází k neúměrnému zanoření a nebo se celá struktura rozbije
– např. při vnoření více if
bloků.
Výsledkem tedy je, že tento formátovací styl je nepřesný, těžko aplikovatelný v konzistentní formě a velmi náročný na údržbu. Steve McConnell ve své knize tvrdí, že viděl tento styl doporučovat jěště v knize z roku 2003.
V podstatě se ukazuje, že jakákoliv forma zarovnávání (mimo prostého odsazení) trpí podobnými problémy a je lepší se jí vyhnout.
while ((pathName[startPath+position]!=';')&&
((startPath+position)<pathName.length))
while (( pathName[ startPath + position ] != ';' ) &&
(( startPath + position ) < pathName.length ))
grossRate[census[groupId].gender,census[groupId].ageGroup]
grossRate[census[groupId].gender, census[groupId].ageGroup]
grossRate [census [groupId].gender, census [groupId].ageGroup]
grossRate[ census[ groupId ].gender, census[ groupId ].ageGroup ]
GetCensus(inputFile,employeeCount,employeeData,maxEmployees,inputError)
GetCensus(inputFile, employeeCount, employeeData, maxEmployees, inputError)
GetCensus (inputFile, employeeCount, employeeData, maxEmployees, inputError)
GetCensus( inputFile, employeeCount, employeeData, maxEmployees, inputError )
if ((('0' <= inChar) && (inChar <= '9')) || (('a' <= inChar) &&
(inChar <= 'z')) || (('A' <= inChar) && (inChar <= 'Z')))
if ((( '0' <= inChar ) && ( inChar <= '9' )) ||
(( 'a' <= inChar ) && ( inChar <= 'z' )) ||
(( 'A' <= inChar ) && ( inChar <= 'Z' ))
)
if (
(( '0' <= inChar ) && ( inChar <= '9' )) ||
(( 'a' <= inChar ) && ( inChar <= 'z' )) ||
(( 'A' <= inChar ) && ( inChar <= 'Z' ))
)
customerBill = PreviousBalance( paymentHistory[
customerId ]) + LateCharge( paymentHistory[ customerId ]);
customerBill = PreviousBalance( paymentHistory[ customerId ]) +
LateCharge( paymentHistory[ customerId ]);
customerPurchases = customerPurchases + CustomerSales( customerId );
customerBill = customerBill + customerPurchases;
totalCustomerBill = customerBill + PreviousBalance( custsomerId ) +
LateCharge( customerId );
customerPurchases = customerPurchases + CustomerSales( customerId );
customerBill = customerBill + customerPurchases;
totalCustomerBill = customerBill + PreviousBalance( custsomerId ) +
LateCharge( customerId );
DrawLine( window.north, window.south, window.east, window.west,
currentWidth, currentAttribute );
DrawLine( window.north, window.south, window.east, window.west,
currentWidth, currentAttribute );
DrawLine(
window.north, window.south, window.east, window.west,
currentWidth, currentAttribute
);
for ( int employeeNum = employee.first + employee.offset;
employeeNum < employee.first + employee.offset + employee.total;
employeeNum++ ) {
for (
int employeeNum = employee.first + employee.offset;
employeeNum < employee.first + employee.offset + employee.total;
employeeNum++
) {
bool ReadEmployees( int maxEmployees, EmployeeList * employees,
EmployeeFile * inputFile, int * employeeCount, bool * isInputError )
bool ReadEmployees( int maxEmployees,
EmployeeList * employees,
EmployeeFile * inputFile,
int * employeeCount,
bool * isInputError )
bool ReadEmployees (
int maxEmployees,
EmployeeList * employees,
EmployeeFile * inputFile,
int * employeeCount,
bool * isInputError
)
int rowIndex, columnIndex;
Color previousColor, currentColor, nextColor;
Point previousTop, previousBottom, currentTop, currentBottom,
nextTop, nextBottom;
Font previousTypeface, currentTypeface, nextTypeface;
Color choices[ NUM_COLORS ];
int rowIndex;
int columnIndex;
Color previousColor;
Color currentColor;
...
Color choices[ NUM_COLORS ];
File * inputFile, outputFile;
File * inputFile;
File * outputFile;