Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

SORTING

Example:
We have array A .Sort the following array in Ascending order.

5 2 1 4 6 3
0 1 2 3 4 5

Expected Result:
1 2 3 4 5 6
0 1 2 3 4 5

Strategy:
There are many strategies for solving this, but first we will use bubble sorting .

Repeat the following steps until the list is sorted:

1. Start with the first number in the list.


2. Compare each element with the element to right.
3. If they are out of order, then swap.

Sorting:
 First pass:
Step 1:

5 2 1 4 6 3

0 1 2 3 4 5 (index number)

Compare first two elements and swaps since 5>2

2 5 1 4 6 3

0 1 2 3 4 5

2nd step:

2 5 1 4 6 3

Now compare index 1 and 2 . swap since 5>1

2 1 5 4 6 3
0 1 2 3 4 5
3rd step:

2 1 5 4 6 3

0 1 2 3 4 5

Now compare index 2 and 3 . swap since 5 > 4

2 1 4 5 6 3
0 1 2 3 4 5

4th step:

2 1 4 5 6 3

0 1 2 3 4 5

now compare index 3 with 4. No swap since 5 < 6 .

2 1 4 5 6 3

0 1 2 3 4 5

5th step:

2 1 4 5 6 3

0 1 2 3 4 5

Now bubble will shift to 6 compare 4 index with 5 . swap since 6 >3

2 1 4 5 3 6

0 1 2 3 4 5

Pass 2:
Now again start from index 0 and compared them all.

Step 1:

2 1 4 5 3 6

0 1 2 3 4 5

Compare index 0 and 1 . swap since 2>1

1 2 4 5 3 6

0 1 2 3 4 5
Step 2:

1 2 4 5 3 6

0 1 2 3 4 5

Now compare index 1 and 2 . don’t swap 2 < 4 .

1 2 4 5 3 6

0 1 2 3 4 5

Step 3 :

1 2 4 5 3 6

0 1 2 3 4 5

Now bubble will shift to index 3 . we will compare index 3 with 4 . don’t swap because 4<5.

1 2 4 5 3 6

0 1 2 3 4 5

Step 4 :

1 2 4 5 3 6

0 1 2 3 4 5

Now bubble will shift to index 4 . we will compare index 4 with 5 . swap since 5 > 3.

1 2 4 3 5 6

0 1 2 3 4 5

Pass 3 :

Step 1 :

1 2 4 3 5 6

0 1 2 3 4 5 We will compare first two elements .since its sorted no


swapping .

1 2 4 3 5 6

0 1 2 3 4 5

Step 2:

1 2 4 3 5 6

0 1 2 3 4 5

Compare index 2 with 3 . swap because 4 < 3


1 2 3 4 5 6

0 1 2 3 4 5

Now the array is already sorted but our algorithm does not know the algorithm need one whole pass
without any swap to know it is sorted.

Pass 3:
Step 1:

1 2 3 4 5 6

1 2 3 4 5 6

Step 2 :

1 2 3 4 5 6

1 2 3 4 5 6

Step 3:

1 2 3 4 5 6

1 2 3 4 5 6

Step 4 :

1 2 3 4 5 6

1 2 3 4 5 6

Step 5 :
1 2 3 4 5 6

1 2 3 4 5 6

Pseudocode for Array :


i 0 to n-1
{if(A[i] > A[i+1])]}
{swap(A[i],A[i+1]}
Selection sorting
Strategy:
1. First find the smallest element.

2. Then swap it with the first element in the given array .

3. Then start search of another smaller element without looking into index 0.

4. Then swap with the element we started our search with.

5. Repeat the steps until the array is sorted.

Example:
We have an array A :

15 28 6 19 9

0 1 2 3 4 (index number)

Expected result:
6 9 15 19 28

Sorting:
Step 1:

15 28 6 19 9

0 1 2 3 4

we will start our search from index 0 and find the smallest element. Swap element of index 2 with index
0.

6 28 9 19 15

0 1 2 3 4

Step 2:

6 28 9 19 15

0 1 2 3 4
Now we will again start searching for smallest number but this time we will start search from index 1.

The smallest is at index 2 we will swap it with index 1.

6 9 28 19 15

0 1 2 3 4

Step 3:

6 9 28 19 15

0 1 2 3 4

Now start the search from index 2 and find smallest element.

The smallest is at index 4. Swap it with index 2.

6 9 15 19 28

0 1 2 3 4

You might also like