Tuesday, 23.04.2024, 12:04
Welcome Guest | RSS
Site menu
Login form
Section categories
Search
Calendar
«  July 2011  »
SuMoTuWeThFrSa
     12
3456789
10111213141516
17181920212223
24252627282930
31
Entries archive
Our poll
Rate my site
Total of answers: 6
Statistics

Total online: 1
Guests: 1
Users: 0

Main » 2011 » July » 1 » Gnome Sort Algorithm
21:17
Gnome Sort Algorithm
#include <cstdlib>
#include <iostream>


//Gnome Sort Algorithm


using namespace std;

//gnomeSort Function
void gnomeSort(int elements, int arr[])
{
    int i = 0, temp;

    while( i < elements ){

           if ( i == 0 || arr [i - 1] <= arr[i] )
              i++;

           else{
                temp = arr[i];
                arr[i] = arr[i - 1];
                arr[--i] = temp;
           }//end else

    }//end while

}//end GnomeSort Function

int main(int argc, char *argv[])
{
    //Test data set
    int test[] = {6, 4, 2, 3, 1, 5, 8};

    cout << "***Gnome Sort Algorithm***" <<endl <<endl;

    //Initial Unsorted List
    cout << "Initial Unsorted List: "<<endl;
    for ( int i = 0; i < 7; i++ )
        cout << test[i] << " ";

    //Calling gnomeSort
    gnomeSort(7, test);
    cout <<endl <<endl;

    //After product; Sorted List
    cout << "Sorted List: "<<endl;
    for ( int i = 0; i < 7; i++ )
        cout << test[i] << " ";

    system("PAUSE");
    return EXIT_SUCCESS;
}

Category: C++ programs | Views: 697 | Added by: H_Imirzyan | Tags: algorithm, sort, gnome | Rating: 0.0/0
Total comments: 0
Name *:
Email *:
Code *: