SDUST数据结构 - chap9 排序
阅读原文时间:2023年07月08日阅读:3

判断题:

选择题:

编程题:

输入样例:

11
4 981 10 -17 0 -20 29 50 8 43 -5

输出样例:

-20 -17 -5 0 4 8 10 29 43 50 981

代码:

#include
using namespace std;
long A[100001];
// 插入排序
void insertion_sort(long A[], int N)
{
for (int P = 1; P < N; P++) {//数组第=个已经有序 long temp = A[P];//摸一张牌 int i; for (i = P; i > 0 && A[i - 1] > temp; i--)
A[i] = A[i - 1];
A[i] = temp;
}
}
int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++) //scanf("%d", &A[i]); cin>>A[i];
//Bubble_sort(A, N);
insertion_sort(A, N);
cout << A[0];
for (int i = 1; i < N; i++)
cout << " " << A[i];
return 0;
}

输入样例:

8 3
8 12 7 3 20 9 5 18

输出样例:

20 18 12

代码:

#include
using namespace std;
int sum[1000000];//根据题目条件创建一个很大的数组
int main()
{
int n, m;
cin>>n>>m;
int i, j;
//int sum[1000000];
int temp;
for(i = 0; i < n; i++)//把输入的财产一次存入数组 { //cin>>sum[i];
scanf("%d",&sum[i]);
}
for(i=0;isum[j+1])
{
temp = sum[j];
sum[j] = sum[j+1];
sum[j+1] = temp;
}
}
}
if(n>=m)
{
for(i=n-1;i>n-m;i--)//以下输出是为了便于解决空格问题
{
//cout<0;i--)//以下输出是为了便于解决空格问题
{
//cout<<sum[i]<<" ";
printf("%d ",sum[i]);
}
printf("%d",sum[0]);

}  
return 0;  

}

输入样例:

4 4
51 100 1000
36 110 300
6 14 32
5 18 40
0 1 2 3

输出样例:

1:1 1:2 1:3 1:4

代码:

#include
#include
#include
struct GuoJia
{
double JP[4];
int Score[4];
}GuoJias[224];
int arr[224];
int Note[224];
void Swap(int i, int j)
{
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
void Init(int N)
{
for (int i = 0; i < N; i++) { arr[i] = i; Note[i] = 0; } } void QSort(int low,int high,int i) { if (low >= high - 1)
return;
int mid = (low + high) / 2;
Swap(low, mid);
int k = low + 1;
for (int j = low + 1; j < high; j++) { if (GuoJias[arr[j]].JP[i] > GuoJias[arr[low]].JP[i])
Swap(k++, j);
}
Swap(low, k- 1);
QSort(low, k - 1, i);
QSort(k, high, i);
}
void panduan(int N,int i)
{
for (int j = 0; j < N; j++) { if (j > 0 && GuoJias[arr[j]].JP[i] == GuoJias[arr[j - 1]].JP[i])
GuoJias[arr[j]].Score[i] = GuoJias[arr[j - 1]].Score[i];
else
GuoJias[arr[j]].Score[i] = j;
}
}
int main()
{
int N, M;
scanf("%d%d", &N, &M);
double num;
for (int i = 0; i < N; i++)
{
scanf("%lf%lf%lf", &GuoJias[i].JP[0], &GuoJias[i].JP[1], &num);
GuoJias[i].JP[2] = GuoJias[i].JP[0] / num;
GuoJias[i].JP[3] = GuoJias[i].JP[1] / num;
}
for (int i = 0; i < 4; i++)
{
Init(N);
QSort(0, N, i);
panduan(N,i);
}
int n;
for (int i = 0; i < M; i++)
{
int minsort = 99999;
int min = 99999;
scanf("%d", &n);
for (int j = 0; j < 4; j++)
{
if (GuoJias[n].Score[j] < minsort)
{
minsort = GuoJias[n].Score[j];
min = j;
}
else if (GuoJias[n].Score[j] == minsort && j < min)
min = j;
}
if(i!=M-1)
printf("%d:%d ", minsort+1, min+1);
else
printf("%d:%d", minsort + 1, min + 1);
}
return 0;
}

输入样例:

6 2
2 3 5 1 6 4

输出样例:

2 1 3 4 5 6

代码:

#include
int main()
{
int n,K;
scanf("%d%d",&n,&K);
int a[n],i=0,j;
while(ia[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
i=0;
while(i<n-1)
printf("%d ",a[i++]);
printf("%d",a[i]);
}