answersLogoWhite

0


Best Answer

Use the insertion sort algorithm.

Insertion sort works on the premise that a set of 1 can always be regarded as a sorted set, thus in a set of n elements, we can divide the set in two, with a sorted set at the beginning (with 1 element) and an unsorted set at the end (with all other elements). We then take the first element of the unsorted set and insert it into the sorted set, repeating until the unsorted set is empty.

We take the first element out of the unsorted set by copying it to a temporary variable, thus creating a gap in its place. We then examine the element to the left of the gap and, if the value is greater than the temporary, we copy it into the gap, effectively moving the gap one place to the left. We stop moving the gap when we reach the start of the array or the element to the left of the gap is not greater than the temporary. We then copy the temporary into the gap, increasing the sorted set by one element and reducing the unsorted set by one element.

We can implement this algorithm using just one for loop:

void sort (int* a, unsigned s) { // assume a refers to an array of length s

for (int i=2; i<s; ++i) {

int t = a[i]; // temporarily store a[i] outside the array

int g = i; // index g is the "gap" in the array

while (0<g && t<a[g-1]) { a[g]=a[g-1]; --g; } // move the gap to the correct insertion point

a[g] = t; // insert the temporary

}

}

Thus, to sort 3 numbers:

int a[3] = {2, 3, 1}; // unsorted

sort (a, 3);

assert (a[0]==1);

assert (a[1]==2);

assert (a[2]==3);

User Avatar

Wiki User

7y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

Store the values in an array and perform an insertion sort.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you make a turbo c program by using one for loop to sort 3 numbers in ascending order?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are the program of turbo pascal?

tagprice


What is name of the software in which you create program?

Turbo c3 or Turbo c4. R.P. Hammad Khan_Haider...


Basic parts of TURBO C program?

Editor - edits text Compiler - compiles the program Linker - links the program Debugger - helps finding bugs


How do you load a program in turbo c?

File/Open


How can you use turbo c in Window7?

With program DosBox,


What is the firing order for a Toyota Supra Turbo?

The firing order on a Toyota supra turbo is 1,5,3,6,2,4


Who is the founder of turbo c program?

Turbo c program is from Borland which is an integrated development environment and a computer language. Some of the parts of turbo c were incorporated from the wizard c in 1987, but it was first developed by Dennis Ritchie in 1970.


What has the author Eric P Bloom written?

Eric P. Bloom has written: 'The Turbo C++ trilogy' -- subject(s): C (Computer program language), C++ (Computer program language), Turbo C (Computer file), Turbo C++ 'Turbo C Plus Plus Trilogy' 'The C trilogy' -- subject(s): C (Computer program language)


Do a program with turbo pascal to print the alphabet backward?

Turbo Pascal is a style of computer programming. This type of writing language can be used to create a program that prints the alphabet backwards.


What is the firing order diagram on rs turbo?

Firing order on a ford escort\fiesta rs turbo is 1342


What Bank does Turbo taxes?

Turbo Tax is a tax program that is available to users of the internet. The program is not for institutions, but for the ordinary consummer. There is an online version that one can easily reach by search engine. The questions are simple, easy to understand, and the program will file your taxes for you.


For loop program turbo c making a right triangle?

yes