How to Sort in Turbo C++
Turbo C++ has undergone a few name changes, but since it employs standards defined by the American National Standards Institute (ANSI), sorting routines with the same syntax are executable in all versions (including Turbo C if you still have a computer from the '80s). Turbo C++ replaced Turbo C around 1990; the August 2010 release is called C++ Builder XE. Turbo C++ extends the C programming language; therefore, most C operations are also valid in C++. Turbo C++ offers many different sorting algorithms; the simplest -- but perhaps not the most efficient for very long lists -- is the bubble-sort algorithm. This algorithm compares elements of the list and swaps values until the list is sorted.
Instructions
-
-
1
Declare a variable called "temp" that is of the same type as the list you are sorting. For example, if you are sorting an array of integers called "integerList" from lowest to highest, declare the variable temp as follows:
int temp;
Later, this variable will temporarily hold values for swapping.
-
2
Declare two integer variables -- "i" and "j." These will access elements in the list array. For example, to access element j, use the syntax "integerList[j]." It is tidier to declare these variables alongside the variable temp as follows:
int temp, i, j;
-
-
3
Declare an integer variable called "listLength" and assign to it the number of elements in the list. For example:
int listLength=30;
-
4
Construct the framework for a "for" loop, using the variable i as the iterator in its arguments, zero for the initial value, listLength for the end value and a step size of one. A "for" loop contains code that repeatedly gets executed over the number of steps defined by the "for" loops' three arguments, which include start value, end value and step size. Do this as follows:
for(i=0; i < listLength; i++)
{
code goes in here
}
-
5
Nest another "for" loop inside the previous loop, this time using the variable j as the iterator, i+1 for the start value, listLength for the end value and a step size of one. The code should now look like this:
int temp, i, j, listLength=30;
for(i=0; i < listLength; i++)
{
for(j=i+1; j< listLength;j++)
{
code goes here
}
}
This will first pick element i from the array being sorted and then go through the rest of the list, compare element j to element i and swap the values if element i is less than element j.
-
6
Insert an "if" loop to test if the j'th element is greater than the i'th as follows:
int temp, i, j, listLength=30;
for(i=0; i < listLength; i++)
{
for(j=i+1; j< listLength;j++)
{
if (integerList[i] > integerList[j])
{
code goes here
}
}
}
-
7
Insert the code to swap elements i and j if integerList[i] is greater than integerList[j] as follows:
int temp, i, j, listLength=30;
for(i=0; i < listLength; i++)
{
for(j=i+1; j< listLength;j++)
{
if (integerList[i] > integerList[j])
{
temp=integerList[i];
integerList[i]=integerList[j];
integerList[j]=temp;
}
}
}
Note that the variable temp declared earlier is now being used as a temporary holder of items in the list.
-
1
References
- Photo Credit numbers image by Amer Delibasic from Fotolia.com