You are on page 1of 4

Mohammad Ali Jinnah University

Computer Programming
Assignment 03

Date: 25 May 2021 Submission Date: 30 May 2021

Let’s Sort

Following is an algorithm to sort a series of numbers in ascending/descending order.

a) Find the smallest number in series(array).


b) Swap the value with the first element of the array.
c) Now the first element is sorted, so repeat the steps a and b for the rest of the array i.e.
from second element to the last element.
d) Repeat the steps a to c until the last element is sorted.

Implement the above algorithm in C language

1. Test the algorithm on the numbers of elements equal to the number of alphabets your
first name has.

2. Test the algorithm on the array of marks you got in mid exams.

HELP ME!
For more clarity the illustration and example of the above algorithm is given below.

For example below is a series(array) to be sorted using above example

0 1 2 3 4 5 6

6 -->First 2 5 8 1 9 10

- After step a, 1 is found as the smallest element.


0 1 2 3 4 5 6

6-->First 2 5 8 1 9 10

- Step b will swap the smallest element found with first element of the array
- Now the first element is sorted, and the first will move to the next position.
0 1 2 3 4 5 6

1 2--->First 5 8 6 9 10

- As step c says, we’ll repeat step 1 and 2.


- After step a,2 is found as smallest element

0 1 2 3 4 5 6

1 2--->First 5 8 6 9 10

2 will be replaced with the first/start element so it remains in the same position. Second
element is also sorted and the third element is the first/start now.
0 1 2 3 4 5 6

1 2 5--->First 8 6 9 10

- As step c says, we’ll repeat step 1 and 2.


- After step a, 5 is found as smallest element
0 1 2 3 4 5 6

1 2 5--->First 8 6 9 10

5 will be replaced with the first/start element so it remains in the same position. Third element
is also sorted and the fourth element is the first/start now.

0 1 2 3 4 5 6

0 1 2 3 4 5 6
1 2 5 8--->First 6 9 10

- As step c says, we’ll repeat step 1 and 2.


- After step a, 6 is found as smallest element

0 1 2 3 4 5 6

1 2 5 8--->First 6 9 10

In step b, 6 will be swapped with the first of the array which is 8 and start will move to the next
position.
0 1 2 3 4 5 6

1 2 5 6 8--->First 9 10

Fourth element is sorted now.


- As step c says, we’ll repeat step 1 and 2.
- After step a, 8 is found as smallest element

0 1 2 3 4 5 6

1 2 5 6 8--->First 9 10

In step b, 8 will remain in the same position as that’s the first element of the array. Fifth
element of the array is sorted so the start will move to the next position.
0 1 2 3 4 5 6

1 2 5 6 8 9--->First 10

- As step c says, we’ll repeat step 1 and 2.


- After step a, 9 is found as smallest element

0 1 2 3 4 5 6

1 2 5 6 8 9--->First 10
In step b, 9 will remain in the same position as that’s the first element of the array. Sixth
element of the array is sorted so the start will move to the next position.

0 1 2 3 4 5 6

1 2 5 6 8 9 10-->First

If the start of the array is the last index then no need to repeat any more. Array is said to be
sorted.

0 1 2 3 4 5 6

1 2 5 6 8 9 10

You might also like