1.选择排序
#include <stdio.h>
int main(void) {
int iTemp;
int iPos;
int i,j;
int a[10] = {65,45,32,13,67,98,25,42,18,23};
for (i = 0; i < 10; i++) {
printf("%d ",a[i]);
}
for (i = 0; i < 9; i++) {
iTemp = a[i];
for ( j = i+1; j < 10; j++) {
if(a[j]<iTemp){
iTemp = a[j];
iPos = j;
}
}
if(iPos!=0){
a[iPos] = a[i];
a[i] = iTemp;
iPos = 0;
}
}
printf("\n");
for (i = 0; i < 10; i++) {
printf("%d\t",a[i]);
}
return 0;
}
2、冒泡法
#include <stdio.h>
int main(void) {
int iTemp;
int i,j;
int a[10] = {65,45,32,13,67,98,25,42,18,23};
for(i=0;i<10;i++){
for (j = 9; j>=i; j--) {
if(a[j]<a[j-1]){
iTemp = a[j-1];
a[j-1]=a[j];
a[j] = iTemp;
}
}
}
printf("\n");
for (i = 0; i < 10; i++) {
printf("%d\t",a[i]);
}
return 0;
}
3、交换排序法
#include <stdio.h>
int main(void) {
printf("test");
int iTemp;
int iPos;
int i,j;
int a[10] = {65,45,32,13,67,98,25,42,18,23};
for(i=0;i<9;i++){
for(j=i+1;j<10;j++){
if(a[j]<a[i]){
iTemp = a[i];
a[i] = a[j];
a[j] = iTemp;
}
}
}
printf("\n");
for (i = 0; i < 10; i++) {
printf("%d\t",a[i]);
}
return 0;
}
4、插入排序法
#include <stdio.h>
int main(void) {
printf("test");
int iTemp;
int iPos;
int i,j;
int a[10] = {65,45,32,13,67,98,25,42,18,23};
for(i=0;i<10;i++){
iTemp = a[i];
iPos = i-1;
while((iPos>=0)&&(iTemp<a[iPos])){
a[iPos+1]=a[iPos];
iPos--;
}
a[iPos+1]=iTemp;
}
printf("\n");
for (i = 0; i < 10; i++) {
printf("%d\t",a[i]);
}
return 0;
}
5、折半法排序
#include <stdio.h>
void CelerityRun(int left,int right,int array[]);
int main(void) {
printf("test");
int iTemp;
int iPos;
int i,j;
int a[10] = {65,45,32,13,67,98,25,42,18,23};
CelerityRun(0,9,a);
printf("\n");
for (i = 0; i < 10; i++) {
printf("%d\t",a[i]);
}
return 0;
}
void CelerityRun(int left,int right,int array[]){
int i,j;
int middle,iTemp;
i = left;
j=right;
do{
while((array[i]<middle)&&(i<right)){
i++;
}
while((array[j]>middle)&&(j>left)){
j--;
}
if(i<=j){
iTemp = array[i];
array[i]=array[j];
array[j]=iTemp;
i++;
j--;
}
}while(i<=j);
}