• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Start selling Create your account
Document preview thumbnail
Preview 2 out of 5 pages
Exam (elaborations)

Sorting algorithms in C

Document preview thumbnail
Preview 2 out of 5 pages

Sorting algorithms in C

Content preview

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

Document information

Study
Unknown
Uploaded on
March 22, 2023
Number of pages
5
Written in
2022/2023
Type
Exam (elaborations)
Contains
Unknown
£5.49

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
1
Followers
0
Items
8
Last sold
3 months ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these revision notes.

Didn't get what you expected? Choose another document

No problem! You can straightaway pick a different document that better suits what you're after.

Pay as you like, start learning straight away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and smashed it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions