Sort an Array Elements in Ascending Order in C++

Advertisements

Prev Tutorial Next Tutorial

C++ Program to Sort an Array Elements in Ascending Order

Sort an array elements means arrange elements of array in Ascending Order and Descending Order. You can easily sort all elements using bubble sort.
Sort Array Elements in Ascending order means arrange elements of array in Increasing Order.

To understand below Example, you have must knowledge of following C++ programming topics; For Loop in C++ and Swap Program in C++.

Process of Sorting an Array

The process of sorting an array requires the exchanging of Array values. While this seems to be a simple process, a computer must be careful that no values are lost during this exchange.
Suppose that Marks[1] = 60 and Marks[2] = 80 and you want to exchange their values so that Marks[1] = 80 and Marks[2] = 60. So you could NOT just do this

Example

Marks[1] = Marks[2];
Marks[2] = Marks[1];  // NOT WORK! Both Marks[1] and Marks[2] Store 80

In the first step, the value stored in Marks[1] is erased and replaced with Marks[2]. The result is that both Marks[1] and Marks[2] now have the same value. Oops! Then what happened to the value in Marks[1]? It is lost!!!

In order to swap two values, you must use a third variable, (a "temporary holding variable"), to temporarily hold the value you do not want to lose:

Example

temp = Marks[1];     // holding variable
Marks[1] = Marks[2];
Marks[2] = temp;

This process successfully exchanges, "swaps", the values of the two variables (without the loss of any values).

Different Ways to Sort Array Elements

There are literally hundreds of different ways to sort arrays. The basic goal of each of these methods is the same: to compare each array element to another array element and swap them if they are in the wrong position.

  • Bubble Sort
  • Exchange Sort
  • Selection Sort
  • Insertion Sort
  • Shell Sort
  • Quick Sort
  • Merge Sort

In Below code we sort array Elements in Ascending Order Using Bubble sort (Bubble Sort Program in C++). Bubble sort is based on the idea of repeatedly comparing pairs of adjacent elements and then swapping their positions if they exist in the wrong order

C++ Program to Sort Elements of Array in Ascending Order

#include<iostream.h>
#include<conio.h>

 void main()
 {
 int i,a[10],temp,j;
 clrscr();
 cout<<"Enter any 10 num in array: \n";
 for(i=0;i<=10;i++)
 {
 cin>>a[i];
 }
 cout<<"\nData before sorting: ";
 for(j=0;j<10;j++)
 {
 cout<<a[j];
 }
 for(i=0;i<=10;i++)
 {
 for(j=0;j<=10-i;j++)
 {
 if(a[j]>a[j+1])
 {
 temp=a[j];
 a[j]=a[j+1];
 a[j+1]=temp;
 }
 }
 }
 cout<<"\nData after sorting: ";
 for(j=0;j<10;j++)
 {
 cout<<a[j];
 }
 getch();
 }

Output

Enter any 10 num in array:
2 5 1 7 5 3 8 9 11 4
Data After Sorting: 1 2 3 4 5 7 8 9 11

Output

sort array elements ascending order
Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements