/* Name: 超长数列中n个整数排序 Copyright: tonyee Author: tonyee Date: 20-03-09 18:50 Description: C++ 练习题 */ #include <iostream> using namespace std; class LIST { int size; int *arr; public: LIST(int a[], int len) // 构造函数,用len初始化size动态分配数组存储空间 arr 指向该存储空间 { size = len; arr = new int[len]; for(int i=0;i<len; i++) arr[i] = a[i]; } void sortpart(int m, int n); void output() { for(int i = 0; i<size; i++) cout << arr[i] <<'/t'; cout << endl; } ~LIST() { if(arr) delete [] arr; } }; void LIST::sortpart(int m, int n) { int i, j, t; for(i=m-2;i<m+n-1;i++) // m-1,m-2 m-1是正常的下标,m-2时扣除第一个元素 for(j=i;j<m+n-2;j++) if(arr[j]<arr[j+1]) { t = arr[j]; arr[j] = arr[j+1]; arr[j+1] = t; } } int main(int argc, char *argv[]) { int a[10] = {1, 8, 3, 0, 5, 9, 7, 6, 9, 8}; LIST tt(a,sizeof(a)/sizeof(a[0])); cout << "原数据:/n"; tt.output(); cout << endl; tt.sortpart(4,5); cout << "现数据:/n"; tt.output(); cout << endl; system("PAUSE"); return EXIT_SUCCESS; }