- #include <iostream>
- #include <algorithm>
- using namespace std;
- template <typename Type>
- struct Print
- {
- void operator ()(Type& item)
- {
- cout << item << '/t';
- }
- };
- void sort(int x[], int n)
- {
- int temp;
- int pos;
- int low;
- int high;
- int mid;
- for(int i = 1; i < n; ++i)
- {
- temp = x[i];
- pos = -1;
- if (temp < x[0])
- {
- pos = 0;
- }
- else if (temp < x[i - 1])
- {
- low = 0;
- high = i - 1;
- while (low <= high)
- {
- mid = (low + high) / 2;
- if (x[mid] <= temp)
- {
- low = mid + 1;
- }
- else
- {
- high = mid - 1;
- }
- }
- pos = low;
- }
- if (pos >= 0)
- {
- memmove((void*)&x[pos + 1], (void*)&x[pos], sizeof(int) * (i - pos));
- x[pos] = temp;
- }
- }
- }
- int main(int argc, char *argv[])
- {
- int x[10];
- for(int i = 0; i < 10; ++i)
- {
- x[i] = rand() % 100;
- }
- for_each(x, x + 10, Print<int>());
- cout << endl;
- sort(x, 10);
- for_each(x, x + 10, Print<int>());
- cout << endl;
- return 0;
- }