Page 338 - ComputerScience_Class_11
P. 338
Let us understand this by taking an example.
The array is to be sorted in descending order:
Index 0 1 2 3 4
Ar 2 16 12 10 21
Iteration 1:
Step 1: The first two elements are compared.
Index 0 1 2 3 4
Ar 2 16 12 10 21
Since, ar[0] < ar[1], i.e., 2 < 16 is true, the numbers are swapped.
Swapping the numbers in the position, the array becomes:
Index 0 1 2 3 4
Ar 16 2 12 10 21
Step 2: The next two elements are compared.
Index 0 1 2 3 4
Ar 16 2 12 10 21
Since, ar[1] < ar[2] , i.e., 2 < 12 is true; the numbers are swapped.
Swapping the numbers in the position, the array becomes:
Index 0 1 2 3 4
Ar 16 12 2 10 21
Step 3: The next two elements are compared.
Index 0 1 2 3 4
Ar 16 12 2 10 21
Since, ar[2] < ar[3], i.e., 2 < 10 is true; the numbers are swapped.
Swapping the numbers in the position, the array becomes:
Index 0 1 2 3 4
Ar 16 12 10 2 21
Step 4: The next two elements are compared.
Index 0 1 2 3 4
Ar 16 12 10 2 21
Since, ar[3] < ar[4], i.e., 2 < 21 is true; the numbers are swapped.
Swapping the numbers in the position, the array becomes:
Index 0 1 2 3 4
Ar 16 12 10 21 2
After the first iteration, the smallest element is placed in the last position.
336 Touchpad Computer Science (Ver. 3.0)-XI

