Select Sort

程式碼:

#include<stdio.h>
#include<stdlib.h>
void Select_Sort(int [],int);
int main(void){
 int arr[] = {37,61,19,41,81,25,56,41,49};
 Select_Sort(arr,9);
 
 system("pause");
 return 0;
}

void Select_Sort(int arr[],int n){
 int i,j,k,max;
 for(i=0;i<n-1;i++){
  max=i; 
  /*找出這陣列比arr[max]小然後交換*/ 
  for(j=i+1;j<n;j++){
   if(arr[max]>arr[j]){ //初始設定arr[max]最大 
    max = j;
   }
  }
  int temp = arr[max];
   arr[max] = arr[i];
   arr[i] = temp;
  /*印出每一輪的排序*/ 
  for(k=0;k<n;k++){
    printf("%d ",arr[k]);
  }
  printf("\n");
 }
}



結果:

留言

熱門文章