1
•
n
Chapter 8 Arrays 497
[ 23
~
Original After
78 45 8 32 56 32 56
pass 3
List
E
Unsorted Sorted
45 8 32 56
After
pass 1 8 | 23 32 45 78
I
After
pass 4
Unsorted Sorted
3 After After
23 45 8 32 56 8 23 32 45 56 78
3 pass 2 pass 5
Sorted Unsorted Sorted
FIGURE 8 - 25 Insertion Sort Example
The design of the insertion sort follows the same pattern we saw in both
the selection sort and the bubble sort ; each iteration of the outer loop is a
sort pass. The inner loop inserts the first element from the unsorted list into
its proper position relative to the rest of the data in the sorted list . I he design
is shown in Figure 8 - 26.
Insertion Sort
o \
_
\ / walk =
cur = 1
V-
cur <= last' t M
I K\
cur ' 1
walk >= 0 & &#
\
M JlocatedJ
located = false
temp <
list [walk]
list[ walk + 1 ]
= temp temp = list(cur) list [walk + 1 ]
= list[ walk ]
o
located = true
walk - -
o ?
I
J
o
Return
FIGURE 8 - 26 Insertion Sort Design
Program 8 - 1 I shows the insertion soil .
498 Section 8.5 Sorting
PROGRAM 8 - 11 Insertion Sort
1 ===== insertionSort === : =======
2 Sort list using Insertion Sort. The list is divided
3 into sorted and unsorted lists. With each pass, first
4 element in unsorted list is inserted into sorted list ,
5 Pre list must contain at least one element
6 last contains index to last element in list
7 Post list has been rearranged
8 */
9 void insertionSort ( int list[ ], int last)
10 {
11 // Statements
12 // Local Declarations
13 int walk ;
14 int temp;
15 bool located ;
16
17 // Statements
18 // Outer loop
19 for ( int current = 1; current <= last; current+t)
20 {
21 // Inner loop: Select and move one element
22 located = false;
23 temp = list[current ];
24 for ( walk = current 1; walk >= 0 && !located ;)
25 if (temp < list[walk ])
26 {
27 list[walk + 1 ] = list[walk];
28 walk ; —
29 > // if
30 else
31 located = true;
32 list ( walk + 1] = temp ;
33 > // for
34 return ;
35 > // insertionSort
Program 811 Analysis Note how the exchange is worked in this sort. Before the inner loop starts, we put the
data from the current element into the hold area (temp). This is the first step in the
exchange . The loop then determines the correct position to place the element by start -
ing with the largest element in the sorted list and working toward the beginning. As it
searches, it spreads the sorted portion of the list by shifting each element one position
higher in the list. When it locates the correct position, therefore, the data have
already been moved right one position and the current location is ''empty"; the sort
therefore simply places the saved element in its proper location, completing the
exchange.
Chapter 8 Arrays 499 1
Testing Sorts
Now that we have written three sorts, howr do we test them? The answer is
that we write a simple test driver. The same test driver can he used for all three
sorts. The only changes necessary are in the include statement, the prototype
statement, and the function call ( see highlighted statements). Program 8- 1 2
is set up to test the insertion sort .
PROGRAM 8 - 1 2 Testing Sorts
1 /* Test driver for insertion sort.
2 Written by:
3 Date:
4 */
5 # include <stdio.h >
6 # include <stdbool.h>
7 # include "P08-11.C "
8
9 Idefine MAX ARY SIZE 15
10
11 // Function Declarations
12 void insertionSort (int list[ ], int last );
13
14 int main ( void )
15 {
16
17
// Local Declarations
_ _
int ary[ MAX ARY SIZE] = { 89, 72, 3 , 15, 21,
18 57 , 61 , 44, 19 , 98 ,
19 5 , 77 , 39 , 59 , 61 };
20 // Statements
21
22
printf( " Unsorted: " );
_ _
for ( int i = 0; i < MAX ARY SIZE; i++)
23 printf( " % 3d " , ary[ i ]);
24
25
_
insertionSort ( ary , MAX ARY SIZE _ - 1 );
26
27
28
printf( " \nSorted : " );
_ _
for ( int i = 0; i < MAX ARY SIZE; i++ )
29 printf( " %3d " , ary[i]);
30 printf( " \n " );
31 return 0;
32 } // main
Results:
19 98 5 77 39 59 61
Unsorted: 89 72 3 15 21 57 61 44
57 59 61 61 72 77 89 98
Sorted : 3 5 15 19 21 39 44
500 Section 8.5 Sorting
Sorts Compared
It is interesting to compare the three sorting algorithms. All three use two
nes ted loops. The outer loop is the same in all three; it moves the wall one
element toward the beginning of the array. The processing in the inner loop,
however, changes with each sort .
In the selection sort , the inner loop starts at the unsorted portion of the
array and finds the smallest element in the unsorted area . Once located , it
swaps the smallest element with the first unsorted element . It then moves the
wall and continues the sort . Note that all exchanges take place in the
unsorted portion of the array; the sorted elements are never touched .
While the selection sort starts at the beginning of the unsorted portion of
the array, the bubble sort starts at the end . Another difference between the
two is that the bubble sort may make multiple exchanges in each pass. While
bubbling its way to the beginning of the array, it exchanges all elements that
are out of sequence. Like the selection sort , once the wall is moved , the
sorted data is never touched .
The insertion sort takes a completely different approach. In each pass,
the first unsorted clement is inserted into the sorted portion of the array.
While it might appear that there are no exchanges in the insertion sort , there
are . As explained previously, the first unsorted element is moved to a hold
area . This is the first step in an exchange. Then , as it loops through the
unsorted portion of the array, elements are shifted higher. Each shift is the
equivalent of the second step in an exchange. Finally, when the correct loca-
tion is found , the third step ol the exchange moves the temporarily stored ele -
ment to its correct position .
I able 8- 1 summarizes the exchange and shift characteristics of these
three sorts.
Sort Exchanges Shifts
Selection Once for each pass
Bubble Several in each pass
Insertion Only partially in loop Zero or more in each pass
TABLE 8 - 1 Sort Exchanges and Passes
Sort Conclusions
In this section , we have covered three classic sorts. With the exception of the
insertion sort , you generally will not find them implemented in production
systems. I he insertion sort is used as a subfunction
in both Quicksort and