//冒泡排序 升序
int bubble(int a[10]){
int temp;
for(int i =0;i<10;i++){
for(int j = 1;j<10-i;j++){
if(a[j-1] >a[j]){
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
}
}
printf("冒泡排序結果:\n");
for(int i = 0;i<10;i++){
printf("%d ",a[i]);
}
printf("\n");
return 0;
}
//直接插入排序 升序
int InsertSort(int a[10],int n){
int i,j;
int temp;
for(i = 0;i < n;i++){
temp = a[i];
j = i - 1;
while (j >= 0 && temp < a[j]){
a[j+1] = a[j];
j--;
}
a[j + 1] = temp;
}
printf("直接排序結果:\n");
for(int i = 0;i<10;i++){
printf("%d ",a[i]);
}
printf("\n");
}
//選擇排序 升序
void select(int a[],int n){
int i = 0;
int tempnum,tempweb;
for(;i < n;i++){
tempnum = a[i];
for(int j = i+1;j<n;j++){
if(tempnum > a[j]){
tempweb = j;
tempnum = a[j];
}
}
a[tempweb] = a[i];
a[i] = tempnum;
}
printf("選擇排序結果:\n");
for(i = 0;i < n;i++){
printf("%d ",a[i]);
}
}
//希爾排序 升序
void shell(int a[],int n,int incre){
int temp,j;
for (; incre >=1 ; incre/=2) {
for(int i = 0;i <incre;i++){
for(int k = i;k < n;k+=incre){
temp = a[k];
j = k - incre;
while (j >= 0 && temp < a[j]){
a[j+incre] = a[j];
j-=incre;
}
a[j + incre] = temp;
}
}
}
sys(a,n);
}
//快速排序 升序
void quicksort(int a[],int low,int high){
int i = low,j = high;
int temp;
if(low < high){
temp = a[low];
while(i < j){
while(j > i && a[j] >= temp){
--j;
}
if(i < j){
a[i] = a[j];
i++;
}
while (i < j && a[i] <= temp){
++i;
}
if(i < j){
a[j] = a[i];
j--;
}
}
a[i] = temp;
quicksort(a,low,i-1);
quicksort(a,i+1,high);
}
}
C語言實現部分排序算法
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.