#include <stdio.h>
//冒泡排序
void maopao(int arr[],int len){
for(int i=0;i<len-1;i++){
for(int j=0;j<len-i-1;j++){
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
//选择排序
void selectSort(int arr[],int len){
int temp;
for(int i=0;i<len-1;i++){
for(int j=i+1;j<len;j++){
if(arr[i] > arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
//折半算法 也称为 二分查找算法
int TwoSort(int * ListData,int ListLength)
{
int i = 0;
int j = 0;
for(i=1;i<=ListLength;i++)
{
int tmp = ListData[i];
int low = 0;
int hight = i-1;
int mid;
while(low <= hight)
{
mid = (low+hight)/2;
if(tmp > ListData[mid])
low = mid+1;
else
hight = mid-1;
}
for(j=i-1;j>=low;j--)
ListData[j+1]=ListData[j];
ListData[low]=tmp;
}
return 0;
}
int main(){
int a[9] = {1,3,5,7,6,8,9,2,4};
printf("\n冒泡排序源数据\n");
for(int g=0;g<9;g++){
printf("%d\t",a[g]);
}
maopao(a,9);
printf("\n冒泡排序后数据\n");
for(int i=0;i<9;i++)
printf("%d\t",a[i]);
int b[9] = {1,3,5,7,6,8,9,2,4};
printf("\n选择排序源数据\n");
for(int g=0;g<9;g++){
printf("%d\t",b[g]);
}
selectSort(b,9);
printf("\n选择排序后数据\n");
for(int i=0;i<9;i++)
printf("%d\t",b[i]);
int c[9] = {1,3,5,7,6,8,9,2,4};
printf("\n折半算法源数据\n");
for(int g=0;g<9;g++){
printf("%d\t",c[g]);
}
TwoSort(c,9);
printf("\n折半算法后数据\n");
for(int i=0;i<9;i++)
printf("%d\t",c[i]);
return 0;
}