Sorting
Sorting is one of the important operations done on array. Here the array elements are arranged in ascending or descending order. In this part we will study three different sorting techniques
- Bubble sort
- Selection sort
- Insertion sort


Now let us write the function to sort an array having 10 integers-
Program-70
#include <iostream.h>
void bubbleSort(int a[], int n){
int i, j, t;
for(i=1; i<=n-1; i++){
for(j=0; j<=(n-1)-i; j++){
if(a[j]>a[j+1]){
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
void main(){
int a[10], i;
for(i=0; i<= 9; i++){
cout<<"Enter the element :"<<i+1<<":";
cin>>a[i];
}
bubbleSort(a,10);
cout<<"The sorted array is as under:\n";
for(i=0; i<=9; i++){
cout<<a[i]<<" ";
}
}
In the "main()" function see that 10 is passed as number of element. In the function "bubbleSort()" the first loop counts the number of passes and the second loop bubbles the numbers, if necessary changes the position of the numbers. See that the second loop ranges from 0 (first number) to the second last number (n-1 is equal to 9, 9-i, i.e 9-1 equal to 8 which is the position of the second last number in the first pass); in the second pass it ranges from 0 to third last number and so on.