# Interchange Sort by ihzam

VIEWS: 21 PAGES: 10

• pg 1
```									                                  SORTING
ALGORITHMS

Sorting is an everyday activity in which the efficiency of the algorithm used is very
important. There are many sorting algorithms designed for use on computers and there
are computer packages available that enable us to compare the merits of the various
algorithms.

TYPES OF SORTING
ALGORITHMS

INTERCHANGE
SORT                                               SHUTTLE                     QUICK SORT
ALGORITHMS                 BUBBLE                      SORT                       ALGORITHMS
SORT                    ALGORITHMS
ALGORITHMS
INTERCHANGE SORT
ALGORITHMS

In this algorithm, the smallest number in the list is found and interchanged with the first
number. Then the smallest excluding the first is found and interchanged with the second
number. Next the smallest numbers excluding the first two are found and interchange
with the third number. This process continues until the list is sorted.

HOW TO SOLVE??

STEPS:
7
1. List down the numbers such the figure besides.

5             2. Swap 7 to 1 and 1 to 7.

3. Write the rest number.
2
4. After that swap 5 to 2 and 2 to 5.

4             5. Next, continue the process by comparing each number into the

correct order if necessary.
10
6. Make sure all the numbers are sorted correctly.

1             7. Total up the comparisons after the process had done.

6
ATTENTION!
: These arrows
3                              symbolized the swaps during
the comparisons process.
LET’S DO IT!

7                 1          1          1          1          1          1          1

5                 5          2          2          2          2          2          2

2                 2          5          3          3          3          3          3

4                 4          4          4          4          4          4          4

10               10         10         10         10           5          5          5

1                 7          7          7          7          7          6          6

6                 6          6          6          6          6          7          7

3                 3          3          5          5        10         10         10
2nd pass
Original

3rd pass

4th pass

5th pass

6th pass

7th pass
1st pass
list
NOTES:

Although no interchange take place on the 4th and 7th passes, this is a property of this
particular list and the passes must still be carried out to ensure that the list is correctly
sorted.

For the selection with interchange sort, we can see that:

The number of comparisons on the 1st pass=7, 2nd pass=6, 3rd pass=5, 4th pass=4,
5th pass=3, 6th pass=2, and 7th pass=1.

This gives a total of 7+6+5+4+3+2+1=28 comparisons.

The number of swaps will be at most one per pass.

The idea of interchange sort method is to
arrange the numbers from the smallest
numbers on the top and the biggest
numbers on the bottom. To do so, we need
to swap the numbers if necessary until we
get exactly a correct order.
QUESTION 1

6               2                  2              2            2            2             2

5               5                  4              4            4            4             4

9               9                  9              6            5            5             5

4               4                  5              5            6            5             5

5               5                  5              5            5            6             6

2               6                  6              9            9            9             9
2nd pass
Original

3rd pass

4th pass

5th pass

6th pass
1st pass
list

By following the steps before, let us see
LET’S                                         whether the answer is correct or not.
SEE!
1. Swap 6 to 2 and 2 to 6. Write the rest numbers.
2. After that swap 5 to 4 and 4 to 5. Write the rest numbers.
3. Continue swapping the numbers into a correct order.
4. Finally, swap 5 to 6 and 6 to 5. Now all the numbers have been
compared.
5. Write again the list to make sure all the numbers have been
sorted correctly.
6. Total up the number of comparisons.
QUESTION 2

Rearrange these numbers using the interchange sort algorithm. {11, 9, 3, 7, 2, 4}

11                4            4             3            2            2

9                9            2             2            3            3

3                3            3             4            4            4

7                7            7             7            7            7

2                2            9             9            9            9

4              11           11           11           11           11
2nd pass
Original

1st pass

3rd pass

4th pass

5th pass
list

Number of comparisons: 5+4+3+2+1=15 comparisons
QUESTION 3

Show the numbers below in the correct order by doing interchange sort. Total the
number of comparisons.

18              18              15              11              11

21              12              12              12              12

11              11              11              15              15

15              15              18              18              18

12              21              21              21              21
2nd pass
1st pass
Original

3rd pass

4th pass
list

Number of comparisons: 4+3+2+1=10 comparisons
QUESTION 4

By following the steps, sort the numbers below correctly.

8               8              8             5               2          2          2

9               9              3             3               3          2          2

2               2              2             2               2          3          3

2               2              2             2               5          5          5

5               5              5             8               8          8          8

3               3              9             9               9          9          9

11             10             10            10              10         10         10

10             11             11            11              11         11         11
2nd pass
Original

3rd pass

4th pass

5th pass

6th pass
1st pass
list

Number of comparisons: 6+5+4+3+2+1=21 comparisons
QUESTION 5

Arrange the number list below in the correct order and after that find the number of
comparisons.

28              12                3               3               3

28              28              28              12              12

25              25              25              25              25

3                3             12              28              28

12              28              28              28              28
2nd pass
Original

3rd pass

4th pass
1st pass
list

Number of comparisons: 4+3+2+1=10 comparisons
CREATE BY

FATIMAH BT JAFRI

PISMP MATH/BI/BM

IPGMK TENGKU AMPUAN AFZAN

```
To top