📄 pex2_10.cpp
字号:
#include <iostream.h>
// interchange the values of the two integer variables x and y
void Swap(int & x, int & y)
{
int temp = x; // store original value of x
x = y; // replace x by y
y = temp; // assign y the original value of x
}
// sort the n element integer array a in descending order.
void ExchangeSort(int a[], int n)
{
int i, j;
// implement n-1 passes. locate correct values
// in a[0],...,a[n-2].
for(i = 0; i < n-1; i++)
// put maximum of a[i+1]...a[n-1] in a[i]
for(j = i+1; j < n; j++)
// exchange if a[i] < a[j]
if (a[i] < a[j])
Swap(a[i], a[j]);
}
// step through the list and print each value.
void PrintList(int a[], int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
void main(void)
{
int list[15] = {38,58,13,15,51,27,10,19,
12,86,49,67,84,60,25};
cout << "Original List\n";
PrintList(list,15);
ExchangeSort(list,15);
cout << endl << "Sorted List" << endl;
PrintList(list,15);
}
/*
<Run of Program 2.4>
Original List
38 58 13 15 51 27 10 19 12 86 49 67 84 60 25
Sorted List
86 84 67 60 58 51 49 38 27 25 19 15 13 12 10
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -