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; } | |
|
Total comments: 0 | |