#include <cstdlib> 
#include <ctime> 
#include <iostream>  
using namespace std;  
int findSmallestRemainingElement 
(int array[], int size, int index);
void swap (int array[], int first_index, int second_index);  
void sort (int array[], int size)
    {  
    for ( int i = 0; i < size; i++ )  
       {   int index = findSmallestRemainingElement( array, size, i );   
       swap( array, i, index ); 
     } 
       }  
       int findSmallestRemainingElement (int array[], int size, int index)
        {  
        int index_of_smallest_value = index;  
        for (int i = index + 1; i < size; i++)  
        {   
        if ( array[ i ] < array[ index_of_smallest_value ]  )  
        {  
        index_of_smallest_value = i;   
        } 
 } 
 return index_of_smallest_value; 
 }   
void swap (int array[], int first_index, int second_index) 
    { 
    int temp = array[ first_index ];  
    array[ first_index ] = array[ second_index ]; 
    array[ second_index ] = temp; }  // small helper method to display the before and after arrays void displayArray 
    (int array[], int size) 
    {  
    cout << "{";  for ( int i = 0; i < size; i++ )  
    {   

    if ( i != 0 )   
    {    cout << ", ";   }   cout << array[ i ];  }  cout << "}"; 
    }
int main () 
    { 
    int array[ 10 ];  
    srand( time( NULL ) ); 
    for ( int i = 0; i < 10; i++ ) 
{   

array[ i ] = rand() % 100;  
} 
cout << "Original array: ";  
displayArray( array, 10 );  
cout << '\n';   sort( array, 10 );  


cout << "Sorted array: "; 
displayArray( array, 10 );  

}

//can somene please explain what is going on. I hardly understand this code.

Edited 2 Years Ago by Praveen_10

It really helps to have some reasonable indentation to see what's what. Here's the code, formatted better.

#include <cstdlib> 
#include <ctime> 
#include <iostream>  
using namespace std;  
int findSmallestRemainingElement (int array[], int size, int index);
void swap (int array[], int first_index, int second_index);  

void sort (int array[], int size)
{  
    for ( int i = 0; i < size; i++ )  
    {   int index = findSmallestRemainingElement( array, size, i );   
    swap( array, i, index ); 
    } 
} 

int findSmallestRemainingElement (int array[], int size, int index)
{  
    int index_of_smallest_value = index;  
    for (int i = index + 1; i < size; i++)  
    {   
        if ( array[ i ] < array[ index_of_smallest_value ]  )  
        {  
            index_of_smallest_value = i;   
        } 
    } 
    return index_of_smallest_value; 
}   

void swap (int array[], int first_index, int second_index) 
{ 
    int temp = array[ first_index ];  
    array[ first_index ] = array[ second_index ]; 
    array[ second_index ] = temp; }  // small helper method to display the before and after arrays void displayArray 


(int array[], int size) 
{  
    cout << "{";  for ( int i = 0; i < size; i++ )  
    {   

        if ( i != 0 )   
        {    cout << ", ";   }   cout << array[ i ];  }  cout << "}"; 
}

int main () 
{ 
    int array[ 10 ];  
    srand( time( NULL ) ); 
    for ( int i = 0; i < 10; i++ ) 
    {   

        array[ i ] = rand() % 100;  
    } 
    cout << "Original array: ";  
    displayArray( array, 10 );  
    cout << '\n';   sort( array, 10 );  


    cout << "Sorted array: "; 
    displayArray( array, 10 );  

}

I can't say I've seen a simple insertion sort made so convoluted by breaking into so many parts.

Simply, sort( ) is calling the find smallest to locate the one value that belongs in position i. The swap function takes the value at i and the smallest value and exchanges them in the array, using swap.
Move up to the next place in the array, repeat till done.

There appears to be some missing code just following the swap function - I suppose it's the beginning of your display function.

This article has been dead for over six months. Start a new discussion instead.