24 Juni 2009

Program C++ Insertion Sort 1

24 Juni 2009
#include <iostream.h>
#include <conio.h>

int main(void)
{
int array[5]; // An array of integers.
int length = 5; // Lenght of the array.
int i, j;
int keyelement;

//Some input
for (i = 0; i < length; i++)
{
cout << "Enter a number: ";
cin >> array[i];
}

//Algorithm
for(j = 1; j < length; j++) // Note!
{
keyelement = array[j];
for (i = j - 1; (i >= 0) && (array[i] < keyelement); i--)
{
array[i+1] = array[i];
}
array[i+1] = keyelement;
}

//Some output
for (i = 0; i < 5; i++)
{
cout<<endl;
cout << array[i] << endl;
}
getch();
}

Program Setelah di eksekusi:

0 comments:

Poskan Komentar

sampaikan komentar dengan sopan dan bertanggung jawab :)