Escrito por estudiantes que aprobaron Inmediatamente disponible después del pago Leer en línea o como PDF ¿Documento equivocado? Cámbialo gratis 4,6 TrustPilot
logo-home
Examen

Sorting algorithms in C

Puntuación
-
Vendido
-
Páginas
5
Grado
A+
Subido en
22-03-2023
Escrito en
2022/2023

Sorting algorithms in C

Institución
Grado

Vista previa del contenido

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

Escuela, estudio y materia

Institución
Estudio
Desconocido
Grado

Información del documento

Subido en
22 de marzo de 2023
Número de páginas
5
Escrito en
2022/2023
Tipo
Examen
Contiene
Desconocido

Temas

$7.55
Accede al documento completo:

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Conoce al vendedor
Seller avatar
micheleaccordino

Conoce al vendedor

Seller avatar
micheleaccordino Southampton Solent University
Seguir Necesitas iniciar sesión para seguir a otros usuarios o asignaturas
Vendido
1
Miembro desde
3 año
Número de seguidores
0
Documentos
8
Última venta
1 mes hace

0.0

0 reseñas

5
0
4
0
3
0
2
0
1
0

Recientemente visto por ti

Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes