Table of contents
1
Searching
Sequential / Linear Search 2
Binary Search 3
Sorting
Selection Sort 4
Radix Sort 5 – 8
Quick Sort 9 – 12
Insertion Sort 13 – 14
Bubble Sort 15 - 17
1. i = 0
2. Read value of key to be searched.
3. if k( i ) == key
Display "Recode found at position"
go to step 7
4. increment k
5. if i < n
go to step 3
6. Display "No match found!"
7. Stop
Algorithm of Sequential / Linear Search
2
Binary Search
1. Accept Key
2. Top = 0, Bottom = n-1
3. m=(Top + Bottom) / 2
4. if (key==k(mid))
Display Record found at position mid.
go to step 8
5. if key< k(mid)
else
top=m+1
6. if (top<=bottom)
go to step 3
7. Display Record Found
8. Stop
3
Algorithm of Selection Sort
1. Start
2. Pass = 1
3. i = 0
4. j = i+1
5. if x[i] > x[j]
interchange x[i] and x[i+1]
6. j = j+1
7. if j < = n-1
Go to step 5
8. Pass = Pass + 1
9. if pass < = n-1
then i = i+1 and go to step 4
10. Stop
4
Algorithm of Radix Sort
This method is based on the value of actual digits in each position of the numbers being sorted.
Example: 132 has 2 in the unit's position , 3 in the ten's position and 1 in the hundred's position.
Start comparing from the most significant digits and move towards the last digit as long as the
digits are equal. if there is no match, the number with digit is the greater number.
This method used the above techniques.
1. The number are partitioned into ten groups based on their unit's digit.
2. Elements within individual groups are sorted on ten's digit.
3. The process is repeated till each sub group has been subdivided an most significant digit.
in the above method, a lot of partitions have to be created which complexity.
Comparing two numbers of equal length
5
Inputs Packets
0 1 2 3 4 5 6 7 8 9
612 612
912 912
796 796
412 412
432 432
100 100
206 206
First Pass
Example of Radix Sort
Example: 612, 912, 796, 412, 432, 100, 206
6
Inputs Packets
0 1 2 3 4 5 6 7 8 9
100 100
612 612
912 912
412 412
432 432
796 796
206 206
Second Pass
7
Inputs Packets
0 1 2 3 4 5 6 7 8 9
100 100
206 206
612 612
912 912
412 412
432 432
796 796
Third Pass
Sorted list : 100, 206, 412, 432, 612, 796, 912
8
1. Start
2. A is an array of 'n' elements.
3. lb=0, ub = n-1 ( ub → upper bound, lb → lower bound)
4. if ( lb < ub ) i.e. if the array can be partitioned.
j = partition (A, lb, j-1) // j is the pivot position
Quicksort (A, lb, j-1) // sort the first partition
Quicksort (A, j+1, ub ) // sort the second partition
5. Stop
Algorithm of Quick Sort
9
Pivot position = up
First sub array = 23 7 48 32 18
Second partition = 82 62
[55] 7 48 32 18 23 82 62
Partition I Partition II
Pivot = A [lb] = 55
Move dn (82 is greater than pivot so stop
incrementing dn)
Move up (23 is less than pivot so stop
decrementing up)
Interchange A[dn] and A[up]
updn
Example of Quick Sort
[55] 7 48 32 18 23 82 62
[55] 7 48 32 18 23 82 62
23 7 48 32 18 [55] 82 62
Example : 55, 7, 48, 32, 18, 23, 82, 62
dn
dn
dn
up
up
up
10
1. down = lb
2. up = ub
3. pivot = A[lb]
4. While (A[down] < = pivot && down < ub)
down ++
5. while (A[up] > pivot && up > lb)
up --
6. if (down < up)
interchange A [down] and A [up]
go to step 4
7. interchange A[up] and pivot
8. return up
9. Stop
Algorithm of Quick Sort (Partitions)
11
[23] 7 48 32 18
[23] 7 48 32 18
[23] 7 48 32 18
[23] 7 18 32 48
[23] 7 18 32 48
18 7 [23] 32 48
[23] 7 18 32 48
Partition I Partition II
Pivot = A [lb] = 23
Move dn (48 is greater than pivot - 23
so stop incrementing dn)
Move up (18 is less than pivot - 23
so stop decrementing up)
Interchange A[dn] and A[up]
Move dn
Move up
Interchange A[up] and pivot
updn
dn
dn
dn
dn
dn
up
up
up
up
dn up
up
Example of Quick Sort (Partitions)
Example: 55, 7, 48, 32, 18, 23, 82, 62
12
Algorithm of Insertions Sort
• In the first pass the 1st element 7 is compared with 0th element 15, 7 is inserted
at 0th place, since 7 is smaller than 15. The 0th element. i.e. 15 is moved one
position to the right.
• In the second pass, the 2nd element 22 is compared with 0th element 7. hence 7
is smaller than 22 then no change. Then 2nd element 22 is compared with 1st
element 15 and again no change.
• In the third pass 3rd element is compared with 0th element. As 3 is smaller than
7, 3 is inserted at 0th place and all the elements from 0th till 2nd position are
moved to right by one position.
• This procedure is repeated for all the element of array. In the last pass array gets
sorted.
13
Example of Insertions Sort
15 7 22 3 14 2
7 15 22 3 14 2
7 15 22 3 14 2
7 15 22 3 14 2
3 7 15 22 14 2
3 7 15 22 14 2
3 7 15 22 14 2
3 7 15 22 14 2
3 7 15 22 14 2
3 7 14 15 22 2
3 7 14 15 22 2
2 3 7 14 15 22
First pass
Second pass
Third pass
Fifth pass
Sorted array
Fourth pass
14
Algorithm of Bubble Sort
1. Start
2. Pass = 1
3. i = 0
4. If x[i] > x(i + 1)
interchange x[i] and x[I + 1]
5. i = i + 1
6. If i<=n-1- Pass
go to Step 4
7. Pass =Pass + 1
8. If Pass < = n-1
go to Step 3
9. Stop
15
Example of Bubble Sort
Example: 25, 37, 12, 48, 57, 33
25 25 25 25 25 25
37 37 12 12 12 12
12 12 37 37 37 37
48 48 48 48 48 48
57 57 57 57 57 33
33 33 33 33 33 57
12 12 12 12 12
25 25 25 25 25
37 37 37 37 37
48 48 48 48 33
33 33 33 33 48
57 57 57 57 57
Pass 1 Pass 2
16
Example of Bubble Sort
Example: 25, 37, 12, 48, 57, 33
12 12 12 12
25 25 25 25
37 37 37 33
33 33 33 37
48 48 48 48
57 57 57 57
12 12 12
25 25 25
33 33 33
37 37 37
48 48 48
57 57 57
Pass 3 Pass 4
12 12
25 25
33 33
37 37
48 48
57 57
Pass 5
17

Sorting and Searching - Data Structure - Notes

  • 1.
    Table of contents 1 Searching Sequential/ Linear Search 2 Binary Search 3 Sorting Selection Sort 4 Radix Sort 5 – 8 Quick Sort 9 – 12 Insertion Sort 13 – 14 Bubble Sort 15 - 17
  • 2.
    1. i =0 2. Read value of key to be searched. 3. if k( i ) == key Display "Recode found at position" go to step 7 4. increment k 5. if i < n go to step 3 6. Display "No match found!" 7. Stop Algorithm of Sequential / Linear Search 2
  • 3.
    Binary Search 1. AcceptKey 2. Top = 0, Bottom = n-1 3. m=(Top + Bottom) / 2 4. if (key==k(mid)) Display Record found at position mid. go to step 8 5. if key< k(mid) else top=m+1 6. if (top<=bottom) go to step 3 7. Display Record Found 8. Stop 3
  • 4.
    Algorithm of SelectionSort 1. Start 2. Pass = 1 3. i = 0 4. j = i+1 5. if x[i] > x[j] interchange x[i] and x[i+1] 6. j = j+1 7. if j < = n-1 Go to step 5 8. Pass = Pass + 1 9. if pass < = n-1 then i = i+1 and go to step 4 10. Stop 4
  • 5.
    Algorithm of RadixSort This method is based on the value of actual digits in each position of the numbers being sorted. Example: 132 has 2 in the unit's position , 3 in the ten's position and 1 in the hundred's position. Start comparing from the most significant digits and move towards the last digit as long as the digits are equal. if there is no match, the number with digit is the greater number. This method used the above techniques. 1. The number are partitioned into ten groups based on their unit's digit. 2. Elements within individual groups are sorted on ten's digit. 3. The process is repeated till each sub group has been subdivided an most significant digit. in the above method, a lot of partitions have to be created which complexity. Comparing two numbers of equal length 5
  • 6.
    Inputs Packets 0 12 3 4 5 6 7 8 9 612 612 912 912 796 796 412 412 432 432 100 100 206 206 First Pass Example of Radix Sort Example: 612, 912, 796, 412, 432, 100, 206 6
  • 7.
    Inputs Packets 0 12 3 4 5 6 7 8 9 100 100 612 612 912 912 412 412 432 432 796 796 206 206 Second Pass 7
  • 8.
    Inputs Packets 0 12 3 4 5 6 7 8 9 100 100 206 206 612 612 912 912 412 412 432 432 796 796 Third Pass Sorted list : 100, 206, 412, 432, 612, 796, 912 8
  • 9.
    1. Start 2. Ais an array of 'n' elements. 3. lb=0, ub = n-1 ( ub → upper bound, lb → lower bound) 4. if ( lb < ub ) i.e. if the array can be partitioned. j = partition (A, lb, j-1) // j is the pivot position Quicksort (A, lb, j-1) // sort the first partition Quicksort (A, j+1, ub ) // sort the second partition 5. Stop Algorithm of Quick Sort 9
  • 10.
    Pivot position =up First sub array = 23 7 48 32 18 Second partition = 82 62 [55] 7 48 32 18 23 82 62 Partition I Partition II Pivot = A [lb] = 55 Move dn (82 is greater than pivot so stop incrementing dn) Move up (23 is less than pivot so stop decrementing up) Interchange A[dn] and A[up] updn Example of Quick Sort [55] 7 48 32 18 23 82 62 [55] 7 48 32 18 23 82 62 23 7 48 32 18 [55] 82 62 Example : 55, 7, 48, 32, 18, 23, 82, 62 dn dn dn up up up 10
  • 11.
    1. down =lb 2. up = ub 3. pivot = A[lb] 4. While (A[down] < = pivot && down < ub) down ++ 5. while (A[up] > pivot && up > lb) up -- 6. if (down < up) interchange A [down] and A [up] go to step 4 7. interchange A[up] and pivot 8. return up 9. Stop Algorithm of Quick Sort (Partitions) 11
  • 12.
    [23] 7 4832 18 [23] 7 48 32 18 [23] 7 48 32 18 [23] 7 18 32 48 [23] 7 18 32 48 18 7 [23] 32 48 [23] 7 18 32 48 Partition I Partition II Pivot = A [lb] = 23 Move dn (48 is greater than pivot - 23 so stop incrementing dn) Move up (18 is less than pivot - 23 so stop decrementing up) Interchange A[dn] and A[up] Move dn Move up Interchange A[up] and pivot updn dn dn dn dn dn up up up up dn up up Example of Quick Sort (Partitions) Example: 55, 7, 48, 32, 18, 23, 82, 62 12
  • 13.
    Algorithm of InsertionsSort • In the first pass the 1st element 7 is compared with 0th element 15, 7 is inserted at 0th place, since 7 is smaller than 15. The 0th element. i.e. 15 is moved one position to the right. • In the second pass, the 2nd element 22 is compared with 0th element 7. hence 7 is smaller than 22 then no change. Then 2nd element 22 is compared with 1st element 15 and again no change. • In the third pass 3rd element is compared with 0th element. As 3 is smaller than 7, 3 is inserted at 0th place and all the elements from 0th till 2nd position are moved to right by one position. • This procedure is repeated for all the element of array. In the last pass array gets sorted. 13
  • 14.
    Example of InsertionsSort 15 7 22 3 14 2 7 15 22 3 14 2 7 15 22 3 14 2 7 15 22 3 14 2 3 7 15 22 14 2 3 7 15 22 14 2 3 7 15 22 14 2 3 7 15 22 14 2 3 7 15 22 14 2 3 7 14 15 22 2 3 7 14 15 22 2 2 3 7 14 15 22 First pass Second pass Third pass Fifth pass Sorted array Fourth pass 14
  • 15.
    Algorithm of BubbleSort 1. Start 2. Pass = 1 3. i = 0 4. If x[i] > x(i + 1) interchange x[i] and x[I + 1] 5. i = i + 1 6. If i<=n-1- Pass go to Step 4 7. Pass =Pass + 1 8. If Pass < = n-1 go to Step 3 9. Stop 15
  • 16.
    Example of BubbleSort Example: 25, 37, 12, 48, 57, 33 25 25 25 25 25 25 37 37 12 12 12 12 12 12 37 37 37 37 48 48 48 48 48 48 57 57 57 57 57 33 33 33 33 33 33 57 12 12 12 12 12 25 25 25 25 25 37 37 37 37 37 48 48 48 48 33 33 33 33 33 48 57 57 57 57 57 Pass 1 Pass 2 16
  • 17.
    Example of BubbleSort Example: 25, 37, 12, 48, 57, 33 12 12 12 12 25 25 25 25 37 37 37 33 33 33 33 37 48 48 48 48 57 57 57 57 12 12 12 25 25 25 33 33 33 37 37 37 48 48 48 57 57 57 Pass 3 Pass 4 12 12 25 25 33 33 37 37 48 48 57 57 Pass 5 17