#include <cstdlib> #include <ctime> #include <iostream> using namespace std; int main() { const int MAX = 5; int a[MAX+1]; srand((unsigned)time(0)); for(int i=1; i<MAX+1; i++) { a[i] = (rand()%100)+1; cout << a[i] << " "; } cout << "(Initial array)" << "\n"; int x; int l; int s; int stop; int temp_x; x = int(MAX / 2); while (x > 0) { stop = 0; l = MAX - x; while (stop == 0) { s = 0; for (int k=1; k<=l; k++) { if (a[k] > a[k+x]) { temp_x = a[k]; a[k] = a[k+x]; a[k+x] = temp_x; s = k; } } l = s - x; if (s == 0) { stop = 1; } } for (int i=1; i<=MAX; i++) { cout << a[i] << " "; } cout << "\n"; x = int(x / 2); } for(int i=1; i<MAX+1; i++) { cout << a[i] << " "; } cout << "(Sorted array)" << "\n"; cout << "\nPress ENTER to continue..." << endl; getchar(); }
Sunday, 29 April 2012
Program for Shell Sort in C++
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment