How to Sort in Turbo C++

How to Sort in Turbo C++ thumbnail
There are many different types of sorting algorithms with varying complexity.

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.

Related Searches:

References

  • Photo Credit numbers image by Amer Delibasic from Fotolia.com

Comments

You May Also Like

  • Turbo C Sorting Methods

    Turbo C Sorting Methods. Sorting an array of data is one of the classic problems of computer science, and so it should...

  • How to Use Turbo C

    Turbo C is the Integrated Development Environment (IDE) for the C programming language created and sold by the Borland Corporation. Originally founded...

  • What Are the Functions of Turbo C?

    What Are the Functions of Turbo C?. Turbo C is a compiler for the programming language C. A compiler can be thought...

  • Computer BASIC Language Definition

    BASIC stands for Beginner's All Purpose Symbolic Instruction Code, and it is a type of computer programming language. Though it is not...

  • How to Use Arrays in Turbo C++

    In Turbo C++, arrays are used to hold collections of data that are unlikely to change in size very often. They are...

  • How to Install Turbo C

    With websites growing in popularity, it is more vital for a business to showcase its skills and what it has to offer...

  • Data Types for Turbo C

    Data Types for Turbo C. Turbo C is a compiler and development environment for the programming language C. It came out in...

  • Features of Turbo C

    Features of Turbo C. Borland's Turbo C, first introduced in 1987, applied the same integrated development model used by the Silicon Valley...

  • How to Learn Turbo C

    In the early days of the personal computer, Borland International's Turbo C led the market for those who wanted to get into...

  • Common Errors in Turbo C

    Common Errors in Turbo C. The C programming language is popular, but it is also a strict language in the sense that...

  • System Requirements for Turbo C

    System Requirements for Turbo C. Turbo C, first released in 1987, is a 16-bit C compiler for the Microsoft Disk Operating System...

  • How to Sort an Array

    Although you can sort an array manually by writing your own code for it, many programming languages have a built in function...

  • Visual Basic Sort Method

    Many older languages provide an array structure, but not any direct way to sort the array. This leaves the programmer to write...

  • How to Sort Columns in VBA

    VBA or "Visual Basic for Applications" is the programming language for writing macros (functions) in Excel spreadsheet. A VBA macro provides the...

  • Biblical Symbols Found in Chinese Oracles

    Some Biblical and secular scholars have observed correspondences between the composition of contemporary and ancient Chinese characters and symbols and stories in...

  • Turbo C Tutorial

    To use Turbo C, you will need to install and run Turbo C version 2.01 on a PC. To start Turbo C,...

  • How Does a Turbo Expander Work?

    Turbo expanders are one of the lesser known but most important components in gas-handling and pressurization systems. Turbo expanders work on one...

  • Define Turbo

    Turbochargers have undergone more than a century of development that make them one of the most powerful and efficient add-ons for any...

  • How to Sort Files Alphabetically in a Folder

    Windows Explorer allows files in a folder to be sorted alphabetically by the file's name. The files may be sorted in ascending...

  • How to Find the Maximum Value in an Array in Visual Basic

    Arrays are variables used in programming to hold a list of numbers or characters. Finding the maximum number value in an array...

Related Ads

Featured