Unit 3 - Searching and Sorting
Unit 3 - Searching and Sorting
UNIT III
PASS 1 7 4 2 3 6 1
j=0, 7>4 4 7 2 3 6 1
j=1, 7>2 4 2 7 3 6 1
j=2, 7>3 4 2 3 7 6 1
j=3, 7>6 4 2 3 6 7 1
j=4, 7>1 4 2 3 6 1 7
PASS 2 4 2 3 6 1 7
j=0, 4>2 2 4 3 6 1 7
j=1, 4>3 2 3 4 6 1 7
j=2, 4<6 2 3 4 6 1 7
j=3, 6>1 2 3 4 1 6 7
PASS 3 2 3 4 1 6 7
j=0, 2<3 2 3 4 1 6 7
j=1, 3<4 2 3 4 1 6 7
j=2, 4>1 2 3 1 4 6 7
PASS 4 2 3 1 4 6 7
j=0, 2>3 2 3 1 4 6 7
j=1, 3>1 2 1 3 4 6 7
PASS 5 2 1 3 4 6 7
j=0, 2<3 1 2 3 4 6 7
1 2 3 4 6 7
Bubble (A, N)
1.Repeat Step 2 for I=1 to N-1.
Bubble (A, N)
1.Repeat Step 2, 3 and 5 for I=1 to N-1.
2. Set flag=0
5. if(flag = =0)
6. then break
PASS 1
7 2 4 6 3 1
Key=2, j=1
i=0, 7>2 7 7 4 6 3 1
i=-1, A[0]= 2 2 7 4 6 3 1
PASS 2
2 7 4 6 3 1
Key=4, j=2
i=1, 7>4 2 7 7 6 3 1
PASS 3
Key=6, j=3 2 4 7 6 3 1
i=2, 7>6 2 4 7 7 3 1
PASS 4
Key=3, j=4 2 4 6 7 3 1
i=3, 7>3 2 4 6 7 7 1
i=2, 6>3 2 4 6 6 7 1
i=1, 4>3 2 4 4 6 7 1
PASS 5
Key=1, j=5 2 3 4 6 7 1
i=4, 7>1 2 3 4 6 7 7
i=3, 6>1 2 3 4 6 6 7
i=2, 4>1 2 3 4 4 6 7
i=1, 3>1 2 3 3 4 6 7
i=0, 2>1 2 2 3 4 6 7
i=-1, A[0]= 1 1 2 3 4 6 7
INSERTION_SORT (A, N)
1. Repeat Step 2 to 5 for j = 1, 2, …, N-1:
2. Key = A[j]
3. i = j – 1.
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
2 3 4 6 8 9 done
SELECTION SORT ALGORITHM
Selection_Sort (A, n)
1. For i = 0 to n - 2
2. Set Min = A[i]
3. loc = i
4. For j = i + 1 to n - 1
5. if(A[j] < Min) then:
6. Min = A[j].
7. loc=j
8. if (loc != i)
9. Then swap (A[i], A[loc]).
PASS 1
Min=7, loc=0 7 4 2 3 6 1
PASS 2
Min=4, loc=1 1 4 2 3 6 7
PASS 3
Min=4, loc=2 1 2 4 3 6 7
PASS 4
Min=4, loc=3 1 2 3 4 6 7
PASS 5
Min=6, loc=4 1 2 3 4 6 7
MS(A, 0, 5)
6 1 3 9 5 8
MS(A, 0, 2) MS(A, 3, 5)
6 1 3 9 5 8
MS (A, 2, 2) MS (A, 5, 5)
MS (A, 0, 1) MS (A, 3, 4)
6 1 3 9 5 8
MS (A, 0, 0) MS (A, 1, 1) MS (A, 3, 3) MS (A, 4, 4)
6 1 9 5
Merge (A, 0, 0, 1)
Merge (A, 3, 3, 4)
1 6 5 9
Merge (A, 3, 4, 5)
Merge (A, 0, 1, 2)
1 3 6 5 8 9
Merge (A, 0, 2, 5)
1 3 5 6 8 9
MergeSort (A, 0, 1)
MergeSort (A, 1, 1) SORTED
SORTED
MergeSort (A, 0, 2) Merge (A, 0, 0, 1)
SORTED
MergeSort (A, 2, 2)
SORTED
MergeSort (A, 0, 5) Merge (A, 0, 1, 2)
SORTED
SORTED MergeSort (A, 3, 3)
Merge (A, 3, 4, 5)
Merge (A, 0, 2, 5)
QUICKSORT(A, p, r) PARTITION(A, p, r)
1 if p < r 1 Key = A[r]
2 then q ← PARTITION(A, p, r) 2 i=p
3 QUICKSORT(A, p, q - 1) 3 for j = p to r - 1
4 QUICKSORT(A, q + 1, r) 4 do if A[j] <= Key
5 then exchange A[i] ↔ A[j]
6 i=i+1
7 exchange A[i] ↔ A[r]
8 return i
2 1 3 4 6 7
Swap(1, 2) 1 2 3 4 6 7
1 2 3 4 6 7
1 2 3 4 6 7
j=4, 4<7,
1 2 3 4 6 7
swap(4,4),set i=5
j=5, 6<7, 1 2 3 4 6 7
swap(6,6),set i=6
swap(7,7) 1 2 3 4 6 7
QUICK SORT(A, 5, 4)
QUICK SORT(A, 3, 4) low>high
1 2 3 4 6 7
Key = 6
Set i= 4 QUICK SORT(A, 3, 4)
j=4, 4<6,
1 2 3 4 6 7
swap(4,4),set i=5
swap(7,7) 1 2 3 4 6 7
QUICK SORT(A, 5, 4)
QUICK SORT(A, 3, 3)
low=high low>high
1 2 3 4 6 7
0 4 2 1 0 2
2 2 3 5 5 6 2
(0) (1) (2) (3) (4) (0) (1) (2) (3) (4) (5)
0 2 3 4 5 6 0 2
(0) (1) (2) (3) (4) (0) (1) (2) (3) (4) (5)
1 1 3 4 5 6 0 1 2
(0) (1) (2) (3) (4) (0) (1) (2) (3) (4) (5)
2 1 2 4 5 6 0 1 2 2
(0) (1) (2) (3) (4) (0) (1) (2) (3) (4) (5)
4 1 2 3 5 6 0 1 2 2 4
(0) (1) (2) (3) (4) (0) (1) (2) (3) (4) (5)
0 1 2 3 5 5 0 0 1 2 2 4
(0) (1) (2) (3) (4) (0) (1) (2) (3) (4) (5)
Nagesh Sharma, A.P. Department of CSE (AI), KIET Group of Institutions
RADIX SORT ALGORITHM
RADIX-SORT(A, d)
1 for i ← 1 to d
2 do use a stable sort to sort array A on digit i
0 0 0 0 0 0 0 0 0 0
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
0 1 0 2 0 1 0 1 1 0
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
0 1 1 3 3 4 4 5 6 6
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
0 1 1 3 3 4 4 5 6 6 205
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9) (0) (1) (2) (3) (4) (5)
0 1 1 3 3 3 4 5 6 6 431 205
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9) (0) (1) (2) (3) (4) (5)
0 0 1 1 3 3 4 4 5 6
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
SORTING ARRAY ON DIGIT 2
0 0 0 0 0 0 0 0 0 0
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
1 2 1 1 0 1 0 0 0 0
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
1 3 4 5 5 6 6 6 6 6
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
1 3 4 5 5 6 6 6 6 6 918
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9) (0) (1) (2) (3) (4) (5)
1 2 4 5 5 6 6 6 6 6 918 657
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9) (0) (1) (2) (3) (4) (5)
0 1 3 4 5 5 6 6 6 6
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
SORTING ARRAY ON DIGIT 3
0 0 0 0 0 0 0 0 0 0
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
0 0 2 0 2 0 1 0 0 1
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
0 0 2 2 4 4 5 5 5 6
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
0 0 2 2 4 4 5 5 5 6 657
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9) (0) (1) (2) (3) (4) (5)
0 0 2 2 4 4 4 5 5 6 431 657
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9) (0) (1) (2) (3) (4) (5)
0 0 0 2 2 4 4 5 5 5
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
COMPARISON AMONG DIFFERENT SORTING
Linear Search:
• Compares the item of interest with each element ofArray
one by one.
LINEAR_SEARCH ( A, N, key)
1. Set LOC = -1
Call Inside Calling Function
2. Repeat Steps 2 to 7 for j = 0 to N-1 LINEAR_SEARCH(A, N, Key)
3. If Key = = A[j]
LINEAR_SEARCH ( A, N, key)
1. If (N = = 0)
Call Inside Calling Function
2. return -1 //if Element does not exists in the Array LINEAR_SEARCH(A, N, Key)
3. If A[N-1] == key
3. If Key = = A[mid]
8. return -1
4. If Key = = A[mid]