Sorting
Sorting is any process of arranging items
systematically.
ascending :A to Z, 0 to 9
descending order: Z to A, 9 to 0
For dates and times, ascending means that earlier values
precede later ones e.g. 1/1/2000 will sort ahead of
1/1/2001
Sorting
Four algorithms are described
1. Selection sort
2. Bubble sort
3. Insertion Sort
4. Quick Sort (Assignment)
Selection Sort
In the first pass, each item in the list is
compared with the first item in the list
If the first item in the list is bigger then the
item being compared then they are swapped.
Selection Sort
Swap
7 5 9 6 1 8 2 0 3 4
1st Comparison
Selection Sort
5 7 9 6 1 8 2 0 3 4
Selection Sort
5 7 9 6 1 8 2 0 3 4
2nd Comparison
Selection Sort
5 7 9 6 1 8 2 0 3 4
3rd Comparison
Selection Sort
Swap
5 7 9 6 1 8 2 0 3 4
4th Comparison
Selection Sort
1 7 9 6 5 8 2 0 3 4
5th Comparison
Selection Sort
1 7 9 6 5 8 2 0 3 4
6th Comparison
Selection Sort
Swap
1 7 9 6 5 8 2 0 3 4
7th Comparison
Selection Sort
0 7 9 6 5 8 2 1 3 4
Selection Sort
0 7 9 6 5 8 2 1 3 4
8th Comparison
Selection Sort
0 7 9 6 5 8 2 1 3 4
9th Comparison
Selection Sort
0 7 9 6 5 8 2 1 3 4
1st Comparison
Selection Sort
Swap
0 7 9 6 5 8 2 1 3 4
2nd Comparison
Selection Sort
0 6 9 7 5 8 2 1 3 4
Selection Sort
Swap
0 6 9 7 5 8 2 1 3 4
3rd Comparison
Selection Sort
0 5 9 7 6 8 2 1 3 4
Selection Sort
0 5 9 7 6 8 2 1 3 4
4th Comparison
Selection Sort
Swap
0 5 9 7 6 8 2 1 3 4
5th Comparison
Selection Sort
0 2 9 7 6 8 5 1 3 4
Selection Sort
And so on…
0 2 9 7 6 8 5 1 3 4
Selection Sort
until…
0 1 2 3 4 5 6 7 8 9
Selection Sort Algorithm
Step 1. [Starting Selection Loop]
Repeat step 2 For S=1 to N-1 by 1
Step 2. [Starting Comparison Loop]
Repeat step 3 For C=S+1 to N by 1
Step 3. [Compare Element]
If(A[s]> A[c]) Then
Temp=A[S]
A[S]=A[C]
A[C]=Temp
Exit
Bubble Sort
Bubblesort is a simple algorithm that
repeatedly steps through the list, compares
adjacent pairs and swap them if they are in
wrong order. The pass through the list is
repeated until list is sorted.
Bubble sort Example
Swap
7 5 9 6 1 8 2 0 3 4
First Comparison
Bubble sort
5 7 9 6 1 8 2 0 3 4
Bubble sort
5 7 9 6 1 8 2 0 3 4
Second Comparison
Bubble sort
Swap
5 7 9 6 1 8 2 0 3 4
Third Comparison
Bubble sort
5 7 6 9 1 8 2 0 3 4
Bubble sort
Swap
5 7 6 9 1 8 2 0 3 4
Fourth Comparison
Bubble sort
5 7 6 1 9 8 2 0 3 4
Bubble sort
Swap
5 7 6 1 9 8 2 0 3 4
Fifth Comparison
Bubble sort
5 7 6 1 8 9 2 0 3 4
Bubble sort
Swap
5 7 6 1 8 9 2 0 3 4
Sixth Comparison
Bubble sort
5 7 6 1 8 2 9 0 3 4
Bubble sort
Swap
5 7 6 1 8 2 9 0 3 4
Seventh Comparison
Bubble sort
5 7 6 1 8 2 0 9 3 4
Bubble sort
Swap
5 7 6 1 8 2 0 9 3 4
8th Comparison
Bubble sort
5 7 6 1 8 2 0 3 9 4
Bubble sort
Swap
5 7 6 1 8 2 0 3 9 4
9th Comparison
Bubble sort
5 7 6 1 8 2 0 3 4 9
Notice… we are sorting list into an ascending list. The largest number is
now at the end of the list…where it should be!
This completes the first pass through the list.
Bubble sort
5 7 6 1 8 2 0 3 4 9
1st Comparison Second Pass
The process begins again.
Bubble sort
Swap
5 7 6 1 8 2 0 3 4 9
2nd Comparison Second Pass
Bubble sort
5 6 7 1 8 2 0 3 4 9
Second Pass
Bubble sort
Swap
5 6 7 1 8 2 0 3 4 9
3rd Comparison Second Pass
Bubble sort
5 6 1 7 8 2 0 3 4 9
Second Pass
Bubble sort
5 6 1 7 8 2 0 3 4 9
4th Comparison Second Pass
Bubble sort
Swap
5 6 1 7 8 2 0 3 4 9
5th Comparison Second Pass
Bubble Sort
Write an algorithm to sort an array A consisting of
N elements in ascending order using bubble sort
Suppose variable U represents the control variable
for upper loop to control the number of iteration .
Similarly , variable I represent the control variable
for inner loop that scans the array starting from
element A[1] to A[U]
Cont.……
1. Set U=N-1
2. [Upper loop]
Repeat step 3 to 7 while (U>=1)
3. Set I=1
4. [Inner Loop]
Repeat step 5 to 6 while (I<=U)
5. IF A[I]>A[I+1]
T=A[I]
A[I]=A[I+1]
A[I+1]=T
End IF
6. I=I+1 [End of inner loop]
7. U=U-1 [End of Upper loop]
8. EXIT
Insertion Sort
In insertion sort algorithm, compare the value
until all the previous value is lesser than
compared value. Insertion sort is more
efficient than bubble sort because in insertion
sort the elements comparisons are lesser as
compared to bubble sort. Insertion sort is
suitable for small data.
Insertion Sort Example
Algorithm for insertion sort
Write an algorithm to sort an array A
consisting of N elements in ascending order
using Insertion sort method
CONT.….
1. REPEAT STEP 2 TO 6 FOR C=2 TO N
2. TEMP=A[C]
3. L=C
4. REPEAT STEP 4 to 6 WHILE (L>0 AND TEMP<A[L-1])
5. A[L]=A[L-1]
L=L-1 [END OF STEP 4 INNER LOOP]
6. A[L]=TEMP [INSERT VALUE]
[END OF STEP 1 UPPER LOOP]
7. EXIT