Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <ctime>
- using namespace std;
- template <typename T>
- struct list
- {
- list<T> *b;
- list<T> *s;
- T val;
- unsigned int count;
- };
- template <typename T>
- void sort(T *inf, unsigned int size, list<T> **t)
- {
- if ((*t) == NULL)
- {
- (*t) = new list<T>;
- (*t)->val = inf[0];
- (*t)->b = NULL;
- (*t)->s = NULL;
- (*t)->count = 1;
- }
- else if (inf[0] < (*t)->val) sort(&inf[0], 0, &(*t)->s);
- else if (inf[0] == (*t)->val) (*t)->count++;
- else sort(&inf[0], 0, &(*t)->b);
- for (; size>1; size--)
- {
- sort(&inf[size - 1], 0, t);
- if (size == 2)
- {
- unsigned int offset = 0;
- show_up(&inf[0], *t, offset);
- }
- }
- }
- template <typename T>
- void show_up(T *inf, list<T> *t, unsigned int &offset)
- {
- if (t->s != NULL) {
- show_up(&inf[0], t->s, offset);
- delete t->s; }
- while (t->count--) { inf[offset] = t->val; offset++; }
- if (t->b != NULL) {
- show_up(&inf[0], t->b, offset);
- delete t->b; }
- return;
- }
- int v=1000000; list<int> *f = NULL;
- unsigned int offset = 0, n, c;
- int main()
- {
- setlocale(LC_ALL, "Russian");
- //system("mode con cols=64 lines=59");
- system("pause >> void");
- c = clock();
- int *i = (int*)malloc(v*sizeof(int));
- for (n = 0; n<v; n++) i[n] = rand();
- cout << "На генерацию массива в 1 000 000 элементов типа int затрачено " <<clock()-c << " мс." << endl;
- system("pause>>void");
- cout << "Сортировка." << endl;
- c = clock();
- sort(&i[0], v, &f);
- free(f); f = NULL;
- cout << endl<<"На сортировку массива в 1 000 000 элементов типа int затрачено " << clock() - c << " мс." << endl;
- system("pause>>void");
- for (int s = 0; s < v; s++)
- {
- cout <<left <<setw(8) << i[s];
- }
- free(i);
- system("pause>>void");
- return 0;
- }
- //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement