C++ PRACTICAL PROGRAMS

Class 12 Computer Science | ASSEB

Program to sort the elements of an array using insertion sort

#include<iostream>

using namespace std;

int main( )

{

int a[10], i, j, k, temp;

cout<<”Enter any 10 number for sorting”;

for (i=0; i<10; i++)

{

cin>>a[i];

}

for (i=1; i<10; i++)

{

for (j=i; j>=1; j–)

{

if (a[j]<a[j-1])

{

temp = a[j];

a[j] = a[j-1];

a[j-1] = temp;

}

                   else

break;

}

}

cout<<”Sorted array”<<endl;

for (k=0;k<10; k++)

{

cout<<a[k]<<endl;

}

return 0;

}

Output

Enter any 10 number for sorting9

1

5

3

7

2

6

8

4

2

Sorted array

1

2

3

4

5

6

7

8

9

QUICK LINKS

Class 11

Class 12

Previous Year Paper

Practical Programs

Scroll to Top