选择排序如下:
/*
HDU OJ 1040
solved at 09.6.19
*/
#include
<
iostream
>
using
namespace
std;
int
main()
{
int
T,N;
int
i,j,t;
cin
>>
T;
while
(T
--
)
{
cin
>>
N;
int
*
a
=
new
int
[N];
for
(i
=
0
;i
<
N;
++
i)
cin
>>
a[i];
for
(i
=
0
;i
<
N;
++
i)
{
t
=
i;
for
(j
=
i
+
1
;j
<
N;
++
j)
if
(a[j]
<
a[t])
t
=
j;
int
temp
=
a[i];
a[i]
=
a[t];
a[t]
=
temp;
}
for
(i
=
0
;i
<
N;
++
i)
if
(i
==
0
)
cout
<<
a[i];
else
cout
<<
"
"
<<
a[i];
cout
<<
endl;
}
return
0
;
}
冒泡排序如下:
#include
<
iostream
>
using
namespace
std;
int
main()
{
int
T,N;
int
i,j;
cin
>>
T;
while
(T
--
)
{
cin
>>
N;
int
*
a
=
new
int
[N];
for
(i
=
0
;i
<
N;
++
i)
cin
>>
a[i];
for
(i
=
0
;i
<
N;
++
i)
{
//
注:此处的i有双重含义,即表示要循环比较的次数,又表示要比较的范围
for
(j
=
N
-
1
;j
>
i;
--
j)
if
(a[j]
<
a[j
-
1
])
{
int
temp
=
a[j];
a[j]
=
a[j
-
1
];
a[j
-
1
]
=
temp;
}
}
for
(i
=
0
;i
<
N;
++
i)
if
(i
==
0
)
cout
<<
a[i];
else
cout
<<
"
"
<<
a[i];
cout
<<
endl;
}
return
0
;
}