Page 112 - Computer science 868 Class 12
P. 112
Step 10: Display search_value not found.
Step 11: Stop.
3.5.4 Sorting Algorithms
Problem 12: Write an algorithm to sort an array of a given size in ascending order using the BUBBLE SORT
technique.
Note: Bubble Sort program is already covered in the Array chapter of this book.
Step 1: Start. 9 6 2 8 3 1 7 5 4
Step 2: Initialise variable i to 0.
6 2 8 3 1 7 5 4 9
Step 3: Repeat Step 4 to Step 9 while i < size -1.
2 6 3 1 7 5 4 8 9
Step 4: Initialise variable j to 0.
2 3 1 6 5 4 7 8 9
Step 5: Repeat Step 6 to Step 8 while j < size -i -1.
Step 6: If array[j] > array[j+1] then go to Step 7, else go to Step 8. 2 1 3 5 4 6 7 8 9
Step 7: Swap a[j] and a[j+1]. 1 2 3 4 5 6 7 8 9
Step 8: Increment j by 1. 1 2 3 4 5 6 7 8 9
Step 9: Increment i by 1. 1 2 3 4 5 6 7 8 9
Step 10: Display the sorted array.
Step 11: Stop.
Problem 13: Write an algorithm to sort an array of a given size in ascending order using the SELECTION SORT
technique.
Note: Selection Sort program is already covered in the Array chapter of this book.
Step 1: Start.
Step 2: Initialise variable i to 0.
Step 3: Repeat Step 5 to Step 12 while i < size - 1.
Step 4: Initialise small to array[i] and pos to i.
Step 5: Initialise j to i + 1.
Step 6: Repeat Step 7 to Step 10 while j < size.
Step 7: If array[j] < s then go to Step 8, else go to Step 10. 6 3 7 2 8 1 *
Step 8: Assign s = array[j]. 1 3 7 2 * 8 6
Step 9: Assign pos = j. *
Step 10: Increment j by 1. 1 2 7 3 8 6
Step 11: Swap array[i] and array[pos]. 1 2 3 7 8 6 *
Step 12: Increment i by 1. 1 2 3 6 8 7 *
Step 13: Display the sorted array. 1 2 3 6 7 8
Step 14: Stop.
110110 Touchpad Computer Science-XII

