Google Translate

Insertion Sort 1

#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:
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© ILMU TEKNIK INFORMATIKA
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0
Posts RSSComments RSS
Back to top