Hello everyone,
I have some trouble with insertion sort algorithm,
Here's the code which work perfect
#include <iostream>
> using namespace std ;
>
> #define LIMIT 13
>
> int main()
> {
> int key ;
> int i,j ;
> int array[LIMIT] = {6, 8, 9, 10, 12, 7, 1, 3, 2, 14, 13, 5, 4} ;
>
> cout << "Before sorting ...." << endl ;
> for(i=0; i<LIMIT; i++)
> cout << array[i] << endl ;
> cout << " ....." << endl ;
>
> for(i=1; i<LIMIT; i++)
> {
> key = array[i] ;
> j = i-1 ;
>
> while(array[j] > key && j>=0) // Look @ this
> condition
> {
> array[j+1] = array[j] ;
> j-- ;
> }
> array[j+1] = key ;
> }
>
> cout << "After sorting ...." << endl ;
>
> for (i=0; i<LIMIT; i++)
>
> cout << array[i] << endl ;
>
> return 0 ;
> }
>
>
But here the code doesn't work good ..
#include <iostream>
using namespace std ;
#define LIMIT 13
int main()
{
int key ;
int i,j ;
int array[LIMIT] = {6, 8, 9, 10, 12, 7, 1, 3, 2, 14, 13, 5, 4} ;
cout << "Before sorting ...." << endl ;
for(i=0; i<LIMIT; i++)
cout << array[i] << endl ;
cout << " ....." << endl ;
for(i=1; i<LIMIT; i++)
{
key = array[i] ;
j = i-1 ;
while(j>=0) // And look @ this condition
{
if( array[j] > key ) // added with an if condition
{
array[j+1] = array[j] ;
j-- ;
}
}
array[j+1] = key ;
}
for (i=0; i<LIMIT; i++)
cout << "After sorting ...." << array[i] << endl ;
return 0 ;
}
Thanks
--
Shamir Shakir
http://www.DewsWorld.t35.com
:::::.......... Hope never ends...........:::::
[Non-text portions of this message have been removed]