Question:
Sort n integers in a series
Problem Description
Design a series containing the number of size, which requires the ability to sort the number of y starting from the specified position x into a descending order, and output a new complete series. You can store the series in a one-dimensional array.
For example, the original columns have 10 numbers and the values are {4th, 3, 0, 5, 9, 7, 6, 9, 8}. If you want to sort the five numbers starting from in descending order,
The new number of columns is {, 9, 8 }. Create a class LIST to complete the above functions.
Class LIST
{
Public:
LIST (int a [], int len); // constructor, uses len to initialize the size, dynamically allocates an array bucket based on the size, and arr points to the bucket
Void sortpart (int m, int n); // sort the number of n numbers starting from the m element in descending order.
Void output (); // output the entire sequence
~ LIST (); // destructor to release the bucket pointed to by arr
Private:
Int size; // Number of Columns
Int * arr; // The starting pointer of the array of a series.
};
Input
There are multiple groups of input data, each group:
The number of integer series input in Row 3 is n;
The value of each element in the integer series is input in Row 3;
Input x and y in the second row to sort the super-long integer series in descending order from the number of y at the specified position x;
Each group of Output results are sorted and occupies one row. Each element is followed by a space.
Sample Input
101 4 2 7 3 8 2 8 23 63 5
Sample Output
1 4 8 7 3 2 2 8 23 6
Reference code:
#include <iostream>using namespace std;
class LIST{private: int size; int *arr; public: LIST(int a[],int len); void sortpart(int m,int n); void output(); ~LIST(); };LIST::LIST(int a[],int len){ size=len; arr=new int [size]; int i; for(i=0;i<size;i++) arr[i]=a[i];}void LIST::sortpart(int m,int n){ int i,j,t; for(i=0;i<n;i++) { for(j=m-1;j<m+n-i-2;j++) { if(arr[j]<arr[j+1]) { t=arr[j]; arr[j]=arr[j+1]; arr[j+1]=t; } } } }void LIST::output(){ int i; for(i=0;i<size;i++) cout<<arr[i]<<" "; cout<<endl;}LIST::~LIST(){ delete []arr;}
int main(){ int i,n,*a,x,y; while(cin>>n) { a=new int [n]; for(i=0;i<n;i++) cin>>a[i]; LIST w(a,n); cin>>x>>y; w.sortpart(x,y); w.output(); delete []a; } return 0;}
Note: pay special attention to the position at the beginning and end of the group when sorting ......