Insertion Sort in C++

by Sandeep on July 20, 2009

Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array (or list) is built one entry at a time.
It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.
Program:

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

void sort(int * a) {

for (int j = 2; j < 10; j++) {
for (int k = 0; k < j; k++) {
if (a[j] < a[k]) {
int temp = a[k];
a[k] = a[j];
a[j] = temp;
}
}
}

for (int i = 0; i < 10; i++) {
cout << a[i] << “n”;
}

}

void main() {
clrscr();
int a[] = {
1, 4, 6, 8, 0, 9, 7, 5, 2, 3};
sort(a);
}


if you find any bug in the above program. let us know about it. Please post your complaint at pctech@gadgetcage.com.

ADVERTISEMENT

We'll send more interesting posts like Insertion Sort in C++ to you!
Enter your Email Address:
Join us on Facebook.

  HostGator
    

Leave a Comment

Previous post:

Next post: