SOLENT UNIVERSITY
Assessment 6
Introduction to Sorting Algorithm
Sorting Algorithm is required in programming to rearrange elements of an array in a specific order by
making comparisons between them. When the code sort an array is comparing one element with all the other
and if the condition is true swap them till all the array is sorted out.
Example:
If the program had an array[]={17,89,54,2,21}; to sort in a decrescent numeric order will do so:
17>89 NO! 17 89 54 2 21
17>54 NO! 17 89 54 2 21
17>2 YES! SWAP 2 89 54 17 21
2>21 NO! 2 89 54 17 21
And start again to compare:
89>54 YES! SWAP 2 54 89 17 21
54>17 YES! SWAP 2 17 89 54 21
17>21 NO 2 17 89 54 21
Start again:
89>54 YES! SWAP 2 17 54 89 21
54>21 YES! SWAP 2 17 21 89 54
and last compare:
89>54 YES! SWAP array rearrange: 2 17 21 54 89.
The easy way, but not the only one, to sort a small array is using a Bubble Sort (slow). The program needs
three variables, two to compare the element (one of them used to print the array too) and the third for storage
temporary the value that will be swap.
Example Bubble Sort:
int array[]={17,89,54,2,21};
int i, j, temp;
for (i = 0; i < 5; i++) //Loop to print the unsorted array (5 is the size of the array)
printf("%d\t", n[i]);
printf("\n");
for (i = 0; i < 5 ; i++){ //This is the nested loop that will compare the value in i with j
for (j = 0; j < 5 ; j++){
if (n[i] > n[j]) { //Condition to swap the value
temp = n[j]; //The process to swap the elements between positions by using a temporary
variable
n[j] = n[i];
n[j] = temp;}
}
}
for (i = 0; i < 5 ; i++) //Loop to print the sorted array.
printf("%d\t", n[i]);
There are other way to sort an array, one of them it will be used in Grade A of this assessment.
59
, SOLENT UNIVERSITY
Grade D
In Grade D is required to create an array with a list of integers, gave from the assessment sheet 6. The pro -
gramme will need to be print each number of the array (n[i]) and compare it with position 0 (n[0]), by using
a for loop.
#include <stdio.h>
void gradeD() {
int n[] = {71, 49, 35, 3, 62, 66, 58, 83};
for (int i = 0; i < 8; i++) {
printf("\n%d\t%d", n[i], n[0]);
}
}
In figure 77, the programme is inside a loop, that print for each n[i] numbers the n[0] 71 on the side of it.
Fig77-Task6-GradeD
60
Assessment 6
Introduction to Sorting Algorithm
Sorting Algorithm is required in programming to rearrange elements of an array in a specific order by
making comparisons between them. When the code sort an array is comparing one element with all the other
and if the condition is true swap them till all the array is sorted out.
Example:
If the program had an array[]={17,89,54,2,21}; to sort in a decrescent numeric order will do so:
17>89 NO! 17 89 54 2 21
17>54 NO! 17 89 54 2 21
17>2 YES! SWAP 2 89 54 17 21
2>21 NO! 2 89 54 17 21
And start again to compare:
89>54 YES! SWAP 2 54 89 17 21
54>17 YES! SWAP 2 17 89 54 21
17>21 NO 2 17 89 54 21
Start again:
89>54 YES! SWAP 2 17 54 89 21
54>21 YES! SWAP 2 17 21 89 54
and last compare:
89>54 YES! SWAP array rearrange: 2 17 21 54 89.
The easy way, but not the only one, to sort a small array is using a Bubble Sort (slow). The program needs
three variables, two to compare the element (one of them used to print the array too) and the third for storage
temporary the value that will be swap.
Example Bubble Sort:
int array[]={17,89,54,2,21};
int i, j, temp;
for (i = 0; i < 5; i++) //Loop to print the unsorted array (5 is the size of the array)
printf("%d\t", n[i]);
printf("\n");
for (i = 0; i < 5 ; i++){ //This is the nested loop that will compare the value in i with j
for (j = 0; j < 5 ; j++){
if (n[i] > n[j]) { //Condition to swap the value
temp = n[j]; //The process to swap the elements between positions by using a temporary
variable
n[j] = n[i];
n[j] = temp;}
}
}
for (i = 0; i < 5 ; i++) //Loop to print the sorted array.
printf("%d\t", n[i]);
There are other way to sort an array, one of them it will be used in Grade A of this assessment.
59
, SOLENT UNIVERSITY
Grade D
In Grade D is required to create an array with a list of integers, gave from the assessment sheet 6. The pro -
gramme will need to be print each number of the array (n[i]) and compare it with position 0 (n[0]), by using
a for loop.
#include <stdio.h>
void gradeD() {
int n[] = {71, 49, 35, 3, 62, 66, 58, 83};
for (int i = 0; i < 8; i++) {
printf("\n%d\t%d", n[i], n[0]);
}
}
In figure 77, the programme is inside a loop, that print for each n[i] numbers the n[0] 71 on the side of it.
Fig77-Task6-GradeD
60