/*
*对于一组凌乱的数据,如何给它们排序?
*可用指针将它们(地址)组合成一个数组,然后进行比较
*/
#include <iostream>
using namespace std;
int main()
{
const int a = 10, b = 45, c = 23, d = 78, e = 90, f = 7;
const int* p[6] = {&a, &b, &c, &d, &e, &f};
cout << "未序:" << endl;
for(int i = 0; i != 6; i++)
cout << *p[i] << ' ';
cout << endl;
cout << "未序地址:" << endl;
for(int i = 0; i != 6; i++)
cout << p[i] << ' ';
cout << endl;
//排序一:
/*for(int i = 0; i != 6; i++)
{
for(int j = 1; j != 6-i; j++)
if(*p[j] < *p[j-1])
swap(p[j],p[j-1]);
}*/
//排序二:
for(int i = 0; i != 6; i++)
{
for(int j = i+1; j != 6; j++)
{
if(*p[j] < *p[i])
swap(p[j],p[i]);
}
}
cout << "有序:" << endl;
for(int i = 0; i != 6; i++)
cout << *p[i] << ' ';
cout << endl;
cout << "有序地址:" << endl;
for(int i = 0; i != 6; i++)
cout << p[i] << ' ';
cout << endl;
return 0;
}
*对于一组凌乱的数据,如何给它们排序?
*可用指针将它们(地址)组合成一个数组,然后进行比较
*/
#include <iostream>
using namespace std;
int main()
{
const int a = 10, b = 45, c = 23, d = 78, e = 90, f = 7;
const int* p[6] = {&a, &b, &c, &d, &e, &f};
cout << "未序:" << endl;
for(int i = 0; i != 6; i++)
cout << *p[i] << ' ';
cout << endl;
cout << "未序地址:" << endl;
for(int i = 0; i != 6; i++)
cout << p[i] << ' ';
cout << endl;
//排序一:
/*for(int i = 0; i != 6; i++)
{
for(int j = 1; j != 6-i; j++)
if(*p[j] < *p[j-1])
swap(p[j],p[j-1]);
}*/
//排序二:
for(int i = 0; i != 6; i++)
{
for(int j = i+1; j != 6; j++)
{
if(*p[j] < *p[i])
swap(p[j],p[i]);
}
}
cout << "有序:" << endl;
for(int i = 0; i != 6; i++)
cout << *p[i] << ' ';
cout << endl;
cout << "有序地址:" << endl;
for(int i = 0; i != 6; i++)
cout << p[i] << ' ';
cout << endl;
return 0;
}