100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Sorting algorithms in C £4.99   Add to cart

Exam (elaborations)

Sorting algorithms in C

 1 view  0 purchase

Sorting algorithms in C

Preview 2 out of 5  pages

  • March 22, 2023
  • 5
  • 2022/2023
  • Exam (elaborations)
  • Unknown
All documents for this subject (8)
avatar-seller
micheleaccordino
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

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller micheleaccordino. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for £4.99. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

57114 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy revision notes and other study material for 14 years now

Start selling
£4.99
  • (0)
  Add to cart