Question: SORTING ARRAY To sort we need: Ability to compare elements Ability to swap elements 0 1 2 3 4 5 6 7 8 9 0
SORTING ARRAY
To sort we need:
- Ability to compare elements
- Ability to swap elements
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 0 | 1 | 2 | 4 | 9 | 7 | 8 | 77 | 88 | 67 |
Here is a type of sort:
Objective: sort items in order of smallest to largest.
- Find the smallest element in the active portion of the array
- Swap smallest element with first element in active portion of the array
- Reduce active portion of array by one
- Repeat until complete
He is some pseudocode that shows how this can be accomplished.
// control the active portion of the array
For(int arr_start=0; arr_start < SIZEOFARRAY; arr_start ++){
// find the smallest element in active portion
Smallest = array_name[arr_start]; // assumed smallest
Index_of_smallest = arr_start;
For(j= arr_start ;j< SIZEOFARRAY; j++){
// get smallest
If array_name[j] < smallest{
Smallest = array_name[j]
Index_of_smallest = j
}
}
// swap smallest with first element in active portion of array
Temp = array_name[arr_start]
array_name[arr_start] = array_name[index_of_smallest]
array_name[index_of_smallest] = Temp
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
